0

I'm trying to make a program that reads textfiles. I tried this code and it almost works but the output starts with these 3 characters 
How do I write so it doesn't output them?

 JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Select a text file");
    int Checker = chooser.showOpenDialog(null);
    File F = chooser.getSelectedFile();
    String line = null;

    try {
        FileReader fileReader = new FileReader(F);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        bufferedReader.close();
    } catch (FileNotFoundException ex) {
        System.out.println("Unable to open file '" + F + "'");
    } catch (IOException ex) {
        System.out.println("Error reading file '" + F + "'");
    }

2 Answers2

1

It's commonly called a BOM (Byte Order Marker) that you can find in files encoded in UTF-8. Here you can find a solution to read a file in utf-8: reading text file with utf-8 encoding using java

Community
  • 1
  • 1
ADreNaLiNe-DJ
  • 4,787
  • 3
  • 26
  • 35
0

see if this works (using Scanner)

import java.io.*;
   import java.util.Scanner;
   import javax.swing.JFileChooser;

    public class ScanXan {
        public static void main(String[] args) throws IOException {


   JFileChooser chooser = new JFileChooser();
    chooser.setDialogTitle("Select a text file");
    int Checker = chooser.showOpenDialog(null);
    File F = chooser.getSelectedFile();


            Scanner s = null;

            try {
                s = new Scanner(new BufferedReader(new FileReader(F)));

                while (s.hasNext()) {
                    System.out.println(s.next());
                }
            } finally {
                if (s != null) {
                    s.close();
                }
            }
        }
    }

reference: https://docs.oracle.com/javase/tutorial/essential/io/scanning.html

Grant Shannon
  • 4,709
  • 1
  • 46
  • 36
  • Have just added import *javax.swing.JFileChooser*; to the top of the file - then compiled and ran using BlueJ IDE. It works fine. Let me know if you can't get this code to compile and run. – Grant Shannon Mar 11 '16 at 18:30