Results 1 to 5 of 5
Thread: IO help
- 03-15-2011, 07:00 AM #1
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
IO help
I am working on coding up a notepad type app and I am still on the basics of the program. I am working on saving and there are a lot of streams to use, and I am wondering which is the best. Is it better to chain a buffered stream and a file stream? Or is using simply a FileWriter fine?
I am also having some trouble with a finally clause. In the java tutorials I see them saying to close the stream in a finally clause, but when I try this I get an error
When I do something like this I get the following errorJava Code:FileWriter out = null; String s = input.getText(); //this is a text area int ch; try{ if(file != null){ out = new FileWriter(file); } for(char c : s.toCharArray()){ ch = (int)c; out.write(ch); } out.flush(); } catch(IOException ioe){ ioe.printStackTrace(); } finally { out.close(); }
Is this a good way to be writing ascii text to a file? Or is there a better way?(I don't really want to use NIO yet)Java Code:C:\Javacode\Notepad>javac notepad.java notepad.java:64: unreported exception java.io.IOException; must be caught or declared to be thrown out.close(); ^ 1 error
Another question I have, I am able to get it to save to a file but I can't figure out how to get it keep formatting?
I saved something like
When I open it in a text editor(windows notepad) it looks like thisJava Code:my name is bob, hello, world
Thanks in advance guys, I am trying my best to avoid asking questions so I learn this better I just want to make sure I am not falling into bad io habits(not closing streams, inefficient streams)Java Code:my nameisbob, hello,world
- 03-15-2011, 09:05 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
FileWriter should be OK.
Why are you writing individual characters?
Why not write the whole String in one go?
Also, you don't need to flush(). Since you are close()ing right after the flush, and close() calls flush, there's no need to do it separately in this instance.
As for the compiliation error. Wrap the close() call in a try/catch as well and simply log the problem should it occur.
Java Code:try { // do stuff in here } catch (IOException ex) { // log, throw, whatever } finally { try { // close the stream } catch (IOException ex) { // Log it. } }
- 03-15-2011, 09:09 AM #3
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Awesome thanks, the extent of my IO at this point is mostly using object streams for serialization. I read a little ahead in the tutorials and I am now using PrintWriter and BufferedReader.
I also just call close() on it after the loop to read or write all the information.
I am however; running into one other small problem, I have implemented save, save as, new, and open successfully, now I am working on deleting the files.
What causes a file to not be able to be deleted when delete() is called?
I tested the file with file.isFile() and it returns true, but the call to file.delete() returns false.
to help, here is the listener for deleting
Java Code:delete.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ if(theFile != null){ int theChoice = JOptionPane.showConfirmDialog( theFrame, "Are you sure you want to delete this file permanently?", "Delete?", JOptionPane.WARNING_MESSAGE, JOptionPane.YES_NO_OPTION ); if(theChoice == JOptionPane.YES_OPTION){ System.out.println(theFile.isFile()); System.out.println(theFile); System.out.println("Deleting"); System.out.println(theFile.delete()); } } theInputText.setText(""); theFile = null; theFrame.setTitle("My Notepad"); } });
- 03-15-2011, 09:25 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Possibly if the file is still open somewhere?
That (assuming it's not a permissions thing) is all I can think of offhand.
- 03-15-2011, 09:31 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks