Function ” writeToFile() ” will be rapidly called to write string to text file .
but I didn’t see any text in the file .
code:
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());
}
}
Writing raw bytes may cause problems with character encoding. As Jon Skeet said, use a writer…