我想知道,在Java中,是否有可能将FileWriter中使用的File对象或至少是文件名作为一个字符串来提取?我看了一下文档,据我所知,这是不可能的,但我在这方面经常出错。
如果能得到帮助,我将非常感激
你可以扩展从 FileWriter
类,为filefilename提供一个getter。
public class MyFileWriter extends FileWriter {
private File file;
private boolean append = false;
public MyFileWriter(File file) throws IOException {
this(file, false);
}
public MyFileWriter(File file, boolean append) throws IOException {
super(file, append);
this.file = file;
this.append = append;
}
public MyFileWriter(String fileName) throws IOException {
this(new File(fileName));
}
public MyFileWriter(String fileName, boolean append) throws IOException {
this(new File(fileName), append);
}
// getters/setters
}