首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Java中将.txt文件追加到TextArea中

在Java中将.txt文件追加到TextArea中
EN

Stack Overflow用户
提问于 2011-09-14 17:17:52
回答 6查看 6.5K关注 0票数 2

我见过很多描述如何将文本附加到textarea中的网站,但是有没有一种方法可以从整个.txt文件中抓取数据并将其显示在文本区域中?

我一直在尝试将各种东西放在这样的一行中:

代码语言:javascript
复制
outputTextArea.append(????);

但目前还不走运。

我对java非常陌生,对术语不是很在行,但我希望我的问题解释得很好。

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

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

代码语言:javascript
复制
public FileReader(String fileName);

到目前为止我已经掌握了这一点。

代码语言:javascript
复制
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中。老实说,我不是想让你帮我做作业,我只是真的真的迷路了。

因此,如果我应该使用上面的行,我可以这样做吗?

代码语言:javascript
复制
FileReader(String fwriter)

EDIT2:这就是我到目前为止所得到的。如果我走对了路,请告诉我。

代码语言:javascript
复制
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)",所以我显然遗漏了一些东西。

EDIT3:啊,我想我已经做到了!感谢大家的帮助。案例已结案,祝您好运:)

EN

回答 6

Stack Overflow用户

发布于 2011-09-14 17:23:25

由于这不是一个"plz can I haz de codez“网站,我不会给出这样做的代码。相反,这里有几点建议:

  1. 使用FileReader或类似的工具来读取文件,并将内容放入一个字符串中。
  2. 然后您可以按照建议将其附加到JTextArea的末尾。

PS你可能想要考虑JTextArea和Swing而不是普通的AWT。

另请参阅:

How to Use Text Areas

Character Streams

票数 3
EN

Stack Overflow用户

发布于 2011-09-14 19:47:16

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2011-09-14 17:26:16

读取文件并将其内容存储到字符串中,然后将该字符串附加到文本区域。

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

因此,您可以自己阅读文本文件。请参阅Java tutorial about character streams

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7413980

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档