需要快速幫助。我是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編碼做到這一點 –