Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Eclipse中保存通过主详细信息块修改的文本文件?_Java_Eclipse_Eclipse Plugin_Eclipse Rcp_Master Detail - Fatal编程技术网

Java 如何在Eclipse中保存通过主详细信息块修改的文本文件?

Java 如何在Eclipse中保存通过主详细信息块修改的文本文件?,java,eclipse,eclipse-plugin,eclipse-rcp,master-detail,Java,Eclipse,Eclipse Plugin,Eclipse Rcp,Master Detail,我创建了一个多页面编辑器,其中包含一个文本编辑器页面和一个主详细信息块页面。在主详细信息块中,我有一个列表,其中包含从文本文件解析的条目 这些文本文件具有以下结构: myVar = 23423423; SEARCH(myVar) block { text = test; } 当我单击其中一个条目时,例如myVar,将显示一个带有两个表单的详细信息块,一个用于变量名,一个用于对应值。如果我通过此表单更改条目,相应的对象将被修改。但是,它不会将多页编辑器标记为脏编辑器,也

我创建了一个多页面编辑器,其中包含一个文本编辑器页面和一个主详细信息块页面。在主详细信息块中,我有一个列表,其中包含从文本文件解析的条目

这些文本文件具有以下结构:

myVar = 23423423;
SEARCH(myVar)
block
   {
       text = test;
   }
当我单击其中一个条目时,例如
myVar
,将显示一个带有两个表单的详细信息块,一个用于变量名,一个用于对应值。如果我通过此表单更改条目,相应的对象将被修改。但是,它不会将多页编辑器标记为脏编辑器,也不会询问我是否要保存更改。greg-449,我需要手动保存文件,因此我尝试了以下方法:

public class MyParser {

    private MyModel model;
    private long lastModified;
    private IFile file;

    public MyParser(FileEditorInput input) {
        this.file = input.getFile();
        this.lastModified = input.getFile().getModificationStamp();
        parse();
    }

    private void parse() {
        try {
            InputStream in = file.getContents();
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(in));
            StringBuilder out = new StringBuilder();
            String line;
            List<MyField> variables = new ArrayList<MyField>();
            while ((line = reader.readLine()) != null) {
                String[] splittedLine = line.split("=");
                if (splittedLine.length >= 2)
                    variables
                            .add(new MyVariable(splittedLine[0], splittedLine[1]));
                   // Implement other entry types
            }
            System.out.println(out.toString()); // Prints the string content
                                                // read from input stream
            reader.close();
            this.setModel(new MyModel().add(variables, false));

        } catch (CoreException | IOException ex) {

        }
    }

    public void saveChanges() {
        FileWriter writer = null;
        try {
            writer = new FileWriter(file.getLocation().toFile());
            try {
                saveChanges(writer);
            } finally {
                writer.close();
            }
            if (file != null) {
                try {
                    file.refreshLocal(IResource.DEPTH_INFINITE,
                            new NullProgressMonitor());
                } catch (CoreException e) {

                }
            }
        } catch (IOException ex) {

        }
    }

    public void saveChanges(Writer writer) {
        //TODO
        this.getModel().setDirty(false);
    }

    public long getLastModified() {
        return this.lastModified;
    }

    public MyModel getModel() {
        return model;
    }

    public void setModel(MyModel model) {
        this.model = model;
    }
公共类MyParser{
私有MyModel模型;
私人长假;
私人IFile文件;
公共MyParser(FileEditorInput输入){
this.file=input.getFile();
this.lastModified=input.getFile().getModificationStamp();
parse();
}
私有void parse(){
试一试{
InputStream in=file.getContents();
BufferedReader reader=新的BufferedReader(
新的InputStreamReader(in);
StringBuilder out=新的StringBuilder();
弦线;
列表变量=新的ArrayList();
而((line=reader.readLine())!=null){
String[]splittedLine=line.split(“”);
如果(splittedLine.length>=2)
变量
.add(新的MyVariable(splittedLine[0],splittedLine[1]);
//实现其他条目类型
}
System.out.println(out.toString());//打印字符串内容
//从输入流读取
reader.close();
this.setModel(new MyModel().add(variables,false));
}捕获(CoreException | IOException ex){
}
}
公共void saveChanges(){
FileWriter=null;
试一试{
writer=newfilewriter(file.getLocation().toFile());
试一试{
保存更改(编写器);
}最后{
writer.close();
}
如果(文件!=null){
试一试{
file.refreshLocal(IResource.DEPTH\u无限,
新的NullProgressMonitor());
}捕获(COREE){
}
}
}捕获(IOEX异常){
}
}
公共void保存更改(编写器){
//待办事项
this.getModel().setDirty(false);
}
公共长getLastModified(){
返回此.lastModified;
}
公共MyModel getModel(){
收益模型;
}
公共void集合模型(MyModel){
this.model=模型;
}

如何将修改后的对象保存到原始文件中?是否需要完全重写文件或是否可以更改脏值?此外,文本文件中的条目顺序很重要。是否需要记住行号,因为在中间添加新条目可能是个问题。文件


非常感谢您的帮助。

您必须替换文件的全部内容。请使用
IFile.setContents
方法设置现有文件的内容。正确的内容顺序由您决定

对于
FormEditor
调用
editordystatechanged()
告知编辑器“脏”状态已更改。每个
formpage
应根据需要实现
isDirty
(或覆盖
FormEditor
isDirty
方法)


如果脏状态正确,编辑器标题中会添加一个“*”。如果编辑器脏,退出时也会显示一个“是否要保存”对话框。

谢谢您提供的信息。我如何在用户修改字段后告诉eclipse可以保存文件(通常在选项卡旁边有一个星号)?此外,如果用户修改了条目并关闭了编辑器,则应打开一个弹出窗口,如何执行此操作?添加了脏状态详细信息。