FileInputStream issue - App has stopped working - android-studio

I am having trouble with FileInputStream in Android Studio. My application has stopped working when I use it to read file .txt in the internal memory (I have writen data to this file successfully). I have try many solutions but they didn't work. Can anyone give the solution for this problem? Here is my code. Thanks very much. (I have added users-permission "WRITE_EXTERNAL_STORAGE" and "READ_EXTERNAL_STORAGE").
String fileName = "infoProfile.txt";
private int readData() {
try {
String path = MainActivity.this.getFilesDir().getAbsolutePath() + "/" + fileName;
FileInputStream in = openFileInput(path);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
ArrayList<String> str = new ArrayList<>();
String data = "";
while ((data = reader.readLine()) != null) {
str.add(data);
}
in.close();
if (str.isEmpty())
return 0;
etName.setText(str.get(0));
etEmail.setText(str.get(1));
etPhone.setText(str.get(2));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return 1;
}

Related

How to print from Android Studio Barcode printer

Barcode I want to send a barcode to the printer. I tried it in PDF, JPEG, PNG format. It prints a blank page on all of them.I couldn't find any results on google.
I need to do it without using Printer Helper, if possible
public class yazdır extends AsyncTask<Void , Void,Void> {
#Override
protected Void doInBackground(Void... arg0) {
DataOutputStream outToServer;
Socket clientSocket;
File filename;
PrintHelper photoPrinter = new PrintHelper(getActivity());
photoPrinter.setScaleMode(PrintHelper.SCALE_MODE_FIT);
for (int i=0;i<=adet;i++) {
try {
String path = Environment.getExternalStorageDirectory().toString();
filename = new File(path + "/folder/subfolder/image.png");
FileInputStream fileInputStream = new FileInputStream(filename);
InputStream is = fileInputStream;
clientSocket = new Socket("192.168.1.31", 9100);
outToServer = new DataOutputStream(clientSocket.getOutputStream());
byte[] buffer = new byte[1024];
int size;
while ((size=is.read(buffer)) !=-1){
outToServer.write(buffer,0,size);
}
outToServer.flush();
} catch (ConnectException connectException) {
Log.e(TAG, connectException.toString(), connectException);
return null;
} catch (UnknownHostException e1) {
Log.e(TAG, e1.toString(), e1);
return null;
} catch (IOException e1) {
Log.e(TAG, e1.toString(), e1);
return null;
} finally {
}
}
return null;}
}
When I send it to a regular printer. Says document is printing and cancels printing.

FileOutputStream prints weird latin characters at the end in java

when I try copy a java file to txt file ,It works fine but it prints some additional lating character at end of output file(Note: there is no such latin character in input file).
please refer the code
import java.io.*;
public class File1 {
public static void main(String[] args) throws IOException {
FileInputStream fin = null;
FileOutputStream fout = null;
int c;
String input = "F:\\Java Intellij\\src\\testprint.java";
String output = "F:\\Java Intellij\\src\\new23.txt";
File infile = new File(input);
File outfile = new File(output);
infile.createNewFile();
outfile.createNewFile();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
fin = new FileInputStream(infile);
fout = new FileOutputStream(outfile);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("enter :");
if (fin != null && fout != null)
try {
do {
c = fin.read();
fout.write(c);
} while (c != -1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fin != null) {
fin.close();
}
if (fout != null) {
fout.close();
}
}
}
}
can anyone shred some light on the question, why this happens??? and suggest a remedy

System.out.println displays blank line

Why does the below 'System.out.println'(fileEncodedString) display a blank line while the value is there when I debug and I do get an output when I decode the value and print.
byte[] fileContent = readFileToByteArray(filePath);
String fileEncodedString = Base64.getEncoder().encodeToString(fileContent);
byte[] fileDecodedString = Base64.getDecoder().decode(fileEncodedString);
System.out.println("Base64 Decoded File (Basic): " + new String(fileDecodedString, "utf-8"));
private static byte[] readFileToByteArray(String filePath) {
FileInputStream fileInputStream = null;
byte[] bytesArray = null;
try {
File file = new File(filePath);
bytesArray = new byte[(int) file.length()];
// read file into bytes[]
fileInputStream = new FileInputStream(file);
fileInputStream.read(bytesArray);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return bytesArray;
}
Try to use below method for readFileToByteArray and pass correct file path:
static byte[] readFileToByteArray(String filePath) throws IOException {
File file = new File(filePath);
return Files.readAllBytes(file.toPath());
}
its working fine.

Modified a model by Jena, how to reload this owl file to Protege

As shown in the title, I read owl file which is generated by Protege to Jena, modified it by adding some NamedIndividuals, and I wanted to read the modified file by Protege. Things went on well until I open this owl file with Protege. Protege just can not read it!
I tried every "RDF/XML", "RDF/XML - ABBREV", "N - TRIPLE", "TTL", and still nothing.
One good news is that when I use "RDF/XML - ABBREV" and delete all the NamedIndividual I added, Protege works.
But I want my Individuals!!!
public class Pizza00 {
public static void main(String[] args) throws IOException{
String SOURCE = "http://www.seaice.com/ontologies/seaice.owl";
String NS = SOURCE + "#";
OntModel m = ModelFactory.createOntologyModel();
try {
m.read(new FileInputStream("G:/Protege/owl files/SeaIce.owl"), null);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
reason rec = new reason();
rec = readFileByLines("G:/data/seaice_property.txt");
int i;
OntClass tmp = m.getOntClass(NS + "SeaIceProperty");
for(i = 1; i <= rec.line - 1; i ++){
m.createIndividual(NS + rec.s[i], tmp);
}
m.write(System.out);
OutputStream out = new FileOutputStream("G:/Protege/owl files/SeaIce - by jena.owl");
m.write(out, "RDF/XML-ABBREV", null);
out.close();
}
static class reason{
String[] s= new String[1000];
int line;
}
public static reason readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
reason x = new reason();
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
x.s[x.line] = tempString;
x.line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return x;
}
}
As you mention in the comments, the error is
this line <j.0:SeaIceProperty
rdf:about="seaice.com/ontologies/seaice.owl#Optical Band Imagery"/>
error occurs.
And the error by Protege is
org.semanticweb.owlapi.rdf.syntax.RDFParserException:
[line=30:column=101] IRI 'seaice.com/ontologies/seaice.owl#Optical
Band Imagery' cannot be resolved against curent base IRI
file:/G:/Protege/owl%20files/SeaIce%20-%20by%20jena.owl
The not-quite IRI seaice.com/ontologies/seaice.owl#Optical Band Imagery is relative, and can't be resolved against the current base IRI, which is a file IRI: file:/G:/Protege/owl%20files/SeaIce%20-%20by%20jena.owl. The easiest way to fix this is to use absolute IRIs when you are creating your named individuals.

Reading a file in Blackberry

I am trying to read content from file in blackberry and i read the file content to result object and when i am trying to print the result object it gives error and the file is in res folder,and this is code that i got problem
private String readTextFile(String fName) {
String result = null;
FileConnection fconn = null;
DataInputStream is = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
is = fconn.openDataInputStream();
byte[] data = IOUtilities.streamToBytes(is);
result = new String(data);
System.out.println("result data is ...... "+result);
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (null != is)
is.close();
if (null != fconn)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
return result;
}
what is fName here ?
The file name or the path could cause similar problem.

Resources