在Java中维护目录结构的同时从列表复制文件
我一直在寻找一个如何做到这一点的例子,但我还没有找到一个 我有一个日志文件,在这个日志文件中将有一个文件列表。然后,我需要将扫描的文件移动到隔离的文件夹中,并保留目录结构。到目前为止,我有以下代码:在Java中维护目录结构的同时从列表复制文件,java,copy,Java,Copy,我一直在寻找一个如何做到这一点的例子,但我还没有找到一个 我有一个日志文件,在这个日志文件中将有一个文件列表。然后,我需要将扫描的文件移动到隔离的文件夹中,并保留目录结构。到目前为止,我有以下代码: public static void main(String[] args) throws FileNotFoundException, IOException { String logPath = "C:\\apache-ant-1.8.2\\build\\LogFiles\\Do
public static void main(String[] args) throws FileNotFoundException, IOException
{
String logPath = "C:\\apache-ant-1.8.2\\build\\LogFiles\\DoScan.log";
String safeFolder = "C:\\apache-ant-1.8.2\\build\\Quaratined";
ArrayList<File> files = new ArrayList<File>();
BufferedReader br = new BufferedReader(new FileReader( logPath ));
String line = null;
while ((line = br.readLine()) != null)
{
Pattern pattern = Pattern.compile("'[a-zA-Z]:\\\\.+?'");
Matcher matcher = pattern.matcher( line );
if (matcher.matches())
{
}
if (matcher.find())
{
String s = matcher.group();
s = s.substring(1, s.length()-1);
files.add(new File(s));
System.out.println("File found:" + files.get(files.size() - 1));
}
}
for (File f : files)
{
// Make sure we get a file indeed
if (f.exists())
{
if (!f.renameTo(new File(safeFolder, f.getName())))
{
System.out.println("Moving file: " + f + " to " + safeFolder);
System.err.println("Unable to move file: " + f);
}
}
else
{
System.out.println("Could not find file: " + f);
}
}
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException、IOException
{
String logPath=“C:\\apache-ant-1.8.2\\build\\LogFiles\\DoScan.log”;
String safeFolder=“C:\\apache-ant-1.8.2\\build\\quarated”;
ArrayList files=新的ArrayList();
BufferedReader br=新的BufferedReader(新文件读取器(日志路径));
字符串行=null;
而((line=br.readLine())!=null)
{
Pattern=Pattern.compile(“[a-zA-Z]:\\\.+?”);
匹配器匹配器=模式匹配器(线);
if(matcher.matches())
{
}
if(matcher.find())
{
字符串s=matcher.group();
s=s.substring(1,s.length()-1);
文件。添加(新文件);
System.out.println(“找到的文件:+files.get(files.size()-1));
}
}
用于(文件f:文件)
{
//确保我们确实得到了一份文件
如果(f.exists())
{
如果(!f.renameTo(新文件(safeFolder,f.getName()))
{
System.out.println(“将文件:+f+”移动到“+safeFolder”);
System.err.println(“无法移动文件:+f”);
}
}
其他的
{
System.out.println(“找不到文件:+f”);
}
}
}
}
这可以工作并成功地移动文件,但不维护目录结构
非常感谢您的帮助 试试这样的方法:
public static void main(String[] args) throws IOException {
String logPath = "/tmp/log";
String safeFolder = "/tmp/q";
ArrayList<File> files = new ArrayList<File>();
BufferedReader br = new BufferedReader(new FileReader(logPath));
String line = null;
while ((line = br.readLine()) != null) {
files.add(new File(line));
System.out.println("File found:" + files.get(files.size() - 1));
}
String root = "/tmp/" ;
for (File f : files && f.isFile()) {
if (f.exists()) {
File destFile = new File(safeFolder, f.getAbsolutePath().replace(root,""));
destFile.getParentFile().mkdirs();
if (!f.renameTo(destFile)) {
System.out.println("Moving file: " + f + " to " + safeFolder);
System.err.println("Unable to move file: " + f);
}
} else {
System.out.println("Could not find file: " + f);
}
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
字符串logPath=“/tmp/log”;
字符串safeFolder=“/tmp/q”;
ArrayList files=新的ArrayList();
BufferedReader br=新的BufferedReader(新文件读取器(日志路径));
字符串行=null;
而((line=br.readLine())!=null){
添加(新文件(行));
System.out.println(“找到的文件:+files.get(files.size()-1));
}
字符串root=“/tmp/”;
对于(文件f:files&&f.isFile()){
如果(f.exists()){
File destFile=新文件(safeFolder,f.getAbsolutePath().replace(根“”);
destFile.getParentFile().mkdirs();
如果(!f.renameTo(destFile)){
System.out.println(“将文件:+f+”移动到“+safeFolder”);
System.err.println(“无法移动文件:+f”);
}
}否则{
System.out.println(“找不到文件:+f”);
}
}
}
尝试以下方法:
public static void main(String[] args) throws IOException {
String logPath = "/tmp/log";
String safeFolder = "/tmp/q";
ArrayList<File> files = new ArrayList<File>();
BufferedReader br = new BufferedReader(new FileReader(logPath));
String line = null;
while ((line = br.readLine()) != null) {
files.add(new File(line));
System.out.println("File found:" + files.get(files.size() - 1));
}
String root = "/tmp/" ;
for (File f : files && f.isFile()) {
if (f.exists()) {
File destFile = new File(safeFolder, f.getAbsolutePath().replace(root,""));
destFile.getParentFile().mkdirs();
if (!f.renameTo(destFile)) {
System.out.println("Moving file: " + f + " to " + safeFolder);
System.err.println("Unable to move file: " + f);
}
} else {
System.out.println("Could not find file: " + f);
}
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
字符串logPath=“/tmp/log”;
字符串safeFolder=“/tmp/q”;
ArrayList files=新的ArrayList();
BufferedReader br=新的BufferedReader(新文件读取器(日志路径));
字符串行=null;
而((line=br.readLine())!=null){
添加(新文件(行));
System.out.println(“找到的文件:+files.get(files.size()-1));
}
字符串root=“/tmp/”;
对于(文件f:files&&f.isFile()){
如果(f.exists()){
File destFile=新文件(safeFolder,f.getAbsolutePath().replace(根“”);
destFile.getParentFile().mkdirs();
如果(!f.renameTo(destFile)){
System.out.println(“将文件:+f+”移动到“+safeFolder”);
System.err.println(“无法移动文件:+f”);
}
}否则{
System.out.println(“找不到文件:+f”);
}
}
}
System.out.println(“找到的文件:+files.get(files.size()-1));”的输出是什么?您还想从哪个“根目录”保存目录结构?日志文件中会有如下内容:[u 1126]|处理文件“C:\apache-ant-1.8.2\build\Plugins\File8.exe”。[_1124]|处理文件“C:\apache-ant-1.8.2\build\Plugins\JAR\file 1.txt”|正在处理文件“C:\apache-ant-1.8.2\build\Plugins\JAR\file 2.txt”。我想让它读取该列表,从该列表复制文件,并维护目录结构。。。所以在隔离文件夹中,我会有C:\apache-ant-1.8.2\build\quaranted\File8.exe C:\apache-ant-1.8.2\build\quaranted\JAR\File 1.txt C:\apache-ant-1.8.2\build\quaranted\JAR\File 2.txt“System.out.println”(“找到的文件:“+files.get(files.size()-1));”的输出是什么?您还想从哪个“根目录”保存目录结构?日志文件中会有如下内容:[u 1126]|处理文件“C:\apache-ant-1.8.2\build\Plugins\File8.exe”。[_1124]|处理文件“C:\apache-ant-1.8.2\build\Plugins\JAR\file 1.txt”|正在处理文件“C:\apache-ant-1.8.2\build\Plugins\JAR\file 2.txt”。我想让它读取该列表,从该列表复制文件,并维护目录结构。。。所以在隔离文件夹中,我会有C:\apache-ant-1.8.2\build\quaranted\File8.exe C:\apache-ant-1.8.2\build\quaranted\JAR\File 1.txt C:\apache-ant-1.8.2\build\quaranted\JAR\File 2.txt尽管现在唯一的问题是它在15:29:17.696931[\u 1118]时移动了整个目录|正在处理目录“C:\apache-ant-1.8.2\build\Plugins”。我需要它只复制实际文件,您需要执行以下操作之一:1)使用regexp匹配“目录”/“文件”2)调用f.isDirectory()检查它是文件还是dirah。我更喜欢你的解决方案。再次感谢,但现在唯一的问题是,当它到达15:29:17.696931[_1118]|处理目录“C:\apache-ant-1.8.2\build\Plugins”时,它正在移动整个目录。我需要它只复制实际的文件。您需要执行以下操作之一:1)使用