将.txt文件附加到Java中的TextArea

时间:2011-09-14 09:17:52

标签: java swing awt java-io

我见过很多网站描述如何将文字追加到textarea,但有没有办法从整个.txt文件中获取数据并将其显示在textarea中?

我一直在玩各种各样的东西,如下所示:

outputTextArea.append(????);

但是没有运气。

java的新手并没有那么好的术语,但我希望我能很好地解释我的问题。


编辑:我不会让我回答我自己的问题,所以我只想把它放在这里。

我正在使用JTextArea,但我想我有点不知所措。我不完全确定我所看到的是什么,但这就是你在说什么?

public FileReader(String fileName);

到目前为止,我已经知道了。

FileWriter fwriter = new FileWriter("BigMoneys.txt", true);
PrintWriter outputFile = new PrintWriter(fwriter);

for (int year = 1; year<= 10; year++)
{
    amount = principal * Math.pow(1.0 + rate, year);
    outputFile.append( year + "\t" + moneyFormat.format(amount) + "\n");

}

outputFile.close();

JOptionPane.showMessageDialog(null, outputTextArea, "Compound Interest", JOptionPane.INFORMATION_MESSAGE);

System.exit(0);

其余的内容在我的教科书中得到了相当好的介绍,并且所有内容都很有道理,但我只是检查了一下,这本书对FileReader一无所知。

我所知道的是我应该使用outputFile中的内容并将其附加到outputTextArea。老实说,我不是想让你为我做功课,我真的真的输了。

所以,如果我应该使用上面那行,我可以这样做吗?

FileReader(String fwriter)


EDIT2:这是我到目前为止所得到的。如果我走在正确的轨道上,请告诉我。

import java.util.Scanner;
import java.io.*;
import java.text.NumberFormat;  //class for numeric formatting
import java.util.Locale;        //class for country-specific information
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

public class Interest3
{
    public static void main(String[] args) throws IOException
    {
        double amount,  //amount of deposit at end of each year
            principal,  //initial amount before interest
            rate;       //rate of interest
        String input;
        String filename;

        Scanner keyboard = new Scanner(System.in);

        System.out.print("Enter the filename: ");
        filename = keyboard.nextLine();

        //create NumberFormat for currency in US dollar format
        NumberFormat moneyFormat = NumberFormat.getCurrencyInstance( Locale.US );

        //create JTextArea to display output
        JTextArea outputTextArea = new JTextArea();

        input = JOptionPane.showInputDialog("Please enter Principal: ");
        principal = Double.parseDouble(input);

        input = JOptionPane.showInputDialog("Please enter Interest Rate (Format: 0.00) ");
        rate = Double.parseDouble(input);

        outputTextArea.setText("Year\tAmount on deposit\n");

        //open new file for writing
        PrintWriter outputFile = new PrintWriter(filename);

        //calculate amount on deposit for each of ten years
        for (int year = 1; year<= 10; year++)
        {
            amount = principal * Math.pow(1.0 + rate, year);

            // append one line of text to outputTextArea
            outputFile.append( year + "\t" + moneyFormat.format(amount) + "\n");

        }

        outputFile.close();

        //open file for reading
        File file = new File(filename);
        FileReader rd = new FileReader(file);

        outputTextArea.append(rd);

        //display results
        JOptionPane.showMessageDialog(null, outputTextArea, "Compound Interest", JOptionPane.INFORMATION_MESSAGE);

        System.exit(0);
    }

}

我在outputTextArea.append(rd);的行上出现错误,说javax.swing.JTextArea中的“append(java.lang.String)无法应用于(java.io.FileReader)”,所以我显然在那里遗漏了一些东西。


编辑3:Aaaand我想我已经知道了!谢谢大家的帮助。结案,晚安:)

6 个答案:

答案 0 :(得分:3)

由于这不是“plz can I haz de codez”网站,我不会为此做出代码。相反,这里有几点建议:

  1. 使用FileReader或类似内容来读取文件,并将内容放入字符串中。
  2. 然后您可以按照建议将其附加到JTextArea的末尾。
  3. PS你可能想要考虑JTextArea和Swing而不是普通的AWT。

    另见:

    How to Use Text Areas

    Character Streams

答案 1 :(得分:2)

为什么不在将文本写入文件的同时将文本写入JTextArea:

FileWriter fwriter = new FileWriter("BigMoneys.txt", true);
PrintWriter outputFile = new PrintWriter(fwriter);
for (int year = 1; year<= 10; year++) {
    amount = principal * Math.pow(1.0 + rate, year);
    String line = year + "\t" + moneyFormat.format(amount) + "\n";
    outputTextArea.append(line);
    outputFile.append(line);
}
outputFile.close();
JOptionPane.showMessageDialog(null, outputTextArea, "Compound Interest", JOptionPane.INFORMATION_MESSAGE);
System.exit(0); 

这应该有用。

但是请注意,这不是设计应用程序的最佳方式,这需要将应用程序分层,但我不想进入这个,因为你显然不够了解关于Java在这方面走得更远。

答案 2 :(得分:1)

读取文件并将其内容存储到String中,然后将String追加到文本区域。

你不会在(J)TextArea中找到任何快捷方法来为你读取文件,因为这不是他的事。文本可以来自文件,数据库,套接字连接或其他任何地方。从所有这些潜在位置阅读文本不是文本区域的责任。它的职责是显示您提供的文本。

所以请自己阅读文本文件。请参阅Java tutorial about character streams

答案 3 :(得分:1)

只关注“从文件中读取内容并将其放入JTextArea”的问题,这就是您所需要的:

//open file for reading
File file = new File(filename);
BufferedReader rd = new BufferedReader(new FileReader(file));
String line;
while ((line = rd.readLine()) != null)
    outputTextArea.append(line + "\n");
rd.close();
//display results
JOptionPane.showMessageDialog(null, outputTextArea, "Compound Interest", JOptionPane.INFORMATION_MESSAGE);

请注意,我起诉BufferedReader,以便文件可以每行读取一行,而不是一次完全读取,这实际上会导致更复杂的代码。

答案 4 :(得分:1)

您可以使用EditorKit为您执行此操作:

JTextArea edit = new JTextArea(...);
...
try
{
    FileReader reader = new FileReader( "TextAreaLoad.txt" );
    BufferedReader br = new BufferedReader(reader);
    EditorKit kit = edit.getUI().getEditorKit(edit);
    Document doc = edit.getDocument();
    kit.read(br, doc, doc.getLength());
    br.close();
}
catch(Exception e2) { System.out.println(e2); }

答案 5 :(得分:-1)

JDK中包含一个JProgressBar演示(文件夹demo / jfc / SwingSet2),它似乎与你想要的一致。还提供了源代码。

相关问题