Linux和java:正在创建文件,但未写入文本
我正在编写一个简单的终端程序,它记录一些信息,并将其放入一个文本文件中,以后有人可以回忆起来。主要是为了记录一个人所做的事情。我在windows上一直很好,并没有遇到过这个问题,但我担心我在看一些简单的东西 如前所述,如果导航到项目目录,我会看到文件已创建,但当我使用文本编辑器打开文件时,创建的字符串中的任何数据都不会打印Linux和java:正在创建文件,但未写入文本,java,linux,file,Java,Linux,File,我正在编写一个简单的终端程序,它记录一些信息,并将其放入一个文本文件中,以后有人可以回忆起来。主要是为了记录一个人所做的事情。我在windows上一直很好,并没有遇到过这个问题,但我担心我在看一些简单的东西 如前所述,如果导航到项目目录,我会看到文件已创建,但当我使用文本编辑器打开文件时,创建的字符串中的任何数据都不会打印 private static void writeFile( String call,float freq,String mode,int rstSnt,int rstRx,
private static void writeFile( String call,float freq,String mode,int rstSnt,int rstRx,String city, String state) throws IOException{
File fileName = new File("log.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(fileName);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));
int i =0;
write.write(i +"; " + call + "; " + freq + "; " + mode + "; " + rstSnt + "; " + rstRx + "; " + city + "," + state + "\n");
i++;
System.out.println("File has been updated!");
} catch (FileNotFoundException ex) {
Logger.getLogger(QtLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
从
BufferedWriter
类调用write()
函数后,需要调用close()
函数。您还应该在FileOutputStream
对象上调用close()
函数
因此,您的新代码应该如下所示:
private static void writeFile( String call,float freq,String mode,int rstSnt,int rstRx,String city, String state) throws IOException{
File fileName = new File("log.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(fileName);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));
int i =0;
write.write(i +"; " + call + "; " + freq + "; " + mode + "; " + rstSnt + "; " + rstRx + "; " + city + "," + state + "\n");
// Close your Writer
write.close();
// Close your OutputStream
fos.close();
i++;
System.out.println("File has been updated!");
} catch (FileNotFoundException ex) {
Logger.getLogger(QtLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void writeFile( String call,float freq,String mode,int rstSnt,int rstRx,String city, String state) throws IOException{
File fileName = new File("log.txt");
try (FileOutputStream fos = = new FileOutputStream(fileName);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));) {
int i =0;
write.write(i +"; " + call + "; " + freq + "; " + mode + "; " + rstSnt + "; " + rstRx + "; " + city + "," + state + "\n");
i++;
System.out.println("File has been updated!");
} catch (FileNotFoundException ex) {
Logger.getLogger(QtLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
您需要关闭输出,或者更准确地说,您需要编写代码以使其关闭(不一定要显式关闭)。Java7引入了能够精确处理这种情况的语法 任何可自动关闭的对象都可以使用以下语法自动安全地关闭:
private static void writeFile( String call,float freq,String mode,int rstSnt,int rstRx,String city, String state) throws IOException{
File fileName = new File("log.txt");
FileOutputStream fos;
try {
fos = new FileOutputStream(fileName);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));
int i =0;
write.write(i +"; " + call + "; " + freq + "; " + mode + "; " + rstSnt + "; " + rstRx + "; " + city + "," + state + "\n");
// Close your Writer
write.close();
// Close your OutputStream
fos.close();
i++;
System.out.println("File has been updated!");
} catch (FileNotFoundException ex) {
Logger.getLogger(QtLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
private static void writeFile( String call,float freq,String mode,int rstSnt,int rstRx,String city, String state) throws IOException{
File fileName = new File("log.txt");
try (FileOutputStream fos = = new FileOutputStream(fileName);
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(fos));) {
int i =0;
write.write(i +"; " + call + "; " + freq + "; " + mode + "; " + rstSnt + "; " + rstRx + "; " + city + "," + state + "\n");
i++;
System.out.println("File has been updated!");
} catch (FileNotFoundException ex) {
Logger.getLogger(QtLogger.class.getName()).log(Level.SEVERE, null, ex);
}
}
只需将可关闭对象的初始化移到
try
资源块中,即可确保它们已关闭,这将flush()
它们作为关闭的结果。与问题无关,但变量fileName
的名称有点不正确。这是实际的文件
对象,不是文件名。哈哈,很好,谢谢!这可能是因为您需要关闭BufferedWriter和FileOutputStream以确保所有内容都写入文件?是的,缺少。close
调用是问题所在,为什么它在Windows中工作?可能是因为Windows在应用程序关闭时刷新缓冲区,而不是Linux,这只是一个猜测,即使它是真的,您也不能依赖它在关闭之前调用flush
,根本没有必要,flush
仅当我们想写一些东西时才应该使用,但我们仍然希望有文件句柄以便稍后写入(例如套接字编程)至于关闭包装流,然后关闭底层流,同样没有必要(因此write.close
就足够了,不需要fos.close()
)