我正在編寫一個簡單的終端程序,記錄一些信息,並將其放入一個文本文件中,稍後有人可以回憶。主要是爲了記錄他所做的事情。我在窗戶上很好,並沒有真正遇到這個問題,但我擔心我正在尋找簡單的東西。Linux和java:正在創建文件,但文本沒有寫入
就像我之前說的,如果我瀏覽到該項目目錄,我看到文件已經創建,但是當我打開使用文本編輯器文件,打印沒有創建的字符串中的數據。
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);
}
}
無關你的問題,但變量'fileName'是一種嚴重命名。它是實際的'File'對象,而不是文件*名稱*。 –
哈哈很好,謝謝! – user3026473
是否因爲您需要關閉BufferedWriter和FileOutputStream以確保所有內容都寫入文件? –