看《Thinking in JAVA》的时候,发现源代码的/net/mindview/util/TextFile.java里面的getAbsoluteFile()是否有用。
代码片段是:
public class FileUtil { public static void write(String fileName, String text) { try { PrintWriter out = new PrintWriter( new File(fileName).getAbsoluteFile()); // PrintWriter out = new PrintWriter( // new File(fileName)); try { out.print(text); } finally { out.close(); } } catch(IOException e) { throw new RuntimeException(e); } } public static void main(String[] args) { write("a.txt","HelloWorld!"); write("a\\b.txt","HelloWorld!"); } }
我现在看到的好处就是在文件名字是包括路径的文件,且路径不存在是,有getAbsoluteFile的时候报告是带有全路径的Exception,如果没有getAbsoluteFile,报告的相对路径的Exception。
还有其他有意吗???