需求:把模块目录下的“Com.java”复制到模块目录下的“Copy.java”
package com.itcast;
import java.io.*;
public class CopyJavaDemo01 {
public static void main(String[] args) throws IOException {
//根据数据源创建字符流输入对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("Java进阶\\IO流\\Com.java"));
//根据目的地创建字符输出流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("Java进阶\\IO流\\Copy.java"));
// int ss;
// while((ss=isr.read())!=-1){
// osw.write(ss);
// }
// isr.close();
// osw.close();
//读写数据,复制文件
char[] ss = new char[1024];
int len;
while ((len=isr.read(ss))!=-1){
osw.write(ss,0,len);
}
osw.write();
isr.close();
}
}
升级版本
package com.itcast;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class CopyJavaDemo02 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("IO流\\Com.java");
FileWriter fw = new FileWriter("IO流\\Copy.java");
// int ss;
// while((ss=fr.read())!=-1){
// fw.write(ss);
// }
char[] ss = new char[1024];
int len;
while((len=fr.read(ss))!=-1){
fw.write(ss,0,len);
}
fw.close();
fr.close();
}
}
|