复制文件到另外一个地址

/*
第一种复制每次复制一个字符
*/

package test;


import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.nio.channels.NonReadableChannelException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {

	private static final String space_operator = " ";
	private static final double pi = Math.PI;
	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	public static void main(String[] args) throws Exception {

		FileReader fReader = new FileReader("e:\\开开.txt");
		FileWriter fWriter = new FileWriter("copy_2.txt");
		
		int ch = 0;
		while((ch = fReader.read()) != -1)
		{
			fWriter.write(ch);
		}
		fReader.close();
		fWriter.close();
	
	}
}


/*
第二种方法用数组复制
*/

package test;


import java.io.FileReader;
import java.io.FileWriter;
import java.math.BigDecimal;
import java.nio.channels.NonReadableChannelException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {

	private static final String space_operator = " ";
	private static final double pi = Math.PI;
	private static final String LINE_SEPARATOR = System.getProperty("line.separator");
	public static void main(String[] args) throws Exception {

		FileReader fReader = new FileReader("e:\\开开.txt");
		FileWriter fWriter = new FileWriter("copy_3.txt");
		
		char []buf = new char[1024];
		int len = 0;
		while((len = fReader.read(buf)) != -1)
		{
			fWriter.write(new String(buf,0,len));
		}
		fReader.close();
		fWriter.close();
	
	}
}

  

posted @ 2019-11-03 20:08  WINDZLY  阅读(237)  评论(0编辑  收藏  举报