只有三个要点: 1,使用org.eclipse.swt.widgets.FileDialog选取文件 2,FileInputStream读文件 3,org.eclipse.swt.custom.StyledText(当然了,其他的控件也OK)输出文件代码: package cn.com.swt;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException;import org.eclipse.swt.SWT; import org.eclipse.swt.custom.StyledText; import org.eclipse.swt.events.SelectionAdapter; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.widgets.Shell;public class WriteFileAfterRead { public void open() { final Shell shell = new Shell(SWT.CLOSE); shell.setLayout(new FillLayout()); Composite composite = new Composite(shell, SWT.NONE); composite.setLayout(new GridLayout(2, false)); Button referBtn = new Button(composite, SWT.NONE);// use to open a file dialog referBtn.setText("Refer"); final StyledText outText = new StyledText(composite, SWT.MULTI | SWT.WRAP);// use to display file content outText.setLayoutData(new GridData(GridData.FILL_BOTH)); referBtn.addSelectionListener(new SelectionAdapter() { public void widgetSelected(SelectionEvent arg0) { // TODO Auto-generated method stub FileDialog fileDialog = new FileDialog(shell); String fileIn = fileDialog.open();// get selected file path // read file content with FileInputStream,output content to screen if (null != fileIn) { FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(fileIn); byte[] bufferBytes = new byte[256]; StringBuffer buffer = new StringBuffer(); while ((fileInputStream.read(bufferBytes)) != -1) { buffer.append(new String(bufferBytes)); } outText.setText(buffer.toString()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fileInputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }); shell.setSize(600, 400); shell.setText("Read File"); shell.layout(); shell.open(); Display display = Display.getDefault(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); } public static void main(String[] args) { new WriteFileAfterRead().open(); } }
楼上的代码会报这个错误Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source) at java.lang.AbstractStringBuilder.append(Unknown Source) at java.lang.StringBuffer.append(Unknown Source)
是文件太大的时候出OutOfMemoryError吧!不是没次都出吧 widgetSelected(){}里面用下面的代码替换掉试一下. public void widgetSelected(SelectionEvent arg0) { // TODO Auto-generated method stub FileDialog fileDialog = new FileDialog(shell); String fileIn = fileDialog.open();// get selected file path // read file content with FileInputStream,output content to screen if (null != fileIn) { BufferedReader fileReader = null; try { fileReader = new BufferedReader(new FileReader(fileIn)); StringBuffer buffer = new StringBuffer(); String line = null; while (( line = fileReader.readLine()) != null) { buffer.append(line); } outText.setText(buffer.toString()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { fileReader.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
1,使用org.eclipse.swt.widgets.FileDialog选取文件
2,FileInputStream读文件
3,org.eclipse.swt.custom.StyledText(当然了,其他的控件也OK)输出文件代码:
package cn.com.swt;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;public class WriteFileAfterRead { public void open() {
final Shell shell = new Shell(SWT.CLOSE);
shell.setLayout(new FillLayout()); Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new GridLayout(2, false)); Button referBtn = new Button(composite, SWT.NONE);// use to open a file dialog
referBtn.setText("Refer"); final StyledText outText = new StyledText(composite, SWT.MULTI
| SWT.WRAP);// use to display file content
outText.setLayoutData(new GridData(GridData.FILL_BOTH));
referBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
FileDialog fileDialog = new FileDialog(shell);
String fileIn = fileDialog.open();// get selected file path // read file content with FileInputStream,output content to screen
if (null != fileIn) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(fileIn);
byte[] bufferBytes = new byte[256];
StringBuffer buffer = new StringBuffer();
while ((fileInputStream.read(bufferBytes)) != -1) {
buffer.append(new String(bufferBytes));
}
outText.setText(buffer.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}); shell.setSize(600, 400);
shell.setText("Read File");
shell.layout();
shell.open();
Display display = Display.getDefault();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
} public static void main(String[] args) {
new WriteFileAfterRead().open();
}
}
at java.util.Arrays.copyOf(Unknown Source)
at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source)
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuffer.append(Unknown Source)
widgetSelected(){}里面用下面的代码替换掉试一下. public void widgetSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
FileDialog fileDialog = new FileDialog(shell);
String fileIn = fileDialog.open();// get selected file path // read file content with FileInputStream,output content to screen
if (null != fileIn) {
BufferedReader fileReader = null;
try {
fileReader = new BufferedReader(new FileReader(fileIn));
StringBuffer buffer = new StringBuffer();
String line = null;
while (( line = fileReader.readLine()) != null) {
buffer.append(line);
}
outText.setText(buffer.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
点击button打开文件打开对话框,但是没有open而直接关闭对话框,程序整个退出,该怎么办?
是不是还得来个try catch?
import java.io.FileNotFoundException;
import java.io.IOException;import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;public class WriteFileAfterRead { public void open() {
final Shell shell = new Shell(SWT.CLOSE);
shell.setLayout(new FillLayout()); Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new GridLayout(2, false)); Button referBtn = new Button(composite, SWT.NONE);// use to open a file dialog
referBtn.setText("Refer"); final StyledText outText = new StyledText(composite, SWT.MULTI
| SWT.WRAP);// use to display file content
outText.setLayoutData(new GridData(GridData.FILL_BOTH));
referBtn.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent arg0) {
// TODO Auto-generated method stub
FileDialog fileDialog = new FileDialog(shell);
String fileIn = fileDialog.open();// get selected file path // read file content with FileInputStream,output content to screen
if (null != fileIn) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(fileIn);
byte[] bufferBytes = new byte[256];
StringBuffer buffer = new StringBuffer();
while ((fileInputStream.read(bufferBytes)) != -1) {
buffer.append(new String(bufferBytes));
}
outText.setText(buffer.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
fileInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}); shell.setSize(600, 400);
shell.setText("Read File");
shell.layout();
shell.open();
Display display = Display.getDefault();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
display.dispose();
} public static void main(String[] args) {
new WriteFileAfterRead().open();
}
}