天天看點

java io流讀取問題

public class DeleteFile {

 public static void main(String[] args) throws Exception {

//  File file= new File("E:\\test\\hello\\2.jpg");

//  System.out.println("路徑絕對:"+file.getAbsolutePath());

//  System.out.println("parent()方法:"+file.getParent());

//  System.out.println("listFiles()方法:"+file.listFiles());

  File newFile=new File("F:\\wo");

  File oldFile= new File("F:\\test2\\hello\\2.jpg");

  forJava(oldFile, newFile);

 }

 public static long forJava(File f1,File f2) throws Exception{

   System.out.println(f1.exists()+":"+f2.exists());

   if(!f2.exists()){

    f2.mkdirs();

   }

    long time=new Date().getTime();

    int length=2097152;

    FileInputStream in=new FileInputStream(f1);

    FileOutputStream out=new FileOutputStream(f2);

    byte[] buffer=new byte[length];

    while(true){

     int ins=in.read(buffer);

     if(ins==-1){

      in.close();

      out.flush();

      out.close();

      return new Date().getTime()-time;

     }else

      out.write(buffer,0,ins);

    }

  }

}

報錯:F:\\wo拒絕通路;  以前碰到過這個問題,到網上查,都說是作業系統的權限問題,

其實不是的,問題出現在上面下劃線處,應指定檔案,而不應是目錄。