如何创建和输出到 Java 中的文件
How to create and output to files in Java
我目前的问题在于,无论我尝试使用何种解决方案在 Java 中创建文件,该文件都永远不会创建或显示。
我在 Whosebug 上搜索了解决方案并尝试了很多很多不同的代码片段,但都无济于事。我试过使用 BufferedWriter、PrintWriter、FileWriter,包裹在 try and catch 中并抛出 IOExceptions,其中 none 似乎有效。对于需要路径的每个字段,我都尝试了单独的文件名和路径中的文件名。没有任何效果。
//I've tried so much I don't know what to show. Here is what remains in my method:
FileWriter fw = new FileWriter("testFile.txt", false);
PrintWriter output = new PrintWriter(fw);
fw.write("Hello");
每当我 运行 我过去的代码时,我都不会抛出任何错误,但是,这些文件从未真正出现过。我怎样才能解决这个问题?
提前致谢!
一些值得尝试的事情:
1) 如果您还没有(它不在您显示的代码中),请确保在完成后关闭文件
2) 使用文件而不是字符串。这将让您仔细检查文件的创建位置
File file = new File("testFile.txt");
System.out.println("I am creating the file at '" + file.getAbsolutePath() + "');
FileWriter fw = new FileWriter(file, false);
fw.write("Hello");
fw.close();
作为奖励,Java的try-with-resource会在完成后自动关闭资源,您可能想试试
File file = new File("testFile.txt");
System.out.println("I am creating the file at '" + file.getAbsolutePath() + "');
try (FileWriter fw = new FileWriter(file, false)) {
fw.write("Hello");
}
有几种方法可以做到这一点:
使用 BufferedWriter 写入:
public void writeWithBufferedWriter()
throws IOException {
String str = "Hello";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(str);
writer.close();
}
如果要附加到文件:
public void appendUsingBufferedWritter()
throws IOException {
String str = "World";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true));
writer.append(' ');
writer.append(str);
writer.close();
}
使用 PrintWriter:
public void usingPrintWriteru()
throws IOException {
FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.print("Some String");
printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
printWriter.close();
}
使用 FileOutputStream:
public void usingFileOutputStream()
throws IOException {
String str = "Hello";
FileOutputStream outputStream = new FileOutputStream(fileName);
byte[] strToBytes = str.getBytes();
outputStream.write(strToBytes);
outputStream.close();
}
注:
- 如果您尝试写入一个不存在的文件,将首先创建该文件并且不会抛出异常。
- 使用流后关闭它非常重要,因为它不是隐式关闭的,以释放与其关联的任何资源。
- 在输出流中,close() 方法在释放资源之前调用 flush(),这会强制将任何缓冲字节写入流。
来源和更多示例:https://www.baeldung.com/java-write-to-file
希望这对您有所帮助。祝你好运。
我目前的问题在于,无论我尝试使用何种解决方案在 Java 中创建文件,该文件都永远不会创建或显示。
我在 Whosebug 上搜索了解决方案并尝试了很多很多不同的代码片段,但都无济于事。我试过使用 BufferedWriter、PrintWriter、FileWriter,包裹在 try and catch 中并抛出 IOExceptions,其中 none 似乎有效。对于需要路径的每个字段,我都尝试了单独的文件名和路径中的文件名。没有任何效果。
//I've tried so much I don't know what to show. Here is what remains in my method:
FileWriter fw = new FileWriter("testFile.txt", false);
PrintWriter output = new PrintWriter(fw);
fw.write("Hello");
每当我 运行 我过去的代码时,我都不会抛出任何错误,但是,这些文件从未真正出现过。我怎样才能解决这个问题? 提前致谢!
一些值得尝试的事情:
1) 如果您还没有(它不在您显示的代码中),请确保在完成后关闭文件
2) 使用文件而不是字符串。这将让您仔细检查文件的创建位置
File file = new File("testFile.txt");
System.out.println("I am creating the file at '" + file.getAbsolutePath() + "');
FileWriter fw = new FileWriter(file, false);
fw.write("Hello");
fw.close();
作为奖励,Java的try-with-resource会在完成后自动关闭资源,您可能想试试
File file = new File("testFile.txt");
System.out.println("I am creating the file at '" + file.getAbsolutePath() + "');
try (FileWriter fw = new FileWriter(file, false)) {
fw.write("Hello");
}
有几种方法可以做到这一点:
使用 BufferedWriter 写入:
public void writeWithBufferedWriter()
throws IOException {
String str = "Hello";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
writer.write(str);
writer.close();
}
如果要附加到文件:
public void appendUsingBufferedWritter()
throws IOException {
String str = "World";
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName, true));
writer.append(' ');
writer.append(str);
writer.close();
}
使用 PrintWriter:
public void usingPrintWriteru()
throws IOException {
FileWriter fileWriter = new FileWriter(fileName);
PrintWriter printWriter = new PrintWriter(fileWriter);
printWriter.print("Some String");
printWriter.printf("Product name is %s and its price is %d $", "iPhone", 1000);
printWriter.close();
}
使用 FileOutputStream:
public void usingFileOutputStream()
throws IOException {
String str = "Hello";
FileOutputStream outputStream = new FileOutputStream(fileName);
byte[] strToBytes = str.getBytes();
outputStream.write(strToBytes);
outputStream.close();
}
注:
- 如果您尝试写入一个不存在的文件,将首先创建该文件并且不会抛出异常。
- 使用流后关闭它非常重要,因为它不是隐式关闭的,以释放与其关联的任何资源。
- 在输出流中,close() 方法在释放资源之前调用 flush(),这会强制将任何缓冲字节写入流。
来源和更多示例:https://www.baeldung.com/java-write-to-file
希望这对您有所帮助。祝你好运。