刷新文档

时间:2014-06-01 19:29:41

标签: java text text-editor

我有一个写/读.txt文件程序,我将放在底部,所以我可以先问我的问题:通常,如果我打开文件并运行程序,我需要关闭.txt文件并重新打开它以查看程序所做的更改。有没有办法刷新程序中的文档,以便它实时在我眼前改变?这是程序:

package SingleThings;

import java.io.*;

public class TextUtil {
    public String path;
    public String[] lines;

    public TextUtil(String pathy,int length){
        path=pathy;
        makeFile();
        lines = new String[length];
        for(int i = 0;i<length;i++){
            lines[i]=getLine(i+1);
        }
    }


    public void clear(){
        try{
            FileWriter write = new FileWriter(path , false);
        }catch(IOException e){
            System.out.println("There was a problem oh noesss " + e);
        }
    }

    private void makeFile(){
        try {
            File file = new File(path);
            if (file.createNewFile()){
                System.out.println("Creating file...");
            }else{
                System.out.println("Loading file...");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getLine(int line){
        BufferedReader in;
        String read = null;
        int linenum = line;
        try {
            in = new BufferedReader(new FileReader(path));
            while(linenum > 0){
                read = in.readLine();
                linenum--;
            }
            in.close();
            return read;
        }catch(IOException e){
            System.out.println("There was a problem:" + e);
            return "error";
        }
    }

    private void write(String text){
        BufferedWriter out;
        try {
            out = new BufferedWriter(new FileWriter(path,true));
            out.write(text);
            out.newLine();
            out.close();
        }catch(IOException e){
            System.out.println("There was a problem:" + e);
        }
    }

    public void update(){
        clear();
        for(int i=0;i<lines.length;i++){
            if(lines[i]!=null){
                write(lines[i]);
            }else{
                write("");
            }
        }
    }
}

0 个答案:

没有答案