Java: PrintWriter 未写入文件
Java: PrintWriter not writing in file
我 运行 解决了 PrintWriter
的一些问题。我写了一些代码,简单地从一个文件中获取一些输入并将其输出到另一个文件。
虽然创建了一个文件,但该文件仍然是空的。所需的输入可以很容易地在控制台中打印出来,这意味着 FileInputStream 工作正常。
为什么 PrintWriter 不打印任何东西?
public static void writeInFile(File in, File out) throws FileNotFoundException {
PrintWriter outputStream = null
Scanner scanner = new Scanner(new FileInputStream(in));
outputStream = new PrintWriter(new FileOutputStream(out));
outputStream.print("test");
while(scanner.hasNext()) {
outputStream.print(scanner.nextLine() + "\n");
}
scanner.close();
}
确保您始终关闭 OutputStreams
:
while(scanner.hasNext()) {
String s = scanner.nextLine();
outputStream.print(s+"\n");
System.out.println("Test "+s); //d.h. das Problem liegt am outputstream
}
outputStream.close();
scanner.close();
编辑:当您 close
outputStream 时,它会自动调用 flush
,将缓冲区写入文件。如果不关闭它,缓冲区可能永远不会 emptied/written 到文件,就像这里的情况一样。
另见 this answer。
在处理需要清理的IO时,我更喜欢使用自动资源清理。这就是您在最基本级别上所需要的全部内容:
public static void writeInToOut(InputStream in, OutputStream out) {
try(PrintWriter outputStream = new PrintWriter(out);
Scanner scanner = new Scanner(in)) {
while(scanner.hasNext()) {
outputStream.print(scanner.nextLine()+"\n");
}
}
}
您现在可以通过多种方式重载此函数:
public static void writeInToOut(File file, OutputStream out) {
try (InputStream in = new FileInputStream(file)) {
writeInToOut(in, out);
} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "IOError", e);
}
}
public static void writeInToOut(File inFile, File outFile) {
try (InputStream in = new FileInputStream(inFile);
OutputStream out = new FileOutputStream(outFile)) {
writeInToOut(in, out);
} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "IOError", e);
}
}
public static void writeStdInToFile(File file) {
try (OutputStream out = new FileOutputStream(file)) {
writeInToOut(System.in, out);
} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "IOError", e);
}
}
我 运行 解决了 PrintWriter
的一些问题。我写了一些代码,简单地从一个文件中获取一些输入并将其输出到另一个文件。
虽然创建了一个文件,但该文件仍然是空的。所需的输入可以很容易地在控制台中打印出来,这意味着 FileInputStream 工作正常。
为什么 PrintWriter 不打印任何东西?
public static void writeInFile(File in, File out) throws FileNotFoundException {
PrintWriter outputStream = null
Scanner scanner = new Scanner(new FileInputStream(in));
outputStream = new PrintWriter(new FileOutputStream(out));
outputStream.print("test");
while(scanner.hasNext()) {
outputStream.print(scanner.nextLine() + "\n");
}
scanner.close();
}
确保您始终关闭 OutputStreams
:
while(scanner.hasNext()) {
String s = scanner.nextLine();
outputStream.print(s+"\n");
System.out.println("Test "+s); //d.h. das Problem liegt am outputstream
}
outputStream.close();
scanner.close();
编辑:当您 close
outputStream 时,它会自动调用 flush
,将缓冲区写入文件。如果不关闭它,缓冲区可能永远不会 emptied/written 到文件,就像这里的情况一样。
另见 this answer。
在处理需要清理的IO时,我更喜欢使用自动资源清理。这就是您在最基本级别上所需要的全部内容:
public static void writeInToOut(InputStream in, OutputStream out) {
try(PrintWriter outputStream = new PrintWriter(out);
Scanner scanner = new Scanner(in)) {
while(scanner.hasNext()) {
outputStream.print(scanner.nextLine()+"\n");
}
}
}
您现在可以通过多种方式重载此函数:
public static void writeInToOut(File file, OutputStream out) {
try (InputStream in = new FileInputStream(file)) {
writeInToOut(in, out);
} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "IOError", e);
}
}
public static void writeInToOut(File inFile, File outFile) {
try (InputStream in = new FileInputStream(inFile);
OutputStream out = new FileOutputStream(outFile)) {
writeInToOut(in, out);
} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "IOError", e);
}
}
public static void writeStdInToFile(File file) {
try (OutputStream out = new FileOutputStream(file)) {
writeInToOut(System.in, out);
} catch (IOException e) {
Logger.getAnonymousLogger().log(Level.WARNING, "IOError", e);
}
}