【问题标题】:How to copy file in resource folder in java如何在java中复制资源文件夹中的文件
【发布时间】:2018-02-07 14:53:11
【问题描述】:

需要快速帮助。我是 Java 新手。在我的项目中,我在资源中有输入和输出文件夹。在输入文件夹中,我有一个 csv 文件。我需要通过java代码将该文件移动到输出文件夹。如何将该输入文件复制到输出目录。我在谷歌尝试过,但没有得到有效的解决方案。我的项目结构是基于标准的maven项目。

【问题讨论】:

  • 这是一个专家问题吗?您想在构建期间复制文件吗?
  • 基本上我必须将该文件从输入文件夹复制到输出文件夹。该怎么做?
  • 没有。我想通过java编码来做到这一点
  • @SujataRoy 分享相关代码并解释哪些部分不起作用以及您遇到的错误是什么。浏览stackoverflow.com/help/mcvestackoverflow.com/help/how-to-ask 链接,然后相应地更新您的问题
  • 您需要在问题本身中分享代码。在您分享错误和您的观察之前,分享代码也无济于事。就像我说的那样,通过 stackoverflow.com/help/mcve 和 stackoverflow.com/help/how-to-ask 链接,然后相应地更新您的问题

标签: java file


【解决方案1】:

我认为您至少可以使用四种方法将 csv 文件移动到其他目录。

我假设您已经知道绝对目录路径

方法一、apache commons的方式

您可以使用 apache commons 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();
        }
    }

方法二、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中使用文件输入和输出流。(阻塞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 Native Interface)

最后,您可以使用 mv 或 MovFile 之类的函数,具体取决于您的 JNI 操作系统。

我认为这超出了本主题的范围。如果需要,您可以 google 或查看 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();

    }
}

问候,

【讨论】:

  • 如果你只是想移动一个文件,那么 Files.move 很容易使用,它提供了在你需要时替换现有文件的能力。
  • 如果您没有 Files.move 怎么办?这不是一个问题。我确实回答了。你为什么不用它作为答案
猜你喜欢
  • 2020-01-20
  • 1970-01-01
  • 1970-01-01
  • 2013-05-10
  • 2017-05-18
  • 2023-01-11
  • 2016-08-06
  • 2013-04-06
  • 1970-01-01
相关资源
最近更新 更多