java.io 第一个例子 TestFileInputStream

来源:互联网 发布:宁弈对知微的爱 编辑:程序博客网 时间:2024/05/15 18:42

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class TestFileInputStream {

 /**
  * @param args
  */
 public static void main(String[] args) {
  int b=0;
  try{
   InputStream in=new FileInputStream("F:"+File.separator+"space"+File.separator+"IOTest"+File.separator+"src"+File.separator+"TestFileInputStream.java");

//这里也可以用“//”或“\”代表windows下的文件分隔符
   OutputStream out=new FileOutputStream("D://Test.java");

//文件不存在会先创建文件再写入流中的内容,文件存在直接向文件写入流中的内容。
   while((b=in.read())!=-1){
    out.write(b);
   }
   in.close();
   out.close();
  }catch(FileNotFoundException e){
   System.out.println(e.getMessage());
   System.exit(-1);
  }catch(IOException e){
   System.out.println(e.getMessage());
   System.exit(-1);
  }
  System.out.println("文件复制完毕");
 }

}

原创粉丝点击