Java 在FileWriter中获取文件名

问题描述 投票:0回答:1

我想知道,在Java中,是否有可能将FileWriter中使用的File对象或至少是文件名作为一个字符串来提取?我看了一下文档,据我所知,这是不可能的,但我在这方面经常出错。

如果能得到帮助,我将非常感激

java arrays file filenames filewriter
1个回答
1
投票

你可以扩展从 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
}
© www.soinside.com 2019 - 2024. All rights reserved.