Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

File reading  RSS feed

 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got a quick question. Im writing a string of text to a text file with this code:
// s is for the string
//field1.getText() is the name of the file to be opened
FileOutputStream fos = new FileOutputStream(field1.getText());
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s);
field2.setText("File has been Written!!");
oos.close();


but when i try to open the file again it doesent seem to work, and just keeps giving me null. What im asking i guess is the code on how to do this because my code just does not work.
thank you
Chris Dancy
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assuming that the outgoing String object is non-null, your problem may be that the outgoing data is cached somewhere between your output stream and your file and you close the file before it is written. Add a oos.flush() call before oos.close() and see if that fixes it.
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok that didnt work. The whole point is saving the text in a text area to a .txt file. The previous code example saves it to the file, and it writes it in byte code. But i just cant seem to get it back to the textarea if i want to. this is the code im using to open the file

BufferedReader b;
try{
b = new BufferedReader(new FileReader (field1.getText()));
String ln;
while((ln = b.readLine())!= null)
System.out.print(b.ready());
new FrameSetUp().area1.append(String.format("%s + its working",ln));

}catch(Exception e)
{
System.out.println(e);
}
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This code gets the name of the file to be opened from the text field and tries to open it. Am I doing something wrong?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Chris Dancy:
Am I doing something wrong?


Yes indeed. The ObjectOutputStream class is used for saving random Java objects to a file, to be later read in and turned back into objects. The only way to make sense out of a file that's been written with ObjectOutputStream is to read it with ObjectInputStream. If you look at such a file in an editor, it will contain binary gibberish. This is not what you want!

Using a FileWriter and a PrintWriter to write your String to a file; this will produce plain text. You'll then be able to read the data out of the file properly.

PrintWriter pw = new FileWriter(fileName);
pw.println(theString);
pw.close();

Unfortunately, your file-reading code is broken and will always return null! That's because when you read the end-of-file and readLine() returns null, this will be assigned to your variable. You just want to do something like

BufferedReader reader = new BufferedReader(new FileReader(fileName));
String line = reader.readLine();
reader.close();

After this code, line will be null if and only if the file was empty.

You would greatly benefit from reading this tutorial on Java I/O.
 
Joe Ess
Bartender
Posts: 9406
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't read a file written with an ObjectOutputStream with anything but an ObjectInputStream and hope that it will make any sense. It does not matter what the file extension is, if you use an ObjectOutputStream to write it, it will be a serialized object, not plain text. Either write and read Objects or write and read text using Readers.
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you much indeed.
Thats all i needed to hear.
Sincerely,
Chris Dancy
 
Chris Dancy
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now a new problem has occured. Let me just say that im taking the text from a textarea, so its more than one line of text. and im writing to a file like this:
try{
BufferedWriter b = new BufferedWriter(new FileWriter(field1.getText()));
b.write(s);
System.out.println(b);
field2.setText("File has been Written!!");
}catch(Exception e)
{
System.out.println(e);
}

Then to print the text back on another text area im using this code now:
BufferedReader b;
try{
BufferedReader r = new BufferedReader(new FileReader(field1.getText()));
String ln;
while((ln = r.readLine()) != null)
{
System.out.println(r);
new FrameSetUp().area2.append(String.format("%s + its working",ln));
}

}catch(EOFException e)
{
System.out.println(e);
}
but all im getting is an EOFexception everytime i try to open the file? i cant figure out whats wrong.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!