Java FileDialog 浏览和读取文件
Java FileDialog browse and read from file
我有一个使用 FileDialog 浏览 file/s 的按钮。我需要从我选择的文件中读取所有行,我已经尝试了很多东西但对我没有任何作用 :( 我有另一个按钮,当我按下它时,它会读取文件而不是打印它。
Button btnBrowse = new Button(composite_2, SWT.NONE);
btnBrowse.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FileDialog fileBrowse = new FileDialog(pmComp, SWT.MULTI);
fileBrowse.setFilterExtensions(new String[] {"*.txt"});
String filePath = fileBrowse.open();
if (filePath != null) {
StringBuffer buf = new StringBuffer();
files = fileBrowse.getFileNames();
for (int i = 0, n = files.length; i < n; i++) {
buf.append(fileBrowse.getFilterPath());
if (buf.charAt(buf.length() - 1) != File.separatorChar) {
buf.append(File.separatorChar);
}
buf.append(files[i]);
buf.append("\n");
}
for (int i = 0, n = files.length; i < n; i++) {
listViewer.add(files[i]);
}
System.out.println(buf);
}
}
});
查看示例代码:
这里 btnBrowseFile
是我们需要 select 文件
的按钮
this.btnBrowseFile.addListener(SWT.Selection, event -> {
final Shell shell = this.getShell();
final FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setText("Select valid text file");
dialog.setFilterExtensions("*.txt");
String selectedFileStr = dialog.open();
if (selectedFileStr != null && !selectedFileStr.isEmpty()) {
final Stream<String> lines = Files.lines(Paths.get(selectedFileStr), StandardCharsets.UTF_8);
List<String> readList = lines.collect(Collectors.toList());
lines.close();
readList.forEach(line -> {
System.out.println(line);
// Do your work
});
}
});
我有一个使用 FileDialog 浏览 file/s 的按钮。我需要从我选择的文件中读取所有行,我已经尝试了很多东西但对我没有任何作用 :( 我有另一个按钮,当我按下它时,它会读取文件而不是打印它。
Button btnBrowse = new Button(composite_2, SWT.NONE);
btnBrowse.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FileDialog fileBrowse = new FileDialog(pmComp, SWT.MULTI);
fileBrowse.setFilterExtensions(new String[] {"*.txt"});
String filePath = fileBrowse.open();
if (filePath != null) {
StringBuffer buf = new StringBuffer();
files = fileBrowse.getFileNames();
for (int i = 0, n = files.length; i < n; i++) {
buf.append(fileBrowse.getFilterPath());
if (buf.charAt(buf.length() - 1) != File.separatorChar) {
buf.append(File.separatorChar);
}
buf.append(files[i]);
buf.append("\n");
}
for (int i = 0, n = files.length; i < n; i++) {
listViewer.add(files[i]);
}
System.out.println(buf);
}
}
});
查看示例代码:
这里 btnBrowseFile
是我们需要 select 文件
this.btnBrowseFile.addListener(SWT.Selection, event -> {
final Shell shell = this.getShell();
final FileDialog dialog = new FileDialog(shell, SWT.OPEN);
dialog.setText("Select valid text file");
dialog.setFilterExtensions("*.txt");
String selectedFileStr = dialog.open();
if (selectedFileStr != null && !selectedFileStr.isEmpty()) {
final Stream<String> lines = Files.lines(Paths.get(selectedFileStr), StandardCharsets.UTF_8);
List<String> readList = lines.collect(Collectors.toList());
lines.close();
readList.forEach(line -> {
System.out.println(line);
// Do your work
});
}
});