java程序運行步驟「java寫文件路徑」

0x01:
FileInputStream/FileOutputStream位元組流進行文件的複製

private static void streamCopyFile(File srcFile, File desFile) {
       try{
          // 使用位元組流進行文件複製
          FileInputStream fi = new FileInputStream(srcFile);
          FileOutputStream fo = new FileOutputStream(desFile);
          Integer by = 0;
          //一次讀取一個位元組
          while((by = fi.read()) != -1) {
              fo.write(by);
          }
          fi.close();
          fo.close();
       }catch(Exception e){
          e.printStackTrace();
       }

 }

0x02:
BufferedInputStream/BufferedOutputStream高效位元組流進行複製文件

private static void bufferedStreamCopyFile(File srcFile, File desFile){
    try{
         // 使用緩衝位元組流進行文件複製
         BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(desFile));
         byte[] b = new byte[1024];
         Integer len = 0;
         //一次讀取1024位元組的數據
         while((len = bis.read(b)) != -1) {
             bos.write(b, 0, len);
         }
         bis.close();
         bos.close();
     }catch(Exception e){
          e.printStackTrace();
       }
}

0x03: FileReader/FileWriter字符流進行文件複製文件

private static void readerWriterCopyFile(File srcFile, File desFile){
    try{
         // 使用字符流進行文件複製,注意:字符流只能複製只含有漢字的文件
          FileReader fr = new FileReader(srcFile);
          FileWriter fw = new FileWriter(desFile);

          Integer by = 0;
          while((by = fr.read()) != -1) {
              fw.write(by);
          }

         fr.close();
         fw.close();
     }catch(Exception e){
          e.printStackTrace();
     }
}

0x04:
BufferedReader/BufferedWriter高效字符流進行文件複製

private static void bufferedReaderWriterCopyFile(File srcFile, File desFile){
    try{
         // 使用帶緩衝區的高效字符流進行文件複製
          BufferedReader br = new BufferedReader(new FileReader(srcFile));
          BufferedWriter bw = new BufferedWriter(new FileWriter(desFile));

          char[] c = new char[1024];
          Integer len = 0;
          while((len = br.read(c)) != -1) {
              bw.write(c, 0, len);
         }

         //方式二
         /*
         String s = null;
         while((s = br.readLine()) != null) {
             bw.write(s);
             bw.newLine();
         }
        */

         br.close();
         bw.close();
     }catch(Exception e){
          e.printStackTrace();
     }
}

0x05: NIO實現文件拷貝(用transferTo的實現 或者transferFrom的實現)

public static void  NIOCopyFile(String  source,String target) {
   try{
       //1.採用RandomAccessFile雙向通道完成,rw表示具有讀寫權限
       RandomAccessFile fromFile = new RandomAccessFile(source,"rw");
       FileChannel fromChannel = fromFile.getChannel();

       RandomAccessFile toFile = new RandomAccessFile(target,"rw");
       FileChannel toChannel = toFile.getChannel();

       long count =  fromChannel.size();
       while (count > 0) {
           long transferred = fromChannel.transferTo(fromChannel.position(), count, toChannel);
           count -= transferred;
        }
       if(fromFile!=null) {
           fromFile.close();
       }
       if(fromChannel!=null) {
           fromChannel.close();
       }
   }catch(Exception e){
        e.printStackTrace();
   }
}

0x06: java.nio.file.Files.copy()實現文件拷貝,其中第三個參數決定是否覆蓋

public static void copyFile(String source,String target){
    Path sourcePath = Paths.get(source);
    Path destinationPath = Paths.get(target);
    try {
        Files.copy(sourcePath, destinationPath,
                StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

原創文章,作者:投稿專員,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/209300.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
投稿專員的頭像投稿專員
上一篇 2024-12-08 15:34
下一篇 2024-12-08 15:34

相關推薦

發表回復

登錄後才能評論