Results 1 to 5 of 5

Thread: IO help

  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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
    Java 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();
    }
    When I do something like this I get the following error
    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
    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)

    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
    Java Code:
    my name
    is
    bob, hello,
    world
    When I open it in a text editor(windows notepad) it looks like this
    Java Code:
    my nameisbob, 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)

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    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.
        }
    }

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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");
    			}
    		});

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,091
    Rep Power
    20

    Default

    Possibly if the file is still open somewhere?
    That (assuming it's not a permissions thing) is all I can think of offhand.

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Alright, I'll have to continue tinkering and seeing if I can figure it out, thanks for the help Tolls.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •