打开Eclipse PDE视图中的文件。

huangapple go评论64阅读模式
英文:

Open a file in an Eclipse PDE View

问题

I have created an Eclipse PDE view, called SampleView. Currently, to programmatically display the output from my file on the view, I am consuming each line from the file, and printing to the view using a scanner. Is this the best way to display the file data? Or is there a better, existing function that I can use in my code to open the file in the view?

Code for SampleView:

public class SampleView extends ViewPart {

    /**
     * The ID of the view as specified by the extension.
     */
    public static final String ID = "asher.views.id.SampleView";

    @Inject IWorkbench workbench;

    @Override
    public void createPartControl(Composite parent) {
        
        Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
        
        File file = new File("/Users/user/Desktop/untitled.json");
        Scanner sc;
        
        try {
            sc = new Scanner(file);
            while (sc.hasNextLine()) 
                text.setText(text.getText() + "\n" + sc.nextLine()); 
            sc.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void setFocus() {
    }
}
英文:

I have created an Eclipse PDE view, called SampleView. Currently, to programmatically display the output from my file on the view, I am consuming each line from the file, and printing to the view using a scanner. Is this the best way to display the file data? Or is there a better, existing function that I can use in my code to open the file in the view?

Code for SampleView:

public class SampleView extends ViewPart {

	/**
	 * The ID of the view as specified by the extension.
	 */
	public static final String ID = "asher.views.id.SampleView";

	@Inject IWorkbench workbench;


	 

	@Override
	public void createPartControl(Composite parent) {
		
		Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
		
		 File file = new File("/Users/user/Desktop/untitled.json");
		 Scanner sc;
		
		try {
			sc = new Scanner(file);
			while (sc.hasNextLine()) 
			      text.setText(text.getText()+"\n"+sc.nextLine()); 
			sc.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void setFocus() {
	}
}

答案1

得分: 1

不使用Scanner进行读取,我建议采用此处描述的更清洁的方法:https://stackoverflow.com/questions/5868369/how-can-i-read-a-large-text-file-line-by-line-using-java

我还建议不要重复调用setText,而是简单地在当前文本上追加;而是使用StringBuilder,然后仅使用StringBuilder的结果调用setText

总的来说,代码会类似如下:

public class SampleView extends ViewPart {

    /**
     * The ID of the view as specified by the extension.
     */
    public static final String ID = "asher.views.id.SampleView";

    @Inject IWorkbench workbench;  

    @Override
    public void createPartControl(Composite parent) {
        Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
        StringBuilder builder = new StringBuilder("");
        try (Stream<String> stream = Files.lines(Paths.get("/Users/user/Desktop/untitled.json"));) {
            stream.forEach(line -> builder.append(line).append("\n"));
            text.setText(builder.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void setFocus() {
    }
}
英文:

Instead of reading using a Scanner, I'd recommend the cleaner approach described here: https://stackoverflow.com/questions/5868369/how-can-i-read-a-large-text-file-line-by-line-using-java

I'd also recommend not repeatedly calling setText and simply appending on the current text; instead, use a StringBuilder and simply call setText with the result of the StringBuilder.

All together, it would look something like this:

public class SampleView extends ViewPart {

    /**
     * The ID of the view as specified by the extension.
     */
    public static final String ID = &quot;asher.views.id.SampleView&quot;;

    @Inject IWorkbench workbench;  

    @Override
    public void createPartControl(Composite parent) {
        Text text = new Text(parent, SWT.READ_ONLY | SWT.V_SCROLL | SWT.H_SCROLL);
        StringBuilder builder = new StringBuilder(&quot;&quot;);
        try (Stream&lt;String&gt; stream = Files.lines(Paths.get(&quot;/Users/user/Desktop/untitled.json&quot;));) {
            stream.forEach(line -&gt; builder.append(line).append(&quot;\n&quot;));
            text.setText(builder.toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void setFocus() {
    }
}

huangapple
  • 本文由 发表于 2020年7月28日 06:05:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63124287.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定