即使不使用像FileUtils这样的现有库,也可以很容易地实现这一点。
这三种方法可以为你的文本对象
private File getExcelFile(final File txtFile) throws IOException {
final String path = txtFile.getCanonicalPath();
final String directory = path.substring(0, path.lastIndexOf(System.getProperty("file.separator")));
return new File(getExcelSubdirectory(directory), getExcelFilename(txtFile.getName()));
}
private File getExcelSubdirectory(final String parent) {
return new File(parent, "excelFiles");
}
private static String getExcelFilename(final String filename) {
return filename.substring(0, filename.lastIndexOf('.')) + ".xls";
}
创建相应的Excel File
对象。如果你使用它们像这样:
File txt = new File("C:/aaa/bbb/ccc/ddd/test.txt");
System.out.println(txt.getCanonicalPath());
File excel = getExcelFile(txt);
System.out.println(excel.getCanonicalPath());
..它会打印:
C:\aaa\bbb\ccc\ddd\test.txt
C:\aaa\bbb\ccc\ddd\excelFiles\test.xls
来源
2014-10-17 12:52:19
Tom
'fileName'看起来像什么? – fabian 2014-10-17 12:36:25
哦,我刚刚明白我的错误。我把absolutePath放在fileName中。这就是为什么我有一些麻烦!感谢您的帮助球员:) – 2014-10-17 12:50:20