将列表内容复制到文件夹中

huangapple 未分类评论53阅读模式
英文:

Copying the content of a list into a folder

问题

我有一个名为"rules"的ArrayList,其中包含规则(Rules)。每个规则是一个XML文件,具有诸如文件名之类的一些属性...

我想将这些规则从ArrayList复制到一个名为"AllMRG"的文件夹中。我尝试了注释之间的代码,但是我收到了"Source 'RG6.31.xml'不存在"的消息。

我通过以下方式更改了代码,但是仍然出现'RG6.31.xml'的问题,尽管ArrayList包含许多规则!

第一次尝试:

File AllMRGFolder = new File("AllMRG");
for (int p = 0; p < rules.size(); p++) {
    /* File MRGFile = new File(rules.get(p).fileName);	
    FileUtils.copyFileToDirectory(MRGFile, AllMRGFolder);  */
			
    File MRGFile = new File("AllMRG/" + rules.get(p).fileName); 
    if (!MRGFile.exists()) {
        FileUtils.copyFileToDirectory(MRGFile, AllMRGFolder);
    }
}

第二次尝试:

String path = "AllMRG";
for (Rule rule : rules) {
    File MRGFile = new File(rule.fileName);
    Files.copy(MRGFile.toPath(), (new File(path + MRGFile.getName())).toPath(), StandardCopyOption.REPLACE_EXISTING);
}

PS:Rule是一个类

public class Rule implements Comparable{
    
    public String fileName;
    public String matches;
    public String TPinstances;
    public int nbrOfMatches;
    public double T;
    
    @Override
    public int compareTo(Object o) {
        if(o instanceof Rule){
            //processing to compare one Rule with another
        }
        return 0;
    }
}

在考虑了Shyam的回答后,这是整个代码。相同的问题仍然存在!

Path directoryPath = Files.createDirectory(Paths.get("AllMGR"));
for (Rule rule : rules) {
    
    Path filePath = directoryPath.resolve(rule.fileName);
    Files.createFile(filePath);
 
    File MRGFile = new File(rule.fileName);
    String ruleContent = new String(Files.readAllBytes(Paths.get(MRGFile.getPath())));
    String fileContent = new String(Files.readAllBytes(filePath));	
    fileContent = ruleContent;
    PrintWriter out13 = new PrintWriter("AllMGR/" + rule.fileName + ".xml");
    out13.print(fileContent);
    out13.close();
}
英文:

I have an arrayList "rules" containing Rules. Each Rule is an XML file and have some properties such as filename...

I want to copy the Rules from the arraylist to a folder named AllMRG. I tried the code between comments but I get the message "Source 'RG6.31.xml' does not exist".

I changed the code by the following, but there is still a problem with 'RG6.31.xml' and the folder AllMRG is empty even though the arrayList contains many Rules!

First attemption:

File AllMRGFolder = new File(&quot;AllMRG&quot;);
for(int p = 0; p &lt; rules.size(); p++) {
	/* File MRGFile = new File(rules.get(p).fileName);	
	FileUtils.copyFileToDirectory(MRGFile, AllMRGFolder);  */
		
	File MRGFile = new File(&quot;AllMRG/&quot; + rules.get(p).fileName); 
	if (!MRGFile.exists()) {
		FileUtils.copyFileToDirectory(MRGFile, AllMRGFolder);
	}
}

Second attemption:

String path = &quot;AllMRG&quot;;
		for(Rule rule : rules) {
			File MRGFile = new File(rule.fileName);
		    Files.copy(MRGFile.toPath(), (new File(path + MRGFile.getName())).toPath(), StandardCopyOption.REPLACE_EXISTING);
		}

PS: Rule is a class

public class Rule implements Comparable{

    public String fileName;
    public String matches;
    public String TPinstances;
    public int nbrOfMatches;
    public double T;

    @Override
    public int compareTo(Object o) {
        if(o instanceof Rule){
            //processing to compare one Rule with another
        }
        return 0;
    }
}

Here is the entire code after having considered Shyam's answer. The same problem persists!

Path directoryPath = Files.createDirectory(Paths.get(&quot;AllMGR&quot;));
		for(Rule rule : rules) {
			
			Path filePath = directoryPath.resolve(rule.fileName);
		    Files.createFile(filePath);
	 
		    File MRGFile = new File(rule.fileName);
		    String ruleContent = new String(Files.readAllBytes(Paths.get(MRGFile.getPath())));
		    String fileContent = new String(Files.readAllBytes(filePath));	
		    fileContent=ruleContent;
			PrintWriter out13= new PrintWriter(&quot;AllMGR/&quot;+rule.fileName+&quot;.xml&quot;);
			out13.print(fileContent);
			out13.close();
} 

答案1

得分: 0

首先,您正在使用rule.filename创建一个新的File,但没有指定任何预定义的路径。然后,您正在构建一个路径,类似于:path + MRGFile.getName(),没有使用路径分隔符,并尝试将文件复制到此位置。我不认为这会起作用。

实际上可以帮助您的是,首先创建一个基本目录,然后在其中创建各个文件。

创建基本目录:

Path directoryPath = Files.createDirectory(Paths.get("AllMGRDir"));

然后,对于您的每个Rule对象,您可以使用以下方法创建文件:

for(Rule rule : rules) {
      Path filePath = directoryPath.resolve(rule.fileName());
      Files.createFile(filePath);
      // 这里是您的剩余代码
}

resolve(String other) 方法解析给定的路径。Java文档指出:

将给定的路径字符串转换为Path,并以与resolve(Path)方法完全相同的方式解析它。例如,假设名称分隔符是“/”,路径表示"foo/bar",然后使用路径字符串"gus"调用此方法将导致Path "foo/bar/gus"。

希望这对您有所帮助。

英文:

Firstly, you are creating a new File with rule.filename without giving any predefined path. Then, you are building a path like: path + MRGFile.getName() without any path delimiters and trying to copy the file to this location. I don't think this will work.

What can actually help you is, creating a base directory first and then creating individual files in it.

Create base directory:
Path directoryPath = Files.createDirectory(Paths.get(&quot;AllMGRDir&quot;));

Then for each of your Rule object you can crate file using:

for(Rule rule : rules) {
      Path filePath = directoryPath.resolve(rule.fileName());
      Files.createFile(filePath);
// your remaining code
    }

The resolve(String other) method resolves the given path. Java doc says that:

> Converts a given path string to a Path and resolves it against this
> Path in exactly the manner specified by the resolve(Path) method.
> For example, suppose that the name separator is "/" and a path
> represents "foo/bar", then invoking this method with the path string
> "gus" will result in the Path "foo/bar/gus"

Hope this helps.

huangapple
  • 本文由 发表于 2020年6月29日 03:05:53
  • 转载请务必保留本文链接:https://java.coder-hub.com/62627062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定