英文:
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<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();
}
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<String> 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<String> 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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论