So I'm unexpectedly receiving a FileNotFoundException. As you can see, shortly before I call the FileReader, I call FileInputStream which works fine. I've tried putting the FileReader in its own Try/Catch clause, but receive the same result. I've gutted most of the lines unnecessary to my question from this block. (Ultimately I call LineNumberReader as well, though I removed it from the block because I'm not even getting that far.)
String FILENAME = "file.txt";
try {
byte[] buffer = new byte[128];
String toStr = new String();
TextView view = (TextView)findViewById(R.id.textview);
FileInputStream fis = openFileInput(FILENAME); /////File is found successfully here/////
fis.read(buffer);
fis.close();
toStr = new String(buffer);
view.append(toStr);
FileReader fr = new FileReader(FILENAME); /////FileNotFoundExceptionThrownHere/////
/////do stuff here/////
fr.close();
}
catch (FileNotFoundException e) {
TextView view = (TextView)findViewById(R.id.textview);
view.append("file not found!");
}
catch (IOException e) {
TextView view = (TextView)findViewById(R.id.textview);
view.append("IO error!");
}
Also, please keep in mind when answering that I'm still somewhat of a novice when it comes to java. I have experience in a couple other languages but java is a bit of a different breed of monster to me. Any help would be tremendously appreciated!
openFileInput() and new FileReader() do not take the same parameter.
openFileInput("file.txt") is equivalent to new FileReader(new File(getFilesDir(), "file.txt")).
Related
This question already has answers here:
Java multiple file transfer over socket
(3 answers)
Closed 5 years ago.
I have been spending forever on this and can not seem to work it out. I am self taught and not very familiar with this so forgive me if it is a remedial question. I am sending data from Android to .Net server. Data is getting corrupt on encoding, I know this I am just not sure how to fix. I am using the .Net Async server sample code found here: Microsoft Async Sample
My Android client code is:
try {
final Socket sock = new Socket();
final int timeOut = (int) TimeUnit.SECONDS.toMillis(5); // 5 sec wait period
sock.connect(new InetSocketAddress("localhost", 11000), timeOut);
if (sock.isConnected()==true){
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
String FileName = "myfile.jpg";
StringBuilder hd = new StringBuilder();
try {
String FilePath= Environment.getExternalStorageDirectory() + "/mydir/" + FileName;
File file = new File(FilePath);
FileInputStream is = new FileInputStream(file);
byte[] chunk = new byte[40960];
int chunkLen = 0;
while ((chunkLen = is.read(chunk)) != -1) {
//String str = new String(Base64.encodeToString(chunk, Base64.NO_WRAP));
//String str = new String(chunk, "ASCII");
String str = new String(chunk, "UTF-8");
out.write(str);
}
//out.write(hd.toString());
} catch (FileNotFoundException fnfE) {
// file not found, handle case
} catch (IOException ioE) {
// problem reading, handle case
}
out.write("<EOF>");
out.flush();
StringBuilder returnString = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
returnString.append(line).append('\n');
}
out.close();
in.close();
sock.close();
}else{
sock.close();
}
} catch (IOException e) {
e.printStackTrace();
}
As you can see in my comments, I have tried base64 and UTF-8. I get all kinds of errors on the server when I do that. If I use Base64 I get not part of Base64 error (extra padding etc.). UTF8 writes the file but it is corrupt. When I send it all as one Base64 string it works fine as I use 'Dim data As Byte() = Convert.FromBase64String(FileData)' but as expected it throws memory errors in Android for large files hence the chunking. I am sending some plain ASCII text along with it so I parse out the non-ASCII stuff to write the file. I am super stuck, any help would be much appreciated. Thanks in advance.
You don't have to encode it at all. Just write it directly as bytes using an OutputStream. Simpler, quicker, better.
I found the answer. It was so weird but makes sense now.
byte[] chunk = new byte[30000];
int chunkLen = 0;
while ((chunkLen = is.read(chunk)) != -1) {
String str = new String(Base64.encodeToString(chunk, Base64.NO_WRAP));
out.write(str);
}
I had to change the chunk size to a multiple of 3 then my base64 encoding worked great. Found it here and gave an up vote. Thank you 'mjv'. Link to the answer
I have a file stored in my phone "SaveTT.txt" . Every word in the file is seperated by spaces. i wish to retrieve each word from the file and display every word in seperate textViews. How to do this. please help
I am able to retrieve the contents of the file into a string with the following code
try {
BufferedReader inputReader = new BufferedReader(new InputStreamReader(
openFileInput("SAVETT.txt")));
StringBuffer stringBuffer = new StringBuffer();
while ((inputString = inputReader.readLine()) != null) {
stringBuffer.append(inputString + "\n");
//EditText txt = (EditText) findViewById(R.id.temp);
//txt.setText(inputString);
}
} catch (IOException e) {
e.printStackTrace();
}
with this code get the entire string into inputString
after this . i am tokenizing the string with the following code
StringTokenizer tokenizer = new StringTokenizer(inputString);
String[] arr = new String[tokenizer.countTokens()];
while(tokenizer.hasMoreElements())
{
arr[i]= tokenizer.nextToken();
i++;
}
with the above code i am trying to save eack token in an array. Next I try to display the text in Textviews.
I dont know where i am goig wrong. the activity is stopped and a NullPointer exception is displayed.
I Figured out the problem myself. The problem was that the value of inputString is inaccessible from outside the Try catch block since it is set inside the block;
so if i write the string tokenisation block inside try catch it works perfectly fine
i am thinking about there are some many way to store data in file , i found one of this is useing buffedinputstream ,but i really don't know is it good ??
if i using like this , it will be most fast ??
is there any other suggestion ?? i just want make the file io more fast !!
public ArrayList<String> testReadingTxtFromFile(){
ArrayList<String> result = null;
try {
FileInputStream fIn = openFileInput("cacheingtext.txt");
InputStreamReader isr = new InputStreamReader(fIn);
BufferedReader reader = new BufferedReader(isr);
String line;
while((line = reader.readLine() )!= null){
String[] datas = line.split(",");
Log.i("check", datas.length+"");
for(String data:datas){
Log.i("check", data);
result.add(data);
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public void testWritingTxtToFile(String[] messages){
try {
FileOutputStream fo = openFileOutput("cacheingtext.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fo);
BufferedWriter writer = new BufferedWriter(osw);
int size = messages.length;
for(int i=0;i<size;i++){
writer.write(messages[i]);
writer.write(",");
Log.i("check", "write "+messages[i]);
}
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The Reader/Writer class hierarchy is character-oriented, and the Input Stream/Output Stream class hierarchy is byte-oriented.
Basically there are two types of streams.Byte streams that are used to handle stream of bytes and character streams for handling streams of characters.
What I see in your case is that you are using a byte-oriented Stream.
Character streams are often "wrappers" for byte streams. The character stream uses the byte stream to perform the physical I/O, while the character stream handles translation between characters and bytes. FileReader, for example, uses FileInputStream, while FileWriter uses FileOutputStream.
So,if you want to generally deal with Characters (reading text files), go for Character-oriented Stream(Reader/Writer). But if you want to process the content independent of what type of file is it, go for byte-oriented stream.
I have an OpenGL game with two images, one is a texture atlas, and one is a single image. When I try to open the single one to an OpenGL object the application dies when closing it.
The code:
InputStream is = context.getResources().openRawResource(texture);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(is);
} finally {
try {
is.close();
is = null;
Log.i("log", "try");
} catch (IOException e) {
Log.i("log", "Catch");
}
}
It decodes the InputStream, but it steps into the catch after closing. I really can't understand what is the problem. Opening and closing InputStreams with the other texture works fine.
try the below code to print a stacktrace
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
ex.printStackTrace(printWriter);
String stacktrace = result.toString();
Log.i("log", "Catch" + stacktrace);
then hopefully you will have your answer :)
Reading a small file (138 kb) from internal phone storage is very slow. It takes about 490 ms. Writing the same file is done in only 70 ms. (measured in emulator)
Any speedup suggestions?
Thanks, Alexander Miehlke, Berlin
private String readFromInternal(String filename) {
try {
FileInputStream fileInputStream = openFileInput(filename);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null)
stringBuilder.append(line);
return stringBuilder.toString();
} catch (Exception e) {
return null;
}
}
private boolean writeToInternal(String filename, String text) {
try {
FileOutputStream fileOutputStream = openFileOutput(filename, Context.MODE_PRIVATE);
fileOutputStream.write(text.getBytes());
fileOutputStream.close();
return true;
} catch (Exception e) {
return false;
}
}
I wouldn't benchmark anything on an emulator. You might speed things up by making sure your BufferedReader has a relatively large buffer. Say 4k-ish. I did general Java tests years ago for optimal buffer size, and above 1-2k, it didn't seem to help much.
Try this on a phone.
Why reading is slower than writing is beyond me, but again, emulator is not where you want to benchmark.
You might try reversing the buffer location. Put the buffer around the stream rather than the reader:
new BufferedReader(new InputStreamReader(new BufferedInputStream(fileInputStream, 2048)))
I assume you used a BufferedReader to be able to read by line. Creating 2 bufferes is probably not great, so if you have a way to read lines that doesn't require the BufferedReader, you're probably better off.
Your problem is that you are reading line by line, but writing in one go. Try reading the file into a buffer in one go instead of the while loop.