1.设置文件路径
String filePath = "xxx\\aaa.txt";
2.创建备份文件对象
File sqlFile = new File(filePath);
3.声明所需要的对象
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter rw = null;
4.关键代码
fos = new FileOutputStream(sqlFile);
osw = new OutputStreamWriter(fos);
rw = new BufferedWriter(osw);
for (String tmp : sqls) {
rw.write(tmp);
rw.newLine();
rw.flush();
}
5.最后需要倒序关闭所有的IO流
finally {
if (rw != null) {
try {
rw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
完整的代码
String filePath = "xxx\\aaa.txt";
File sqlFile = new File(filePath);
FileOutputStream fos = null;
OutputStreamWriter osw = null;
BufferedWriter rw = null;
try {
fos = new FileOutputStream(sqlFile);
osw = new OutputStreamWriter(fos);
rw = new BufferedWriter(osw);
for (String tmp : sqls) {
rw.write(tmp);
rw.newLine();
rw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (rw != null) {
try {
rw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw != null) {
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}