【发布时间】:2021-12-02 16:48:06
【问题描述】:
这是我的代码,我想创建方法,接受文件并将其移动到指定文件夹的我的电脑中。我只是将现有的文本文件复制到另一个文本文件,但我想移动到指定的文件夹中,而不是复制。如何解决这个问题?
public static void main(String[] args) {
InputStream inStream = null;
OutputStream outStream = null;
try {
File afile = new File("C:\\Users\\anar.memmedov\\Desktop\\test.txt");
File bfile = new File("C:\\Users\\anar.memmedov\\Desktop\\ok\\test3.txt");
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
//delete the original file
afile.delete();
System.out.println("File is copied successful!");
} catch (IOException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
我读了这篇文章,但只有移动到另一个文件,而不是文件夹。我只想接受文件并将其移动到我电脑中的指定文件夹。
标签: java file directory copy move