java复制文本文件代码 java复制文本文件代码怎么写

java将两个文本文件的内容复制到另一个文件中主要是用到java里面的i/o流 。代码例子如下:
import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader; /** * java读写文件,复制文件 * 读取d:/1.txt文件内容,写入f:/text.txt文件中. * @author young * */public class FileWriterTest {// 读写文件public static void rwFile(){FileWriter fw = null;BufferedReader br = null;try {fw = new FileWriter("f:\\text.txt", true);br = new BufferedReader(new InputStreamReader(new FileInputStream("d:\\1.txt"), "UTF-8"));String line = null;while ((line = br.readLine()) != null) {System.out.println("文件内容: " + line);fw.write(line);fw.flush();}br.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fw != null) {try {fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}public static void main(String[] args) {rwFile();}}
首先在D盘新建文件1.txt,输入任意内容 。然后执行java代码即可 。
利用JAVA语言编写一个 名为copy的程序 实现文件的拷贝功能,应该怎样做?import java.io.File;\x0d\x0aimport java.io.FileInputStream;\x0d\x0aimport java.io.FileNotFoundException;\x0d\x0aimport java.io.FileOutputStream;\x0d\x0aimport java.io.IOException;\x0d\x0apublic class Copy {\x0d\x0a/**\x0d\x0a* @param args\x0d\x0a*/\x0d\x0apublic static void main(String[] args) {\x0d\x0a// TODO Auto-generated method stub\x0d\x0aif(args.length!=2){\x0d\x0aSystem.out.print("没有输入正确数目的参数 , 程序退出java复制文本文件代码!");\x0d\x0aSystem.exit(0);\x0d\x0a}\x0d\x0aFile fileS = new File("./"+args[0]);\x0d\x0aFile fileD = new File("./"+args[1]);\x0d\x0aif(fileD.exists())System.out.println("目标文件 "+args[1]+" 已存在java复制文本文件代码!");\x0d\x0abyte[] temp = new byte[50];\x0d\x0aint totalSize = 0;\x0d\x0atry {\x0d\x0aFileInputStream fr = new FileInputStream(fileS);\x0d\x0aFileOutputStream fo = new FileOutputStream(fileD);\x0d\x0aint length = 0;\x0d\x0awhile((length = fr.read(temp, 0, temp.length)) != -1){\x0d\x0atotalSize += length;\x0d\x0afo.write(temp, 0, length);\x0d\x0a}\x0d\x0aSystem.out.println("文件 "+args[0]+" 有 "+totalSize+" 个字节");\x0d\x0aSystem.out.println("复制完成!");\x0d\x0a} catch (FileNotFoundException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0aSystem.out.println("源文件 "+args[0]+" 不存在!");\x0d\x0a} catch (IOException e) {\x0d\x0a// TODO Auto-generated catch block\x0d\x0ae.printStackTrace();\x0d\x0a}\x0d\x0a}\x0d\x0a}
java中怎么复制一个文件的内容主要是用到java里面的i/o流 。代码例子如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* java读写文件,复制文件
* 读取d:/1.txt文件内容,写入f:/text.txt文件中.
* @author young
*
*/
public class FileWriterTest {
// 读写文件
public static void rwFile(){
FileWriter fw = null;
BufferedReader br = null;
try {
fw = new FileWriter("f:\\text.txt", true);
br = new BufferedReader(new InputStreamReader(
new FileInputStream("d:\\1.txt"), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
System.out.println("文件内容: " + line);
fw.write(line);
fw.flush();
}
br.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {

推荐阅读