2017-08-30 183 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

相关问题