我们把文件写入的方法抽取出来,哪里需要,那里调用。很基础使用的一个小模块方法。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileTxt {
/**
* @author: zhangxubin
* @date: 2019年5月10日 上午11:41:35
* @Description
*/
public static void main(String[] args) {
String a = "wxhn";
String b = "kem";
FileTxt fileTxt = new FileTxt();
fileTxt.insertInto(a, b);
}
public void insertInto(String area_code,String area_name) {
FileOutputStream fop = null;
File file;
try {
file = new File("D:/fileone.txt");
fop = new FileOutputStream(file,true);
if (!file.exists()) {//判断是否存在,否就创建一个
file.createNewFile();
}
String station = "insert into tb_route (site,site_type,site_city) values(" +"\'"+area_code+"\',"+"\'1\'," +"\'"+ area_name+"\')"+" \r\n";
System.err.println(station);
byte[] contentInBytes = station.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();
}
}
}
}