简体   繁体   English

为什么我的代码没有打印出txt文件的所有行?

[英]Why doesn't my code print out all lines of the txt file?

So, i have this code that is supposed to read back the text from "Questions.txt" and display it in JTextArea with the option to save any edits but it only reads back the last line. 因此,我有这段代码应该从“ Questions.txt”中读取文本,并在JTextArea中显示它,并选择保存所有编辑,但它只读取最后一行。 Whats wrong with it? 它出什么问题了?

package secondgui;

public class MainGUI {
public static void main (String [] args) {
    SecondGUI phillip = new SecondGUI(); 
    phillip.repaint();

}
}




 package secondgui;

 import java.awt.event.*;
 import java.io.FileWriter;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.util.Scanner;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 import javax.swing.*;

 public class SecondGUI extends JFrame implements ActionListener {

/**
 * @param args the command line arguments
 */
private JButton save = new JButton("Save Edits");
private JTextArea edit = new JTextArea();

public SecondGUI() {
    this.setVisible(true);
    this.setSize(600, 600);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
    this.add(panel);

    JLabel label = new JLabel("Please edit the question(s) below"
            + " as you see fit");

    panel.add(save);

    save.addActionListener(this);


    panel.add(label);
    edit.setLineWrap(true);

    try { 
        Scanner b = new readFile().openFile();
        while (b.hasNextLine()) {
            edit.setText(b.nextLine() + "\n");
        }
    } catch (Exception e) {}

    JScrollPane scrollyBar = new JScrollPane(edit);
    scrollyBar.setBounds(150, 150, 250, 100);
    scrollyBar.setSize(300, 300);

    panel.add(scrollyBar);
    this.repaint();
  }

  @Override
  public void actionPerformed(ActionEvent e) {
    if (e.getSource() == save) {
        PrintWriter out;
        FileWriter outFile;
        try {
            outFile = new FileWriter("Questions.txt");
            out = new PrintWriter(outFile);
            out.println(edit.getText());
            out.close();
            outFile.close();
        } catch (IOException ex) {
            Logger.getLogger(SecondGUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

 }
 }

 package secondgui;

import java.io.*;
import java.util.*;


public class readFile {

private Scanner x = new Scanner(System.in);

public Scanner openFile() {
    try {
        x = new Scanner(new File("Questions.txt"));

    } catch (Exception e) {
        System.out.println("Could not find file");

    }
    return x;
}

public void readFile() {
    while (x.hasNext()) {
        x.next();
    }
}

public void closeFile() {
    x.close();
}
}

The offending line is: edit.setText(b.nextLine() + "\\n"); 令人反感的行是: edit.setText(b.nextLine() + "\\n"); You should be appending the next line, not setting the text. 您应该添加下一行,而不要设置文本。 If I remember correctly, there is a edit.append() method that you can use. 如果我没记错的话,可以使用edit.append()方法。

Check out the JavaDocs for more information about the JTextArea . 查看JavaDocs以获取有关JTextArea更多信息。

Regardless of your last-line issue, I think your readfile() mechanism is questionable at best. 无论您遇到的最后问题如何,我都认为您的readfile()机制充其量是可疑的。 This: 这个:

 private Scanner x = new Scanner(System.in);
 try {
        x = new Scanner(new File("Questions.txt"));

    } catch (Exception e) {
        System.out.println("Could not find file");
    }

looks dodgy. 看起来很狡猾。 If you encounter an error the scanner defaults to stdin. 如果遇到错误,则扫描仪默认为标准输入。 And this: 和这个:

public void readFile() {
    while (x.hasNext()) {
        x.next();
    }
}

just seems to iterate through the file without doing anything. 似乎不做任何事情就遍历文件。 I would read the tutorial on reading files , and (for the moment at least) throw explicit exceptions if any of that fails. 我将阅读有关读取文件教程 ,并且(至少目前为止),如果其中任何一个失败,则抛出显式异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM