Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中将.txt文件附加到文本区域_Java_Swing_Awt_Java Io - Fatal编程技术网

在Java中将.txt文件附加到文本区域

在Java中将.txt文件附加到文本区域,java,swing,awt,java-io,Java,Swing,Awt,Java Io,我见过很多网站描述如何将文本添加到textarea,但是有没有办法从整个.txt文件中获取数据并显示在textarea中 我一直在玩弄各种各样的东西,把它们放在这样一条线上: outputTextArea.append(????); 但是还没有运气 对java非常陌生,术语不太好,但我希望我能很好地解释我的问题 编辑:它不会让我回答我自己的问题,所以我将把它放在这里 我正在使用JTextArea,但我想我有点不知所措。我不完全确定我看到了什么,但这就是你说的吗 public FileRead

我见过很多网站描述如何将文本添加到
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);

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

}
import java.util.Scanner;
导入java.io.*;
导入java.text.NumberFormat//用于数字格式设置的类
导入java.util.Locale//国家特定信息类
导入javax.swing.JOptionPane;
导入javax.swing.JTextArea;
公共利益3
{
公共静态void main(字符串[]args)引发IOException
{
双倍金额,//每年年末的存款金额
本金,//利息前的初始金额
利率;//利率
字符串输入;
字符串文件名;
扫描仪键盘=新扫描仪(System.in);
System.out.print(“输入文件名:”);
filename=keyboard.nextLine();
//为美元格式的货币创建NumberFormat
NumberFormat moneyFormat=NumberFormat.getCurrencyInstance(Locale.US);
//创建JTextArea以显示输出
JTextArea outputTextArea=新的JTextArea();
input=JOptionPane.showInputDialog(“请输入主体:”);
principal=Double.parseDouble(输入);
input=JOptionPane.showInputDialog(“请输入利率(格式:0.00)”);
速率=Double.parseDouble(输入);
outputExtArea.setText(“年\t存款金额\n”);
//打开新文件进行写入
PrintWriter outputFile=新的PrintWriter(文件名);
//计算每十年的存款金额
对于(int year=1;year,因为这不是一个“plz can I haz de codez”网站,我不会给出这样做的代码。相反,这里有几个要点:

  • 使用或类似的方法读取文件,并将内容放入字符串中
  • 然后,您可以按照建议将其附加到JTextArea的末尾
  • PS,您可能需要考虑和摆动而不是普通AWT。

    另见:


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

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


    因此,请自己阅读文本文件。请参阅。

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

    为什么不在将文本写入文件的同时将文本写入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); 
    
    FileWriter fwriter=newfilewriter(“BigMoneys.txt”,true);
    PrintWriter outputFile=新的PrintWriter(fwriter);
    
    对于(int year=1;year只关注“从文件中读取内容并将其放入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
    ,这样文件可以每行读取一行,而不是一次完整地读取,这实际上会导致更复杂的代码。

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

    太棒了。谢谢。我会调查的。为了回复,我不得不编辑我的原始帖子。我发誓,尽量不要听起来像个白痴。你不需要使用filewriter。你需要使用文件读取器读取文件,插入字符串,并将其附加到你的JTextArea中。或者我明白你想告诉我什么,我只是不知道怎么做开始吧。在我可以使用FileReader将循环数据放入字符串之前,我不是必须使用FileWriter将循环数据放入txt文件吗?如果您准备使用
    JEditorPane
    ,请参见。我只学了5周java,所以不,我认为我没有准备好,但我肯定会将其添加到书签中。它可能会在某个时候派上用场谢谢。问题不是关于JProgressBar的!我知道,但演示显示了一个文本区域,该区域由一个线程填充,该线程从外部资源读取文本。这是SwingSet2演示中的JProgressBarDemo。我应该更明确。哇,太棒了!这正是Ashkan一直在说的,但我不知道st不知道如何写,也不知道在哪里写。你是一个救生员。这并不完全正确,因为你的原始说明规定你先写文件,然后再读回文件(请参阅你对我答案的评论中的步骤2和步骤3)但是这个答案并没有从文件中读取回来。哦..是的。我想没有。我想我今晚没有睡觉。程序只是在请求文件名后停止。没有说它完成了,只是闪烁。好吧,我至少让它运行了,但它给了我这个消息:
    在线程“main”中输入文件名:asdf异常java.io.FileNotFoundException:asdf(系统找不到指定的文件)位于java.io.FileInputStream.open(本机方法)位于java.io.FileInputStream。(FileInputStream.java:120)位于java.io.FileReader。(FileReader.java:55)位于Interest3.main(Interest3.java:53)进程已完成。
    -1所有文本组件都支持读取(…)方法。还支持write()方法。这两个方法一起工作,以确保在
    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); }