将StringBuilder附加到JTextArea

时间:2014-02-15 00:03:41

标签: java jframe stringbuilder jfilechooser

我想让自己成为一个文字处理器。我只熟悉JFileChooser的概念并将其实现到我的代码中。我希望能够使用JFileChooser打开文本文件并将文本文件的内容追加到JTextArea。为了尽力分离我的功能,我实现了MVC(模型视图控制器),所以我有3个类。该视图称为WordFrame.java,该模型称为DataStuff.java,控制器称为ProcessEvents.java。我的问题很简单,我可以将文本文件的内容打印到控制台,但尝试将所述内容附加到TextArea只会产生空白区域,下面的图像显示textArea上的位置,表示其字符间距,这是在加载文件之后。如上所述,控制台完美地打印出来。

enter image description here

另外,这里是模型和控制器的代码

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");
        }
    }
}

1 个答案:

答案 0 :(得分:2)

在第一次审核中,您似乎在每次循环迭代时创建一个新的字符串构建器,而不是重用它。您应该在启动while循环之前创建stringbuilder,并且在每次迭代时只需附加读取字符。

相关问题