需要快速帮助。我是Java新手。在我的项目中,我输入了资源中的&输出文件夹。在输入文件夹里我有一个csv文件。我需要将该文件移动到通过java代码输出文件夹。如何将该输入文件复制到输出目录。我曾在谷歌尝试,但没有得到一个工作解决方案。我的项目结构基于标准的Maven项目。如何在java中的资源文件夹中复制文件
-7
A
回答
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
相关问题
- 1. 如何从资源中复制文件?
- 2. 复制文件夹从资源到文档在iphone中sdk
- 3. 如何使用gradle复制classes文件夹中的资源文件?
- 4. 文件中的资源文件夹
- 5. 复制包资源和复制文件中指定的文件?
- 6. 如何从资源的可绘制文件夹中删除文件夹?
- 7. 在输出文件夹中制作Eclipse复制源文件
- 8. Gradle:接受src/main/java文件夹中的资源文件
- 9. 如何在Java中创建Eclipse文件夹资源过滤器
- 10. 将文件从资源复制到文件夹
- 11. 将资源文件复制到文件夹?
- 12. DSC文件资源 - 复制文件
- 13. 如何在Ubuntu中的多个文件夹中复制文件?
- 14. 如何将文件添加到XCode中的资源文件夹?
- 15. 如何登录资源文件夹中的文件?
- 16. 如何在VC++中复制文件夹?
- 17. 如何将文件从“资源”复制到磁盘上的文件夹
- 18. 将源文件夹中的文件复制到不同的目标文件夹
- 19. 如何导出java中资源文件夹中的所有文件和文件夹
- 20. 如何在复制文件时将文件复制到python文件夹中
- 21. 如何制作从源文件夹中绘制的jar文件
- 22. 将文件从源文件夹复制到目标文件夹
- 23. 在Unity中使用资源文件夹
- 24. 行家从不同的资源文件夹中的多个文件复制到一个文件夹tagert
- 25. Java - 复制Jar文件夹
- 26. 如何gradle这个和其他资源文件夹添加资源文件夹
- 27. Java保存资源文件夹到磁盘文件夹
- 28. 如何在系统文件资源管理器中打开文件/文件夹?
- 29. 如何在.NET中复制文件夹和所有子文件夹和文件?
- 30. android-从资产文件夹中复制文件
这是一个Maven的问题吗?你想在构建过程中复制文件吗? –
基本上我必须将该文件从输入文件夹复制到输出文件夹。怎么做? –
不,我想通过java编码做到这一点 –