java 检查文本文件是否存在且没有被覆盖

java check if text file exists without overwrite

我想检查一个文本文件是否存在,并设置一个PrintWriter写入其中。现在任何新的 PrintWriter 实例都会覆盖最后一个。 我的主要:

TextFileForStatus textFile = new TextFileForStatus();
int startingIndex = 1; 

try{
    String output = textFile.readIndex();
    System.out.println("In file: " + output);
    if(output == null)
    {
        textFile.writeToFile(Integer.toString(startingIndex));
    }
    else {
        System.out.println("output: " + output);
        startingIndex = Integer.parseInt(output);
    }
} catch(ReaderException RE){
    RE.getMessage().toString();
}catch(Exception EX){
    EX.getMessage().toString();
}

和我创建的class创建文件:

public class TextFileForStatus {

    private final String fileName = "status";
    private final String Format =  "UTF-8";
    private BufferedReader reader = null;
    private PrintWriter writer = null;

    public TextFileForStatus() throws FileNotFoundException, UnsupportedEncodingException {
        writer = new PrintWriter(fileName, Format);
        reader = new BufferedReader(new FileReader(fileName));
    }
    public void writeToFile(String currentStatus){
        writer.println(currentStatus);
        System.out.println("writer wrote: "+ currentStatus + " to file");
        writer.flush();
    }
    public String readIndex() throws IOException{
        String indexInFile = "";
        while((indexInFile = reader.readLine())!=null){
            indexInFile += reader.readLine();
        }
        return indexInFile;
    }
}

我可以使用已经存在的文本文件吗?

您可以使用new File(fileName).exists() 来检查文件是否存在。所以你可能想试试:

public class TextFileForStatus {

    private final String fileName = "status";
    private final String Format = "UTF-8";
    private BufferedReader reader = null;
    private PrintWriter writer = null;
    private boolean fileExists; // flag

    public TextFileForStatus() throws FileNotFoundException, UnsupportedEncodingException {
        fileExists = new File(fileName).exists();
        writer = new PrintWriter(fileName, Format);
        reader = new BufferedReader(new FileReader(fileName));
    }

    public void writeToFile(String currentStatus) {
        if (fileExists) {
            writer.println(currentStatus);
            System.out.println("writer wrote: " + currentStatus + " to file");
            writer.flush();
        }
    }

    public String readIndex() throws IOException {
        if (!fileExists) return "";

        String indexInFile = "";
        while ((indexInFile = reader.readLine()) != null) {
            indexInFile += reader.readLine();
        }
        return indexInFile;
    }
}

使用 JDK7 文件:

public void writeToFile(String path, String fileName, String status) throws Exception {
    String text = "writer wrote: "+ status + " to file";
    Path p = Paths.get(path, fileName);
    if (Files.isWritable(p)) { //checks for existence too
        Files.write(p, text.getBytes(), StandardOpenOption.APPEND); // see https://docs.oracle.com/javase/tutorial/essential/io/file.html#openOptions
    }
}

检查 OpenOption 写入选项。