import java.io.*;public class In { public static void main(String[] args) throws Exception { File f = new File("in.txt");//指定要输入的文件 FileInputStream fis = new FileInputStream(f); BufferedInputStream bis = new BufferedInputStream(fis);
System.setIn(bis);//重新分配“标准”输入流。 int l = (int)f.length(); byte[] b = new byte[l]; System.in.read(b, 0, l);//读入到字节数组中 bis.close(); for(int i=0; i<b.length; i++) { System.out.print((char)b[i]); } } }上面只演示了输入流,楼主可以自己实现输出流!
如果是处理字符用字符流 ,以下是用字符流复制文件的简单做法! import java.io.*; public class FileCopyTest1{ public static void main(String args[]){ File f1=new File("F://a.txt"); File f2=new File("F://c.txt"); long time1=0L; long time2=0L; time1=System.currentTimeMillis(); try{ FileReader in=new FileReader(f1); BufferedReader bi=new BufferedReader(in,1024);
System.setIn(new FileInputStream("C:/test.txt"));
import java.io.*;public class In {
public static void main(String[] args) throws Exception {
File f = new File("in.txt");//指定要输入的文件
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
System.setIn(bis);//重新分配“标准”输入流。
int l = (int)f.length();
byte[] b = new byte[l];
System.in.read(b, 0, l);//读入到字节数组中
bis.close();
for(int i=0; i<b.length; i++) {
System.out.print((char)b[i]);
}
}
}上面只演示了输入流,楼主可以自己实现输出流!
import java.io.*;
public class FileCopyTest1{
public static void main(String args[]){
File f1=new File("F://a.txt");
File f2=new File("F://c.txt");
long time1=0L;
long time2=0L;
time1=System.currentTimeMillis();
try{
FileReader in=new FileReader(f1);
BufferedReader bi=new BufferedReader(in,1024);
FileWriter out=new FileWriter(f2);
BufferedWriter bo=new BufferedWriter(out,1024);
String data=bi.readLine();
while(data!=null){
bo.write(data);
bo.newLine();
data=bi.readLine();
}
bi.close();
bo.close();
time2=System.currentTimeMillis();
System.out.println("复制文件所需时间:"+(time1-time2)+"毫秒!");
}catch(IOException e){
e.getMessage();
}
}
}