从Java GUI中的文件中读取多行

时间:2018-03-26 15:22:21

标签: java swing user-interface bufferedreader filereader

我是一名学生,当我将程序读入包含GUI的程序时,我的程序读取文本文件中的所有行,我遇到了麻烦,它只会显示最后一行。我让它在控制台上工作,它运行良好并显示每一行。有人可以帮忙吗?

这是在Console中运行的代码

    String parent = "D:\\Online Class\\Java Programs\\Final Project\\Tests\\";
    Scanner fileInput = new Scanner(System.in);

    System.out.print("Enter Name of File: ");
    String name= fileInput.nextLine();
    String extension = ".txt";
    String fileName = (parent+name+extension);

    //reads file and displays it
    String line = null;

    try{
        FileReader fileReader = new FileReader(fileName);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) !=null)
        {
            System.out.println(line);
        }
        bufferedReader.close();
    }

    catch(FileNotFoundException ex)
    {
        System.out.println("Unable to open file '" + fileName + "'");
    }
    catch(IOException ex)
    {
        System.out.println("Error Reading file '"+fileName + "'");
    }
}

以下是使用GUI

将其输入程序时无效的代码
//search for file (must be in same folder as program)
    String parent = "D:\\Online Class\\Java Programs\\Final Project\\Tests\\";
    String fileInput = taskNameTextField.getText();
    String name = fileInput;
    String extension = ".txt";

    String fileName = (parent+name+extension);

    //reads file and displays it
    String line = null;

    try{
        FileReader fileReader = new FileReader(fileName);

        BufferedReader bufferedReader = new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) !=null)
        {
            System.out.println(line);
        }
        bufferedReader.close();
    }

    catch(FileNotFoundException ex)
    {
        System.out.println("Unable to open file '" + fileName + "'");
    }
    catch(IOException ex)
    {
        System.out.println("Error Reading file '"+fileName + "'");
    }
}

以下是正在执行的GUI的完整代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import javax.swing.*;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.File;
import java.io.*;
import java.util.Scanner;

public class readGUItest implements ActionListener
{

    JFrame window = new JFrame("Task");
    JTextField taskNameTextField = new JTextField();
    JTextArea DescriptTextArea = new JTextArea();
    JLabel taskNameLabel = new JLabel("Task Name");
    JLabel descriptLable = new JLabel("Description");
    JButton save = new JButton("save");



    public static void main(String[] args)throws IOException
    {
        readGUItest tGt = new readGUItest();
    }

    public readGUItest()
    {

        window.add(taskNameLabel);
        window.add(taskNameTextField);
        window.add(descriptLable);
        window.add(DescriptTextArea);
        window.add(save);
        save.addActionListener(this);

        window.setSize(new Dimension(200,300));
        window.setLayout(new GridLayout(3,3));
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setVisible(true);
    }



    public void actionPerformed(ActionEvent event)
    {
        //search for file (must be in same folder as program)
        String parent = "D:\\Online Class\\Java Programs\\Final Project\\Tests\\";
        String fileInput = taskNameTextField.getText();
        String name = fileInput;
        String extension = ".txt";

        String fileName = (parent+name+extension);

        String line = null;

        try{
            FileReader fileReader = new FileReader(fileName);

            BufferedReader bufferedReader = new BufferedReader(fileReader);

            while((line = bufferedReader.readLine()) !=null)
            {
                DescriptTextArea.setText(line);
            }
            bufferedReader.close();
        }

        catch(FileNotFoundException ex)
        {
            DescriptTextArea.setText("Unable to open file '" + fileName + "'");
        }
        catch(IOException ex)
        {
            DescriptTextArea.setText("Error Reading file '"+fileName + "'");
        }
    }

}

2 个答案:

答案 0 :(得分:0)

您在setText上使用DescriptTextArea,它取代了小部件的先前内容。

您更愿意使用JTextField的文档,而不是像这样:

DescriptTextArea.getDocument().insertString(
    DescriptTextArea.getDocument().getEndPosition().getOffset(),
    line + System.getProperty("line.separator"), 
    null);

这会在文档末尾插入新行并附加行分隔符。

答案 1 :(得分:0)

JTextArea有一个默认情况下以纯文本形式读取文件的方法,这应该对你有用

public static bool IsBaselined
{
    get { return ((bool)(HttpContext.Current.Profile.GetPropertyValue("IsBaselined"))); }
    set { HttpContext.Current.Profile.SetPropertyValue("IsBaselined", value); }
}

http://www.java2s.com/Code/JavaAPI/javax.swing/JTextAreareadReaderinObjectdesc.htm

https://docs.oracle.com/javase/7/docs/api/javax/swing/text/JTextComponent.html#read(java.io.Reader,%20java.lang.Object)