1

When reading from a file, the first line that I read has a weird character (using BufferedReader). How do I delete this character? I know I can do it manually, but I want to do it the right way.

enter image description here Picture(NetBeans output)

Cœur
  • 37,241
  • 25
  • 195
  • 267
VELIKI UM
  • 89
  • 1
  • 8

1 Answers1

0

Using the relevant code from the link that the OP provided, here is an answer to the question which works as intended.

import java.io.*;

public class UTF8ToAnsiUtils {

    // FEFF because this is the Unicode char represented by the UTF-8 byte order mark (EF BB BF).
    public static final String UTF8_BOM = "\uFEFF";

    public static void main(String args[]) {
        try {
            if (args.length != 2) {
                System.out
                        .println("Usage : java UTF8ToAnsiUtils utf8file ansifile");
                System.exit(1);
            }

            boolean firstLine = true;
            FileInputStream fis = new FileInputStream(args[0]);
            BufferedReader r = new BufferedReader(new InputStreamReader(fis,
                    "UTF8"));
            FileOutputStream fos = new FileOutputStream(args[1]);
            Writer w = new BufferedWriter(new OutputStreamWriter(fos, "Cp1252"));
            for (String s = ""; (s = r.readLine()) != null;) {
                if (firstLine) {
                    s = UTF8ToAnsiUtils.removeUTF8BOM(s);
                    firstLine = false;
                }
                w.write(s + System.getProperty("line.separator"));
                w.flush();
            }

            w.close();
            r.close();
            System.exit(0);
        }

        catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

    private static String removeUTF8BOM(String s) {
        if (s.startsWith(UTF8_BOM)) {
            s = s.substring(1);
        }
        return s;
    }
}
demongolem
  • 9,474
  • 36
  • 90
  • 105