文件内容复制

  1. 创建两个txt文件,(inputstream.txt,outputstream.txt)

Java流操作FileReader&FileWriter
2. 在inputstream.txt中写入文字(outputstream.txt 中不写任何内容)
Java流操作FileReader&FileWriter

Java流操作FileReader&FileWriter

  1. 进行文件内容拷贝
package com.gao.xi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class TestAllEng2 {

	public static void main(String[] args) {

		/*
		 * 1.获取当前类的所在路径 
		 * 2.获取两个文件的路径 
		 * 3.声明FileReader和FileWriter变量
		 * 4.实例化文件输出输入流
		 * 5.读写文件
		 * 6.关闭流
		 */
		// * 1.获取当前类的所在路径
		String path = "src/com/gao/xi/";
		// * 2.获取两个文件的路径
		String inpath = path + "inputstream.txt";
		String outpath = path + "outputstream.txt";
		// * 3.声明FileInputStream和FileOutStream变量
		FileReader fin = null;
		FileWriter fout = null;
		try {
			// * 4.实例化文件输出输入流
			fin = new FileReader(inpath);
			fout = new FileWriter(outpath,true);
			// * 5.读写文件
			char[] buff = new char[1024];
			int len = 0;
			while ((len = fin.read(buff)) != -1) {
				fout.write(buff, 0, len);
				System.out.println(new String(buff, 0, len));
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			// * 6.关闭流
			if (fin != null) {
				try {
					fin.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			if (fout != null) {
				try {
					fout.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		}

	}

}


Java流操作FileReader&FileWriter

相关文章:

  • 2022-12-23
  • 2021-11-10
  • 2022-12-23
  • 2021-05-22
  • 2021-09-25
  • 2021-09-20
  • 2022-01-16
  • 2022-12-23
猜你喜欢
  • 2021-06-20
  • 2021-11-20
  • 2022-12-23
  • 2022-12-23
  • 2021-04-03
  • 2021-11-20
  • 2022-01-29
相关资源
相似解决方案