Java 将文件列表从一个位置复制到另一个位置
我有一个txt文件中的文件名列表 说A,B,C,D 现在我知道文件列表在C:/data/中。此数据文件夹还包含其他文件,如A、B、C、D、A1、B1等,现在我想将这些A、B、C、D文件从C:/data/复制到C:/dataOne/ 我有一个java代码,它只将一个文件从一个位置复制到另一个位置。但我有txt文件中的文件名列表 这就是我尝试过的Java 将文件列表从一个位置复制到另一个位置,java,file,copy,Java,File,Copy,我有一个txt文件中的文件名列表 说A,B,C,D 现在我知道文件列表在C:/data/中。此数据文件夹还包含其他文件,如A、B、C、D、A1、B1等,现在我想将这些A、B、C、D文件从C:/data/复制到C:/dataOne/ 我有一个java代码,它只将一个文件从一个位置复制到另一个位置。但我有txt文件中的文件名列表 这就是我尝试过的 public static void main(String[] args) { InputStream inStream
public static void main(String[] args)
{
InputStream inStream = null;
OutputStream outStream = null;
try{
File afile =new File("C:\\folderA\\A.txt");
File bfile =new File("C:\\folderB\\A.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();
System.out.println("File is copied successful!");
}catch(IOException e){
e.printStackTrace();
}
}
我的.txt文件中有大约100个文件名
如何将100个文件从一个位置复制到另一个位置。如果使用Java 7或更多版本,则可以执行此操作:
final Path srcdir = Paths.get("C:\\data");
final Path dstdir = Paths.get("C:\\dataOne");
for (final Path path: Files.newDirectoryStream(srcdir))
Files.copy(path, dstdir.resolve(path.getFileName()));
如果不想复制所有文件,可以使用DirectoryStream.filter筛选DirectoryStream
如果要复制的文件名位于文件中,请执行以下操作:
final Path fileNames = Paths.get("filewithfilenames");
final List<String> allFilesByName
= Files.readAllLines(fileNames, StandardCharsets.UTF_8);
然后使用path.get从AllFileByName的每一行获取路径。根据这些路径是相对路径还是绝对路径,您可能必须针对srcdir进行解析
Java8使这更容易,因为它有一个Files.lines;这意味着您可以将.map映射到路径,然后将.forEach路径映射到文件.copy。如果您使用Java 7或更高版本,则可以执行此操作:
final Path srcdir = Paths.get("C:\\data");
final Path dstdir = Paths.get("C:\\dataOne");
for (final Path path: Files.newDirectoryStream(srcdir))
Files.copy(path, dstdir.resolve(path.getFileName()));
如果不想复制所有文件,可以使用DirectoryStream.filter筛选DirectoryStream
如果要复制的文件名位于文件中,请执行以下操作:
final Path fileNames = Paths.get("filewithfilenames");
final List<String> allFilesByName
= Files.readAllLines(fileNames, StandardCharsets.UTF_8);
然后使用path.get从AllFileByName的每一行获取路径。根据这些路径是相对路径还是绝对路径,您可能必须针对srcdir进行解析
Java8使这更容易,因为它有一个Files.lines;这意味着您可以将.map映射到路径,然后将.forEach路径映射到文件.copy。检查此代码。将整个文件夹(包括所有文件)从源复制到目标
import java.io.*;
public class copydir
{
public static void main(String args[])
{
File srcFolder = new File("E://Paresh/programs/test");
File destFolder = new File("D://paresh");
if(!srcFolder.exists())
{
System.out.println("Directory does not exist.");
//just exit
System.exit(0);
}
else{
try{
copyDirectory(srcFolder,destFolder);
}
catch(IOException e)
{
e.printStackTrace();
//error, just exit
System.exit(0);
}
}
System.out.println("Done");
}
public static void copyDirectory(File src , File target) throws IOException
{
if (src.isDirectory())
{
if (!target.exists())
{
target.mkdir();
}
String[] children = src.list();
for (int i=0; i<children.length; i++)
{
copyDirectory(new File(src, children[i]),new File(target, children[i]));
}
}
// if Directory exists then only files copy
else
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(target);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}
欢迎您查看此代码。将整个文件夹(包括所有文件)从源复制到目标
import java.io.*;
public class copydir
{
public static void main(String args[])
{
File srcFolder = new File("E://Paresh/programs/test");
File destFolder = new File("D://paresh");
if(!srcFolder.exists())
{
System.out.println("Directory does not exist.");
//just exit
System.exit(0);
}
else{
try{
copyDirectory(srcFolder,destFolder);
}
catch(IOException e)
{
e.printStackTrace();
//error, just exit
System.exit(0);
}
}
System.out.println("Done");
}
public static void copyDirectory(File src , File target) throws IOException
{
if (src.isDirectory())
{
if (!target.exists())
{
target.mkdir();
}
String[] children = src.list();
for (int i=0; i<children.length; i++)
{
copyDirectory(new File(src, children[i]),new File(target, children[i]));
}
}
// if Directory exists then only files copy
else
{
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(target);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0)
{
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
}
欢迎光临是的,我不想复制所有文件,我只选择了一些文件,我知道这些文件的文件名。请看我的编辑,它包含一种获取文件名的方法。我尝试了第一种方法,将所有文件复制到dir,效果很好。第二种方法,我在.txt文件中有文件名,final Path fileNames=Path.getfilenames.txt;最终列表allFilesByName=Files.readAllLinesfileNames,StandardCharsets.UTF_8;在这之后,我需要迭代它,对吗;你需要为每一行创建一条路径注意,新行将被删除,你应该很乐意去我会尝试并返回给你,请稍候是的,我不想复制所有文件,我只选择了一些文件,我知道这些文件的文件名见我的编辑,它包含一种获取文件名的方法我尝试了第一种方法将所有文件复制到dir,这很有效,第二种方法的文件名是.txt文件,final Path fileNames=Path.getfilenames.txt;最终列表allFilesByName=Files.readAllLinesfileNames,StandardCharsets.UTF_8;在这之后,我需要迭代它,对吗;你需要从每一行中建立一条路径注意,新行将被删除,你应该很乐意去做。我会尝试并返回给你,请稍候