-2

I am writing a file which is very important for the application (Client & vendor specific). I would like to know what are the point should i consider at writting the file. Please see below a demo file writing process

public class EDIWriter {
    public final static String END_OF_ATTRIBUTE = "|";
    File file;
    BufferedWriter writer;
    public void writer(){
    try{
    file = new File("C://edifile.edi");
    writer = new BufferedWriter(new FileWriter(file));

    StringBuilder editext = new StringBuilder();
    editext.append("ISA");
    editext.append(END_OF_ATTRIBUTE);
    editext.append("00");
    editext.append(END_OF_ATTRIBUTE);

    writer.write(editext.toString());
    writer.flush();
    writer.close();
        }catch(IOException e){

            e.printStackTrace();

        }catch(Exception e){

            e.printStackTrace();
        }
    }

    public static void main(String arg[]){
        new EDIWriter().writer();
    }
}
user1010399
  • 2,258
  • 5
  • 30
  • 42

5 Answers5

1

One point is to be sure to close the file, even if an exception occurred. So I would suggest to add a finally block:

finally {
 try {
    if (writer != null) {
      writer.close();
    }
 } catch (IOException ioexception) {
    //log exception
 }

Another point I just remembered: As your method is public, it could be called twice (from different callers). Synchronization may help to handle this issue.

Stefan Freitag
  • 3,578
  • 3
  • 26
  • 33
  • 1
    If the end result is important, the exception thrown by close() shouldn't just be logged. It should bubble up the stack until the user knows that closing the stream failed, which probably means that the file doesn't contain everything written to it. If Java 7 or later is used, the try-with-resources statement is a better choice (which avoids this pitfall). – JB Nizet Nov 16 '13 at 10:16
  • thanks Stefan, one more point i wanted to know, as i am using `StringBuilder` to append the string value from different java bean class. is right to use the `StringBuilder`? or should i consider `StringBuffer`, What you think? – user1010399 Nov 16 '13 at 12:05
  • 1
    No. Use StringBuilder. StringBuffer should be deprecated: StringBuilder is more recent and replaces it. You can also write directly to the writer, which would avoid the unnecessary temporary String concatenation. – JB Nizet Nov 16 '13 at 12:07
1

Yes, it is necessary to close the resources(which are all opened to parse/write) while handling the file operations. Else, you will end up with out of memory related issues.

Sabari
  • 73
  • 5
0

maybe you should backup your original file before edit, then check the file for correctness after edit, if it's broken, restore from the backup file.

just a suggestion.

vivisidea
  • 445
  • 5
  • 6
0

The most important point to consider is: Is my program working correctly and as specified? Does it pass my tests and meet all of my requirements? If not, then come here when you have a specific problem you are trying to solve. If so, then move on to the next task.

Jason C
  • 38,729
  • 14
  • 126
  • 182
-2

First,the file must exists. if(!file.exists()){ file.createNewFile(); }

Invalid
  • 66
  • 1
  • 5