了解了InputStream和OutputStream后, 简单的文件copy程序就很简单了。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFile {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
InputStream in = new FileInputStream("helloWorld.txt");
byte[] bs= new byte[1024];
int b = -1;
int start = 0;
while(( b = in.read()) != -1){
bs[start] = (byte)b;
start++;
}
System.out.println(new String(bs, 0, start));
in.close();
OutputStream out = new FileOutputStream("copyhelloworld.txt");
out.write(bs, 0, start);
out.close();
}
}