今天老大让写一个文件,四五百条数据,一条条整实在费劲,最后从网上千辛万苦找来两个demo组合一下成了
代码没解决异常,只是用来简单生成文件使用
拷贝的话记得解决异常,还有很多细节需要改动,但是平常拿来解决问题是够了
public class Demo1 {
public static void main(String[] args) throws Exception {
File file = new File("D:\\feiq\\Recv Files\\职业.txt");
BufferedReader reader = null;
System.out.println("以行为单位读取文件内容,一次读一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
String[] str = tempString.split(" ");
File file2 = new File("E:" + File.separator + "demo.sql");
OutputStream out = null; // 声明字节输出流
out = new FileOutputStream(file2, true); // 通过子类实例化
String s = str[0]+":"+"'"+str[1]+"'"+","; // 要输出的信息
byte b[] = s.getBytes(); // 将String变为byte数组
out.write(b); // 写入数据
out.close(); // 关闭
}
reader.close();
}
}