Java I / O, writing an array to a text file

I use the following code to write an array to a file:

FileWriter fstream1=new FileWriter("outx.txt");
BufferedWriter out1= new BufferedWriter(fstream1);
FileWriter fstream2=new FileWriter("outy.txt");
BufferedWriter out2= new BufferedWriter(fstream2);
for(int i=0;i<320*240;i++)
{

        out1.write(0+System.getProperty("line.separator"));//
       // out1.write("\n");
        out2.write(0+System.getProperty("line.separator"));
//out2.write("\n");

}

: here, in the above code, I put all the zeros of the file should contain 76800 lines (0 s), but my file has only 69932 lines. what is the problem and if you can suggest another way to do it.

+3
source share
3 answers

As others have noted, it is likely that your buffers have unrelated data.

A valid way to rewrite the code is as follows:

Writer out1 = new FileWriter("outx.txt");
try {
  out1 = new BufferedWriter(out1);
  Writer out2 = new FileWriter("outy.txt");
  try {
    out2 = new BufferedWriter(out2);
    for (int i = 0; i < 320 * 240; i++) {
      out1.write(0 + System.getProperty("line.separator"));
      out2.write(0 + System.getProperty("line.separator"));
    }
  } finally {
    out2.close();
  }
} finally {
  out1.close();
}

This code:

  • will clear data using close
  • will always release files using close, even if an error occurs (using finally)
  • obeys contract for classes Closeable
+1

? close(), . BufferedWriter , () .

, :

out1.close();

out2.close();

, , , , BufferedOutputStream BufferedWriter, ( ) , (, , ).

, close() , . , :

Writer myOutWriter = null;
try {
   myOutWriter = new BufferedWriter(new FileWriter("..."));
   // Write to myOutWriter here
} catch (IOException ioe) {
   // Handle any exceptions here
} finally { 
   try {
      if (myOutWriter != null) {
         myOutWriter.close();
      }
   } catch (IOException ioe) {
      // Not much you can do here
   }
}

IO Apache Commons (http://commons.apache.org/io/) IOUtils.closeQuietly(), finally try catch, null check call, . :

Writer myOutWriter = null;
try {
   myOutWriter = new BufferedWriter(new FileWriter("..."));
   // Write to myOutWriter here
} catch (IOException ioe) {
   // Handle any exceptions here
} finally { 
   IOUtils.closeQuietly(myOutWriter);
}
+5

out1.flush();
out2.flush();

for.

, BufferedReader, .

: :

public static void main(String[] args) throws IOException {
    final String outputString = "0" + System.getProperty("line.separator");
    BufferedWriter out1 = null;
    BufferedWriter out2 = null;
    try {
        out1 = new BufferedWriter(new FileWriter("outx.txt"));
        out2 = new BufferedWriter(new FileWriter("outy.txt"));
        for(int i = 0; i < 320 * 240; i++) {
            out1.write(outputString);
            out2.write(outputString);
        }
        out1.flush(); // Not really needed as close will flush, but it is 
        out2.flush(); // useful for describing the intent of the code
    } finally {
        closeQuietly(out1);
        closeQuietly(out2);
    }
}

private static void closeQuietly(Closeable c) {
    try {
        if (c != null) {
            c.close();
        }
    } catch (Exception e) {
        // No-op
    }
}
+4

Source: https://habr.com/ru/post/1758521/


All Articles