JAVA IO - 簡單的文件copy程序

瞭解了InputStream和OutputStream後, 簡單的文件copy程序就很簡單了。 java

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();
	}

}
相關文章
相關標籤/搜索