[英]How to write and read file to temporary folder in windows
我正在使用graphviz將代碼解析為樹圖像。 您知道,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的操作系統的所有臨時目錄。
您可以這樣獲得os temp目錄路徑:
String tempPath = System.getProperty("java.io.tmpdir");
File tempFile = new File(tempPath + File.separator + "myTempFile.dat");
// todo read/write file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.