简体   繁体   中英

write to file - which way is cleaner?

I am using java.io.PrintWriter to write some text to a text file. I was wondering if it was better to build in a variable all what I need to write and give only once

PrintWriter out = new PrintWriter(outputfile);
out.printf("%s", myvariablewithalltext);

or if I can call n times PrintWriter to write block of text in a for loop.

It works in either way and there is no much more code, I was just wondering which is better.

In most cases it's better to write in stream. The main reason is that your variable might take too much memory, but stream will automatically flush it's content. Writing text into the variable is essentially manual buffering. And better way to do it is to use appropriate buffering stream/writer. In you case you can just use java.io.BufferedWriter . Like so

BufferedWriter out = new BufferedWriter(new PrintWriter("file.txt"));

or, if you prefer PrintWriter interface, you can do this

PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("file.txt")));

Assuming you are open for other suggestions (not just the two you mentioned in question).

If all you want is a clean way of writing text to a file, which of course has multiple solutions, here are few ways:

  1. Using PrintWriter .

    example:

     String contentToWrite = "This is some random Text"; PrintWriter writerToFile = new PrintWriter("TheOutputFile.txt"); writerToFile.print(contentToWrite); writerToFile.close(); 
  2. Using FileOutputStream

    example:

    String contentToWrite = "This is some random Text"; FileOutputSream fileOPS = new FileOutputStream("TheOutputFile.txt"); fileOPS.write(contentToWrite.getBytes()); fileOPS.close();

  3. Using Files

  4. Using FileWriter along with BufferWriter
  5. Using FileUtils by apache.commons.io
  6. Using Files by guava

Some approaches here just take the content (no parsing or conversion required ie in string format) and write that to a file. [ no parsing/conversion -> less code -> cleaner code ]. ;)

Some do not require you to make nesting of objects. [ less objects -> less code -> cleaner code ]. ;)

Of course usage depends on your implementation. but I hope this will help you in making decision what would best suit your requirement.

Note: every class name I mentioned is a link to its reference document.

It is the latter. There is no good reason whatsoever to put the entire content into a variable, just to write it in a file. If you have some additional use for that variable beyond writing to file, that might change things a little bit, but even then, there is, probably, a better way.

I think it depends on your content lenght.

If you have just some litle text, it's better to keep all in memory and write in one shot.

But if your content is very large or if some part take long time to computed, probably you should write piece by piece to avoid have huge data kept in memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM