Files.readAllLines()在FileWriting之后不起作用。

huangapple go评论89阅读模式
英文:

Files.readAllLine() not working after FileWriting

问题

希望你一切顺利,这是我的第一个问题:
我遇到了一个问题:
我的目标是从代码片段(字段、属性、构造函数)创建Java文件。
我尝试通过在读取和写入文件之间进行切换来实现这一目标。
读取文件以获取旧值,删除闭合的“}”
写入文件:新的代码片段,加上闭合的“}”
我尝试的问题在于Files.readAllLines()FileWriter()不起作用。
你可以在下面找到我的源代码。

public static void fillFile(String fileName, String name, String value) throws IOException {
    Path path = Paths.get(fileName);
    List<String> all = Files.readAllLines(path, StandardCharsets.UTF_8);
    System.out.println(" the path " + Paths.get(fileName));
    System.out.println(name + " :; " + value);
    System.out.println("to write " + all.toString().replaceAll(", ", " ").substring(1, (all+"").replaceAll(", ", " ").length()-1)  + "\n" +  name + (name.endsWith(")")?"":" = ")+ value+ (name.endsWith(")")?"":";")+"\n } ddddddddddddddddddddddd");
    FileWriter test = new FileWriter(fileName,true);
    test.write(all.toString().replaceAll(", ", " ").substring(1, all.toString().replaceAll(", ", " ").length()-1) +  "\n" +  name + (name.endsWith(")")?"":" = ")+ value+ (name.endsWith(")")?"":";")+"\n }");
    //test.flush();
    test.close();
}

另一个问题:是否有其他更简单的方法来实现我的目标?

英文:

hoping you're doing well, this is my first question <br>
I have a trouble: <br>
My goal is to create java files from pieces of code (fields, attributes, constructor)
I tried to do this by altering between reading and writing file. <br>
Reading file to get the old value , delete closing "}" <br>
Writing in file : the new piece of code, plus closing "}" <br>
The problem with my try is that Files.readAllLine() or FileWriter() is not working.
You can find my source code below.

public static void fillFile(String fileName, String name, String value) throws IOException {
		Path path = Paths.get(fileName);
		List&lt;String&gt; all = Files.readAllLines(path,StandardCharsets.UTF_8);
		System.out.println(&quot; the path &quot;+Paths.get(fileName));
        System.out.println(name + &quot; :; &quot;+ value);
		System.out.println(&quot;to write &quot; + all.toString().replaceAll(&quot;, &quot;, &quot; &quot;).substring(1, (all+&quot;&quot;).replaceAll(&quot;, &quot;, &quot; &quot;).length()-1)  + &quot;\n&quot; +  name + (name.endsWith(&quot;)&quot;)?&quot;&quot;:&quot; = &quot;)+ value+ (name.endsWith(&quot;)&quot;)?&quot;&quot;:&quot;;&quot;)+&quot;\n } ddddddddddddddddddddddd&quot;);
		FileWriter test = new FileWriter(fileName,true);
		test.write(all.toString().replaceAll(&quot;, &quot;, &quot; &quot;).substring(1, all.toString().replaceAll(&quot;, &quot;, &quot; &quot;).length()-1) +  &quot;\n&quot; +  name + (name.endsWith(&quot;)&quot;)?&quot;&quot;:&quot; = &quot;)+ value+ (name.endsWith(&quot;)&quot;)?&quot;&quot;:&quot;;&quot;)+&quot;\n }&quot;);
		//test.flush();
		test.close();
	}

Another question : there is an other easy way to reach my goal ?

答案1

得分: 1

解决方法是 FileWriter 类应该有一个不同的路径,而不是 File.readAllLine() 使用的路径。<br>
因此,我们需要创建一个临时文件,然后将其复制到所需的 fileName 文件,然后我们删除临时文件。<br>
希望这能帮助需要它的人。它是有效的!!!

public static void fillFile(String fileName, String name, String value) throws IOException {
	Path path = Paths.get(fileName);
	List&lt;String&gt; all = null;
	if (path.toFile().exists()) { 
		all = Files.readAllLines(path, StandardCharsets.UTF_8);
	}
	if (all != null) {
		System.out.println(path + " the size of " + all.toArray(new String[all.size()]) + "");
	}
	Path path2 = Files.createTempFile(path.getParent(), "test-file", ".java");
	Files.write(path2, ((all + "").replaceAll(", ", "\n").replaceAll("\\[", "").replaceAll("\\]", "").substring(1, ((all + "").replaceAll(", ", " ").replaceAll("\\[", "").replaceAll("\\]", "")).length() - 1) + "\n" + name + value + "\n" + "}").getBytes());
	Files.copy(path2, path, StandardCopyOption.REPLACE_EXISTING);
	Files.deleteIfExists(path2);
}
英文:

The solution is that the FileWriter classe should have a different path that the one which File.readAllLine()use. <br>
So we have to create a temporary file, which will be copied to the desired fileName file, then we delete the temporary file <br>
Hope this will help people who need it. It is working !!!

public static void fillFile(String fileName, String name, String value) throws IOException {
		Path path = Paths.get(fileName);
		List&lt;String&gt; all = null;
		if (path.toFile().exists()) { 
			all = Files.readAllLines(path,StandardCharsets.UTF_8);}
		if(all!= null) System.out.println(path + &quot; the size of &quot;+ all.toArray(new String[all.size()])+&quot;&quot;);
		Path path2 = Files.createTempFile(path.getParent(),&quot;test-file&quot;, &quot;.java&quot;);
		Files.write(path2, ((all+&quot;&quot;).replaceAll(&quot;, &quot;, &quot;\n&quot;).replaceAll(&quot;\\[&quot;, &quot;&quot;).replaceAll(&quot;\\]&quot;, &quot;&quot;).substring(1, ((all+&quot;&quot;).replaceAll(&quot;, &quot;, &quot; &quot;).replaceAll(&quot;\\[&quot;, &quot;&quot;).replaceAll(&quot;\\]&quot;, &quot;&quot;)).length()-1) +  &quot;\n&quot; +  name + value+ &quot;\n&quot; +&quot;}&quot;).getBytes());
		Files.copy(path2, path, StandardCopyOption.REPLACE_EXISTING);
		Files.deleteIfExists(path2);
	}

huangapple
  • 本文由 发表于 2020年4月7日 08:17:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/61070902.html
匿名

发表评论

匿名网友

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

确定