简体   繁体   中英

Scanner doesn't read from file

I'm writing program, which will be read file, choose by user. I have code:

public class program extends javax.swing.JFrame {

private String textEncode;
...

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFileChooser fch = new JFileChooser();
    int choose = fch.showOpenDialog(this);
    if(choose == JFileChooser.APPROVE_OPTION) {
        String help = fch.getSelectedFile().getPath();
        jTextField2.setText(help);
        try {
            Scanner in = new Scanner(new File(help));
            while(in.hasNextLine()) {
            textEncode = in.nextLine();
            }
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "Nie znaleziono pliku", "Błąd wczytywania", JOptionPane.ERROR_MESSAGE);
        }
    }
    jTextArea1.setText(textEncode);
    System.out.println(textEncode);
}

My file has 1 line of text. When program end read file, variable textEncode has value "null". Where is problem? I try with in.next() and in.hasNext() , but it doesn't work too.

I found a solution:

public class program extends javax.swing.JFrame {

private String textEncode;
...

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    JFileChooser fch = new JFileChooser();
    int choose = fch.showOpenDialog(this);
    if(choose == JFileChooser.APPROVE_OPTION) {
        String help = fch.getSelectedFile().getPath();
        jTextField2.setText(help);
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(help), "UTF-8"));
            String line;
            String readed = "";
            while((line = in.readLine()) != null) {
                readed = readed + line + "\n";
            }
            jTextArea1.setText(readed);
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(this, "Nie znaleziono pliku", "Błąd wczytywania", JOptionPane.ERROR_MESSAGE);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(aes.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(aes.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

But now I have problem with display jTextArea1. When file has loaded, the textArea is resize and looks like this: application window TextArea is added into jScrollPane.

I had the same problem, and it took me a while to figure it out.

My problem was that my file contained french special characters like "é". This caused the scanner to return "null" for scanner.nextLine() without causing any exception, no matter how long was the file, no matter where the special characters were placed.

To solve this, I just removed all the special characters. If anybody has a solution to make Scanner read the special characters, he's welcome to comment below.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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