// 以追加的方式将文本输出到本地文件中
public static void writeTextAppendLocalFile(String localFilePath, String content) {
File file = new File(localFilePath);
file.mkdir();
file = new File(localFilePath + "\\java-input-file.txt");
FileOutputStream fos = null;
OutputStreamWriter osw = null;
try {
if (!file.exists()) {
boolean hasFile = file.createNewFile();
if (hasFile) {
// System.out.println("file not exists, create new file");
}
fos = new FileOutputStream(file);
} else {
// System.out.println("file exists");
fos = new FileOutputStream(file, true);
}
osw = new OutputStreamWriter(fos, "utf-8");
osw.write(content);
osw.write("\r\n");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (osw != null) {
osw.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
|