哪个是用Java创建文件并写入它的最佳方法

我通常使用PrintWritter对象来创建和写入文件,但不确定它在速度和安全性方面是否与使用其他方法创建和写入文件的其他方式相比是最佳的

 Writer writer = new BufferedWriter( new OutputStreamWriter( new FileOutputStream("example.html"), "utf-8")); writer.write("Something"); 

vs

 File file = new File("example.html"); BufferedWriter output = new BufferedWriter(new FileWriter(file)); output.write("Something"); 

vs

 File file = new File("example.html"); FileOutputStream is = new FileOutputStream(file); OutputStreamWriter osw = new OutputStreamWriter(is); Writer w = new BufferedWriter(osw); w.write("something"); 

vs

 PrintWritter pw = new PrintWriter("example.html", "UTF-8"); pw.write("Something"); 

另外,何时使用一个而不是另一个; 一个用例场景将不胜感激。 我不是要求如何创建和写入文件,我知道如何做到这一点。 它更多的是比较和对比的问题,我问。

我更喜欢:

 boolean append = true; boolean autoFlush = true; String charset = "UTF-8"; String filePath = "C:/foo.txt"; File file = new File(filePath); if(!file.exists()) file.mkdirs(); FileOutputStream fos = new FileOutputStream(file, append); OutputStreamWriter osw = new OutputStreamWriter(fos, charset); BufferedWriter bw = new BufferedWriter(osw); PrintWriter pw = new PrintWriter(bw, autoFlush); pw.write("Some File Contents"); 

这给你:

  1. 决定是附加到文本文件还是覆盖它。
  2. 决定是否自动刷新。
  3. 指定字符集。
  4. 使其缓冲,从而提高流媒体性能。
  5. 方便的方法(例如println()及其重载方法)。

老实说,我不知道这是否是最好的选择,但我认为这是一个类似于公共接受的抽象系统:

In.java:

 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; public class In { public In() { } /** * Tranforms the content of a file (ie: "origin.txt") in a String * * @param fileName: Name of the file to read * @return String which represents the content of the file */ public String readFile(String fileName) { FileReader fr = null; try { fr = new FileReader(fileName); } catch (FileNotFoundException e1) { e1.printStackTrace(); } BufferedReader bf = new BufferedReader(fr); String file = "", aux = ""; try { while ((file = bf.readLine()) != null) { aux = aux + file + "\n"; } bf.close(); } catch (IOException e) { e.printStackTrace(); } return aux; } } 

Out.java:

 import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class Out { public Out() {} /** * Writes on the file named as the first parameter the String which gets as second parameter. * * @param fileName: Destiny file * @param message: String to write on the destiny file */ public void writeStringOnFile(String fileName, String message) { FileWriter w = null; try { w = new FileWriter(fileName); } catch (IOException e) { e.printStackTrace(); } BufferedWriter bw = new BufferedWriter(w); PrintWriter wr = new PrintWriter(bw); try { wr.write(message); wr.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } } 

IO.java:

 public class IO { public Out out; public In in; /** * Object which provides an equivalent use to Java 'System' class */ public IO() { this.setIn(new In()); this.setOut(new Out()); } public void setIn(In in) { this.in = in; } public In getIn() { return this.in; } public void setOut(Out out) { this.out = out; } public Out getOut() { return this.out; } } 

因此,您几乎可以像使用System一样使用类IO

希望能帮助到你。

克莱门西奥莫拉莱斯卢卡斯。

像往常一样,最佳解决方案取决于您的任务:使用文本或使用二进制数据。 在第一种情况下,你应该更喜欢写作者,在第二种情况下。

您要求比较将TEXT写入文件的方法。

您问题中的最后一个案例是编写/创建新文本文件的最佳案例。 它适用于99%的实际任务。

 PrintWritter pw = new PrintWriter("example.html", "UTF-8"); pw.write("Something"); 

它简短易读。 它支持显式编码和缓冲。 它不需要不必要的包装器。

在这种解决方案不适用的情况下,可以而且应该使用其他旧方法。 例如,您要将任何文本附加到文件。 在这种情况下,除了使用基于流包装器的编写器之外别无选择(遗憾的是,FileWriter不支持显式编码)。