废话不多说 上代码:
public static void writeFile(String content,String filePath){
FileOutputStream fop = null;
File file;
//String content = "This is the text content";
try {
file = new File(filePath);
if (file.exists()){
file.delete();
}
file.createNewFile();
fop = new FileOutputStream(file);
//content = "测试数据";
byte[] contentInBytes = content.getBytes();
fop.write(contentInBytes);
fop.flush();
fop.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fop != null) {
fop.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
如上代码是好的,就只是调换了一下一行代码的顺序。如下图时是没有数据的:我猜想是因为之前的文件和删除的文件不是同一内存地址了,但是windows下是好的,linux下是坏的,只能说jvm在平台下也是有差异的吧。