2017-08-30 182 views
-7

需要快速幫助。我是Java新手。在我的項目中,我輸入了資源中的&輸出文件夾。在輸入文件夾裏我有一個csv文件。我需要將該文件移動到通過java代碼輸出文件夾。如何將該輸入文件複製到輸出目錄。我曾在谷歌嘗試,但沒有得到一個工作解決方案。我的項目結構基於標準的Maven項目。如何在java中的資源文件夾中複製文件

+0

這是一個Maven的問題嗎?你想在構建過程中複製文件嗎? –

+0

基本上我必須將該文件從輸入文件夾複製到輸出文件夾。怎麼做? –

+0

不,我想通過java編碼做到這一點 –

回答

1

我認爲至少有四種方法可以用來將csv文件移動到其他目錄。

我假設你已經知道絕對目錄路徑

方法1.阿帕奇百科全書

的方式,您可以使用Apache公地IO庫。

public static void moveWithApacheCommonsIO() 
    { 
     File sourceFile = new File("resource/AssetsImportCompleteSample.csv"); 
     File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv"); 

     try { 
      FileUtils.moveFile(sourceFile, destinationFile); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

方法2. Java的NIO的方式

您也可以使用的Java NIO(非阻塞IO)

public static void moveWithFileNIO() 
    { 
     File sourceFile = new File("resource/AssetsImportCompleteSample.csv"); 
     File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv"); 
     FileInputStream inputStream = null; 
     FileOutputStream outputStream = null; 

     sourceFile.deleteOnExit(); 

     try { 
      inputStream = new FileInputStream(sourceFile); 
      outputStream = new FileOutputStream(destinationFile); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     final FileChannel inChannel = inputStream.getChannel(); 
     final FileChannel outChannel = outputStream.getChannel(); 

     try { 
      inChannel.transferTo(0, inChannel.size(), outChannel); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally 
     { 
      try { 
       inChannel.close(); 
       outChannel.close(); 
       inputStream.close(); 
       outputStream.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

    } 

方法3.傳統的Java的方式IO

您可以在java中使用傳統方法和文件輸入流(Blocking IO)

public static void moveWithFileInOutStream() 
    { 
     File sourceFile = new File("resource/AssetsImportCompleteSample.csv"); 
     File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv"); 
     InputStream fin = null; 
     OutputStream fout = null; 

     sourceFile.deleteOnExit(); 

     try { 
      fin = new BufferedInputStream(new FileInputStream(sourceFile)); 
      fout = new BufferedOutputStream(new FileOutputStream(destinationFile)); 

      byte[] readBytes = new byte[1024]; 
      int readed = 0; 


      while((readed = fin.read(readBytes)) != -1) 
      { 
       fout.write(readBytes, 0, readed); 

      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally 
     { 
      try { 
       fin.close(); 
       fout.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
    } 

方法4.使用JNI(Java本地接口)

最後,你可以像使用取決於使用JNI您的操作系統的MV或MovFile一些功能。

我認爲它超出了本主題的範圍。你可以谷歌它或看到JNA庫來完成你的任務,如果你想。

這是完整的代碼給你。

import java.io.BufferedInputStream; 
import java.io.BufferedOutputStream; 
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; 
import java.nio.channels.FileChannel; 

import org.apache.commons.io.FileUtils; 

public class MovefileTest { 

    public static void moveWithApacheCommonsIO() 
    { 
     File sourceFile = new File("resource/AssetsImportCompleteSample.csv"); 
     File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv"); 

     try { 
      FileUtils.moveFile(sourceFile, destinationFile); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    public static void moveWithFileInOutStream() 
    { 
     File sourceFile = new File("resource/AssetsImportCompleteSample.csv"); 
     File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv"); 
     InputStream fin = null; 
     OutputStream fout = null; 

     sourceFile.deleteOnExit(); 

     try { 
      fin = new BufferedInputStream(new FileInputStream(sourceFile)); 
      fout = new BufferedOutputStream(new FileOutputStream(destinationFile)); 

      byte[] readBytes = new byte[1024]; 
      int readed = 0; 


      while((readed = fin.read(readBytes)) != -1) 
      { 
       fout.write(readBytes, 0, readed); 

      } 

     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally 
     { 
      try { 
       fin.close(); 
       fout.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

     } 
    } 

    public static void moveWithFileNIO() 
    { 
     File sourceFile = new File("resource/AssetsImportCompleteSample.csv"); 
     File destinationFile = new File("resource/aa/AssetsImportCompleteSample.csv"); 
     FileInputStream inputStream = null; 
     FileOutputStream outputStream = null; 

     sourceFile.deleteOnExit(); 

     try { 
      inputStream = new FileInputStream(sourceFile); 
      outputStream = new FileOutputStream(destinationFile); 
     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     final FileChannel inChannel = inputStream.getChannel(); 
     final FileChannel outChannel = outputStream.getChannel(); 

     try { 
      inChannel.transferTo(0, inChannel.size(), outChannel); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     finally 
     { 
      try { 
       inChannel.close(); 
       outChannel.close(); 
       inputStream.close(); 
       outputStream.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     } 

    } 

    public static void main(String[] args) { 

     //moveWithApacheCommonsIO(); 
     //moveWithFileNIO(); 
     moveWithFileInOutStream(); 

    } 
} 

問候,

+0

如果你只是想移動一個文件,然後Files.move很容易使用,它提供了在需要時替換現有文件的功能。 –

+0

如果您沒有Files.move,該怎麼辦?這不是一個問題。我確實回答。爲什麼你不用它作爲答案 – tommybee

相關問題