gpt4 book ai didi

java - 如何在Windows中向临时文件夹写入和读取文件

转载 作者:行者123 更新时间:2023-12-01 12:39:14 26 4
gpt4 key购买 nike

我正在使用 graphviz 来解析 TreeMap 像的代码。要知道,Graphviz 就是读取一个文件,然后写入文件图像。那么,如何将该文件写入Windows中的临时文件夹,然后再次读取该文件。这是我的代码:

     DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(tree);

String OS = System.getProperty("os.name");
Runtime rt = Runtime.getRuntime();


try {

File file = new File("D:\\workspace\\output.dot"); // create a file
BufferedWriter bw = new BufferedWriter(new FileWriter(file.getAbsoluteFile()));
bw.write(st2);
bw.close();


String dotPath = "C:\\Program Files (x86)\\Graphviz2.38\\bin\\dot.exe";
String fileInputPath = "D:\\workspace\\output.dot";
String fileOutputPath = "D:\\workspace\\treeImage.png";
String tParam = "-Tpng";
String tOParam = "-o";

String[] cmd = new String[5];
cmd[0] = dotPath;
cmd[1] = tParam;
cmd[2] = fileInputPath;
cmd[3] = tOParam;
cmd[4] = fileOutputPath;

rt.exec(cmd);

} catch (IOException ex) {
System.out.println("Failed to write to file");
}
}

最佳答案

使用File.createTempFile,这将正确处理具有 JVM 的所有操作系统的所有临时目录。

关于java - 如何在Windows中向临时文件夹写入和读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25278852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com