user1058036
user1058036

Reputation: 333

How can I make Notepad to save text in UTF-8 without the BOM?

I have a CSV file with special accents and save it in Notepad by selecting UTF-8 encoding. When I read the file using Java, it reads the BOM characters too.

So I want to save this file in UTF-8 format without appending a BOM initially in Notepad.

Otherwise, is there a built-in class in Java that eliminates the BOM characters that present at beginning, when reading the contents in a file?

Upvotes: 29

Views: 76109

Answers (7)

korifey
korifey

Reputation: 3509

  1. Use Notepad++ - it is free and much better than Notepad. It will help to save text without a BOM using EncodingEncode in UTF-8 without BOM:

    Notepad++ v6 and olders: Screenshot of the Notepad++ Menubar -> Encoding -> Encode in UTF-8 without BOM menu in Notepad++ v6.7.9.2

    Notepad++ v7+:
    Screenshot of the Notepad++ Menubar -> Encoding -> Encode in UTF-8 without BOM menu in Notepad++ v7+

  2. When I encountered this problem in Java, I didn't find any library to parse these first three bytes (BOM). So my advice:

    • Use PushbackInputStream(in, 3).
    • Read the first three bytes
    • If it's not BOM (EF BB BF), push them back
    • Process the stream as UTF-8

Upvotes: 39

Marc Durdin
Marc Durdin

Reputation: 1793

Notepad on Windows 10 version 1903 (May 2019 update) and later versions supports saving to UTF-8 without a BOM. In fact, UTF-8 is the default file format now.

Screenshot of Notepad

Reference: Windows 10 Notepad is Getting Better UTF-8 Encoding Support

Upvotes: 11

olaf atchmi
olaf atchmi

Reputation: 113

I just learned from this Stack Overflow post, as @martin-geisler points out, that you can save files without the BOM in Windows Notepad, by selecting ANSI as the encoding.

I'm assuming that for more advanced uses this won't work because the resulting file is probably not the end encoding wished, but actually ANSI; but I tested and confirmed this works to save a very small .php script without BOM using only Notepad.

I learned the long, hard way that Windows' Notepad is not a true editor, although I'd like to point out for others that, despite this, it is misleadingly called up when you type "editor" on newer Windows machines, at least on one of mine.

I am currently using Emacs and other editors to solve this problem.

Upvotes: 11

Thomas
Thomas

Reputation: 88707

We're using the utility BOMStripperInputStream.java to strip the BOM from our input if present.

Upvotes: 0

Jeow Li Huan
Jeow Li Huan

Reputation: 3796

You might want to try out Notepad2 or Notepad++. Those Notepad replacements have the option for you to choose whether to output BOM.

As for a Java solution, as far as I know, Java does not understand the standard UTF-8. I googled and found Java's UTF-8 and Unicode writing is broken - Use this fix that might be the solution.

Upvotes: 0

Angelo Fuchs
Angelo Fuchs

Reputation: 9941

The answer is: Not at all. Notepad can't do that.

In Java you can just skip the first byte in your InputStream and be done.

Upvotes: 0

ziesemer
ziesemer

Reputation: 28687

Use Notepad++ instead. See my personal blog post on it. From within Notepad++, choose the "Encoding" menu, then "Encode in UTF-8 without BOM".

Upvotes: 9

Related Questions