Java BufferedWriter 不写入文件
Java BufferedWriter doesn't write to file
我想将一些字符串写入文件(使用 Java 8)。此代码片段编译没有任何错误,但没有写入文件....我是 noob ofcorz...有什么建议吗?
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
private static final String FILENAME = "C:\IntelliJfiles\write-to-
file\src\pl\sca\file.txt";
public static void main(String[] args) {
try {
String str = "kal";
BufferedWriter writer = new BufferedWriter(new
FileWriter(FILENAME));
writer.write(str);
System.out.println("Done");
}
catch (Exception e) {
System.out.println("There's no file");
e.printStackTrace();
}
}
}
在您关闭文件之前,它可能不会写入文件 - 写入磁盘非常耗时,因此它会等到写入足够的字符后再将它们实际放入文件中。这就是 BufferedWriter
中的 Buffered 的意思。如果关闭它,它将 "flush" 缓冲区放到磁盘上。
您可以使用 writer.close()
自行关闭它
或者你可以使用try-with-resources,它会自动关闭编写器:
try (FileWriter fw = new FileWriter("C:\IntelliJfiles\write-to-file\src\pl\sca\file.txt");
BufferedWriter bw = new BufferedWriter(fw)){
bw.write("kal");
} catch (Exception e) {
e.printStackTrace();
}
需要关闭编写器,可以使用try-with-resources:
String str = "kal";
// we can use try-with-resources from java 7
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILENAME))) {
writer.write(str);
}
System.out.println("Done");
或者直接添加writer.close()
:
writer.write(str);
writer.close();
如果您的目标是只写入一次文件,BufferWriter
的替代方法是 Files.write(Path, byte[], OpenOption...)。
String str = "test";
Path path = Paths.get("file.txt");
try {
byte[] bytes = str.getBytes(StandardsCharsets.UTF_8);
Files.write(path, bytes);
} catch (FileNotFoundException e) {
// Handle file absence.
} catch (IOException e) {
// Handle other exceptions related to io.
}
注意:如前所述, 捕获 Exception
并不表示在所有情况下都缺少文件。如果您想处理文件缺失或 IOException
与 io.
相关的所有异常情况,请捕获 FileNotFoundException
试试这个
try {
String str = "kal";
BufferedWriter writer = new BufferedWriter(new
FileWriter(FILENAME));
writer.write(str);
writer.close();
System.out.println("Done");
}
我想将一些字符串写入文件(使用 Java 8)。此代码片段编译没有任何错误,但没有写入文件....我是 noob ofcorz...有什么建议吗?
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
private static final String FILENAME = "C:\IntelliJfiles\write-to-
file\src\pl\sca\file.txt";
public static void main(String[] args) {
try {
String str = "kal";
BufferedWriter writer = new BufferedWriter(new
FileWriter(FILENAME));
writer.write(str);
System.out.println("Done");
}
catch (Exception e) {
System.out.println("There's no file");
e.printStackTrace();
}
}
}
在您关闭文件之前,它可能不会写入文件 - 写入磁盘非常耗时,因此它会等到写入足够的字符后再将它们实际放入文件中。这就是 BufferedWriter
中的 Buffered 的意思。如果关闭它,它将 "flush" 缓冲区放到磁盘上。
您可以使用 writer.close()
或者你可以使用try-with-resources,它会自动关闭编写器:
try (FileWriter fw = new FileWriter("C:\IntelliJfiles\write-to-file\src\pl\sca\file.txt");
BufferedWriter bw = new BufferedWriter(fw)){
bw.write("kal");
} catch (Exception e) {
e.printStackTrace();
}
需要关闭编写器,可以使用try-with-resources:
String str = "kal";
// we can use try-with-resources from java 7
try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILENAME))) {
writer.write(str);
}
System.out.println("Done");
或者直接添加writer.close()
:
writer.write(str);
writer.close();
如果您的目标是只写入一次文件,BufferWriter
的替代方法是 Files.write(Path, byte[], OpenOption...)。
String str = "test";
Path path = Paths.get("file.txt");
try {
byte[] bytes = str.getBytes(StandardsCharsets.UTF_8);
Files.write(path, bytes);
} catch (FileNotFoundException e) {
// Handle file absence.
} catch (IOException e) {
// Handle other exceptions related to io.
}
注意:如前所述,Exception
并不表示在所有情况下都缺少文件。如果您想处理文件缺失或 IOException
与 io.
FileNotFoundException
试试这个
try {
String str = "kal";
BufferedWriter writer = new BufferedWriter(new
FileWriter(FILENAME));
writer.write(str);
writer.close();
System.out.println("Done");
}