我试图让自己成为一个文字处理器。我只熟悉JFileChooser的概念,并将其实现到我的代码中。我希望能够使用JFileChooser来打开文本文件并将文本文件的内容附加到JTextArea。为了尽我所能分离我的功能,我实现了MVC(模型视图控制器),所以我有3个类。该视图称为WordFrame.java,模型称为DataStuff.java,控制器称为ProcessEvents.java。我的问题很简单,我可以将文本文件的内容打印到控制台,但试图将所述内容附加到TextArea只会产生空白区域,下图显示了textArea上的位置,表示其字符间距,这是在文件加载后。如上所述,控制台可以完美地打印出来。将StringBuilder附加到JTextArea
也,这里是模型和控制器
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Scanner;
public class ProcessEvents {
private WordFrame frame = new WordFrame();
private DataStuff data = new DataStuff();
private DialogBoxes dialogs = new DialogBoxes();
private boolean fileSaved;
String fileName = "";
int fontSize = 0;
public ProcessEvents(WordFrame frame, DataStuff data){
this.frame = frame;
this.data = data;
this.frame.addListener(new wordProcessListener());
}
class wordProcessListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource().equals(frame.openMenuItem)){
frame.fileChooser.showOpenDialog(frame);
File f = frame.fileChooser.getSelectedFile();
System.out.println("Command Executed: open");
data.loadFile(f.getAbsoluteFile());
if(data.showText() != null){
String texts = data.showText().toString();
System.out.println(data.showText());
frame.textArea.append(data.showText().toString());
}
}
if(e.getSource().equals(frame.FontMenuItem)){
System.out.println("font");
}
if(e.getSource().equals(frame.exitMenuItem)){
dialogs.getConfirmDialog("exitWithoutSave");
}
if(e.getSource().equals(frame.saveMenuItem)){
dialogs.getSaveFileDialog();
data.saveFile("Bingo");
fileSaved = true;
}
if(e.getSource().equals(dialogs.close)){
if(!fileSaved){
}
}
}
}
}
这里的代码模型
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataStuff {
private String fileName = "";
private File file;
String text;
String name;
private File saveFile;
int counter = 0;
FileInputStream fis = null;
FileOutputStream fout = null;
StringBuilder sb;
char[] charArray;
int count = 0;
public void loadFile(File fileName){
this.file = fileName;
try{
fis = new FileInputStream(file);
charArray = new char[fis.read()];
while ((counter = fis.read()) != -1) {
System.out.print((char) counter);
sb = new StringBuilder();
sb.append(charArray);
}
}
catch(IOException ex){
ex.printStackTrace();
}
finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public StringBuilder showText(){
return sb;
}
public void saveFile(String name) {
this.name = name;
try{
fout = new FileOutputStream(saveFile+"/"+name+".txt");
}
catch(IOException e){
System.out.println("File failed to save or something went horribly wrong");
}
}
}
,并一如既往地支持总是赞赏。
编辑:所以我实际上在下面的人的帮助下修复了这个解决方案,我决定我不想替换上面的代码,以防万一别人在同一个问题上磕磕绊绊,可以看到差异,这里是视图类再次进行调整。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataStuff {
private String fileName = "";
private File file;
String text;
String name;
private File saveFile;
int counter = 0;
FileInputStream fis = null;
FileOutputStream fout = null;
StringBuilder sb = new StringBuilder(4096);
//char[] charArray = new char[4096];
int count = 0;
public void loadFile(File fileName){
this.file = fileName;
try{
fis = new FileInputStream(file);
// charArray = Character.toChars(fis.read());
while ((counter = fis.read()) != -1) {
System.out.print((char) counter);
sb.append((char) counter);
}
}
catch(IOException ex){
ex.printStackTrace();
}
finally {
try {
if (fis != null)
fis.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public StringBuilder showText(){
return sb;
}
public void saveFile(String name) {
this.name = name;
try{
fout = new FileOutputStream(saveFile+"/"+name+".txt");
}
catch(IOException e){
System.out.println("File failed to save or something went horribly wrong");
}
}
}
这个新代码的唯一问题是,你正在逐字节读取文件,这在性能方面并不是一个好主意。如果您使用阵列,您将减少磁盘访问次数,从而提高性能。 – vbazaga86
是的,我读过这个地方,我应该使用BufferedReader,我相信这就是它的名字。我会看看,谢谢,非常感谢。 –