英文:
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 = "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() {
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论