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

发布于 2024-12-04 09:26:14 字数 3473 浏览 2 评论 0原文

我见过很多网站描述如何将文本附加到 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)”,所以我显然错过了那里的一些东西。


EDIT3:啊啊我想我已经明白了!感谢大家的帮助。案件已结,晚安:)

I've seen a lot of websites that describe how to append text into a textarea, but is there a way to grab data from a whole .txt file and display it in the textarea?

I've been playing around with various things to put into a line like this:

outputTextArea.append(????);

But no luck yet.

Super new to java and not so good with the terminology but I hope I explained my question well.

EDIT: It won't let me respond to my own question, so I'm just going to put it up here.

I am using JTextArea, but I guess I'm a little overwhelmed. I'm not entirely sure what I'm seeing, but is this what you were talking about?

public FileReader(String fileName);

I've got this so far.

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

The rest of this was covered in my textbook fairly well, and all makes decent sense, but I just checked and the book has nothing about FileReader.

All I know is I'm supposed to use what's in outputFile and append it to outputTextArea. I'm honestly not trying to get you to do my homework for me, I'm just really really lost.

So if I am supposed to use that line above, could I do this?

FileReader(String fwriter)

EDIT2: This is what I've got so far. Please tell me if I'm on the right track.

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

}

I've got an error on the line with outputTextArea.append(rd); that says "append(java.lang.String) in javax.swing.JTextArea cannot be applied to (java.io.FileReader)", so I'm obviously missing something down there.

EDIT3: Aaaand I think I've got it! Thanks for everyone's help. Case closed, goodnight :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

澜川若宁 2024-12-11 09:26:14

由于这不是一个“请问我可以使用代码吗”网站,我不会提供执行此操作的代码。相反,这里有一些提示:

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

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

另请参阅:

如何使用文本区域

字符流

As this is not a "plz can I haz de codez" site I am not going to give code for doing that. Instead, here's a couple of pointers:

  1. Use FileReader or something similar to read the file, and put the contents into a string.
  2. You can then append it to the end of your JTextArea as suggested.

PS you may want to consider JTextArea and Swing instead of plain AWT.

Also see:

How to Use Text Areas

Character Streams

总攻大人 2024-12-11 09:26:14

为什么不在将文本写入文件的同时将文本写入 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 的了解还不够。朝那个方向走得更远。

Why don't you write the text to your JTextArea at the same time you write it to a File:

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

That shall work.

Please note, however, that this would not be the best way to design an application, which would require to split your applications in layers, but I don't want to enter into this, because you obviously don't know enough about Java to go further in that direction.

任性一次 2024-12-11 09:26:14

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

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

因此请自行阅读文本文件。请参阅有关字符流的 Java 教程

Read the file and store its content into a String, and then append the String to the text area.

You won't find any shortcut method in (J)TextArea to read the file for you, because that's not his business. The text could come from a file, a database, a socket connection or anywhere else. That's not the responsibility of the text area to read the text from all these potential locations. Its responsibility is to display the text you give it.

So read the text file yourself. See the Java tutorial about character streams.

深海不蓝 2024-12-11 09:26:14

仅关注“从文件中读取内容并将其放入 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 以便可以逐行读取文件,而不是而不是一次性读完它,这实际上会导致代码更复杂。

Focusing only on the problem "reading content from a file and put it into a JTextArea", here is what you need:

//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);

Note that I sue a BufferedReader so that the file can be read line per line, rather than reading it completely in one time, which in fact would lead to more complicated code.

扮仙女 2024-12-11 09:26:14

您可以使用 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); }

You can use the EditorKit to do this for you:

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); }
亢潮 2024-12-11 09:26:14

JDK 中包含一个 JProgressBar 演示(文件夹 demo/jfc/SwingSet2),它似乎符合您的需求。还提供了源代码。

There is a JProgressBar demo included in the JDK (folder demo/jfc/SwingSet2) which seems correspond to what you want. Source code is provided also.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文