Java作业三(下)

题目

使用Java的输入、输出流将一个文本文件D:\test.txt的内容按行读出每读一行就顺序添加行号并写入到另一个文件E:\test.bin中。(即P321 4.编程题的第(2)题)

代码

package homework;
import java.io.*;
public class Work_3 {
	public static void main(String[] args) throws IOException {
		File readerin = new File("test.txt");
		readerin.createNewFile();
		File writerout = new File("test.bin");
		writerout.createNewFile();
		try {
			FileReader in = new FileReader(readerin);
			BufferedReader reader = new BufferedReader(in);
			FileWriter out = new FileWriter(writerout);
			BufferedWriter writer = new BufferedWriter(out);
			String str = null;
			int count = 0;
			System.out.println("内容:");
			while((str = reader.readLine())!=null) {
				count++;
				str = count + str;
				writer.write(str);
				writer.newLine();
				System.out.println(str);
			}
			reader.close();
			writer.close();
			System.out.println("完成");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

 

1.腾龙梦屋文章内容无特殊注明皆为源儿原创,转载请注明来源,谢谢!
2.若有相关文章侵犯您的权益,请联系源儿删除,谢谢!
3.相关软件、资料仅供学习参考使用,在24h内务必删除!
腾龙梦屋 » Java作业三(下)
加速支持