将快速调用函数“writeToFile()”将字符串写入文本文件。但我没有在文件中看到任何文本。
代码:
public class MyClass {
private File data_file = new File("data_from_java.txt");
public void writeToFile(String str){
try {
FileOutputStream fos= new FileOutputStream(this.data_file, true);
System.out.print(str); // there is text shown in terminal
fos.write(str.getBytes());
fos.flush();
fos.close(); // why file donesn't have text
}catch(Exception ex) {
System.out.println(ex.getMessage());
}
}