Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 195985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:43:43+00:00 2026-05-11T16:43:43+00:00

I use the following code to save Chinese characters into a .txt file, but

  • 0

I use the following code to save Chinese characters into a .txt file, but when I opened it with Wordpad, I couldn’t read it.

StringBuffer Shanghai_StrBuf = new StringBuffer("\u4E0A\u6D77");
boolean Append = true;

FileOutputStream fos;
fos = new FileOutputStream(FileName, Append);
for (int i = 0;i < Shanghai_StrBuf.length(); i++) {
    fos.write(Shanghai_StrBuf.charAt(i));
}
fos.close();

What can I do ? I know if I cut and paste Chinese characters into Wordpad, I can save it into a .txt file. How do I do that in Java ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T16:43:44+00:00Added an answer on May 11, 2026 at 4:43 pm

    There are several factors at work here:

    • Text files have no intrinsic metadata for describing their encoding (for all the talk of angle-bracket taxes, there are reasons XML is popular)
    • The default encoding for Windows is still an 8bit (or doublebyte) “ANSI” character set with a limited range of values – text files written in this format are not portable
    • To tell a Unicode file from an ANSI file, Windows apps rely on the presence of a byte order mark at the start of the file (not strictly true – Raymond Chen explains). In theory, the BOM is there to tell you the endianess (byte order) of the data. For UTF-8, even though there is only one byte order, Windows apps rely on the marker bytes to automatically figure out that it is Unicode (though you’ll note that Notepad has an encoding option on its open/save dialogs).
    • It is wrong to say that Java is broken because it does not write a UTF-8 BOM automatically. On Unix systems, it would be an error to write a BOM to a script file, for example, and many Unix systems use UTF-8 as their default encoding. There are times when you don’t want it on Windows, either, like when you’re appending data to an existing file: fos = new FileOutputStream(FileName,Append);

    Here is a method of reliably appending UTF-8 data to a file:

      private static void writeUtf8ToFile(File file, boolean append, String data)
          throws IOException {
        boolean skipBOM = append && file.isFile() && (file.length() > 0);
        Closer res = new Closer();
        try {
          OutputStream out = res.using(new FileOutputStream(file, append));
          Writer writer = res.using(new OutputStreamWriter(out, Charset
              .forName("UTF-8")));
          if (!skipBOM) {
            writer.write('\uFEFF');
          }
          writer.write(data);
        } finally {
          res.close();
        }
      }
    

    Usage:

      public static void main(String[] args) throws IOException {
        String chinese = "\u4E0A\u6D77";
        boolean append = true;
        writeUtf8ToFile(new File("chinese.txt"), append, chinese);
      }
    

    Note: if the file already existed and you chose to append and existing data wasn’t UTF-8 encoded, the only thing that code will create is a mess.

    Here is the Closer type used in this code:

    public class Closer implements Closeable {
      private Closeable closeable;
    
      public <T extends Closeable> T using(T t) {
        closeable = t;
        return t;
      }
    
      @Override public void close() throws IOException {
        if (closeable != null) {
          closeable.close();
        }
      }
    }
    

    This code makes a Windows-style best guess about how to read the file based on byte order marks:

      private static final Charset[] UTF_ENCODINGS = { Charset.forName("UTF-8"),
          Charset.forName("UTF-16LE"), Charset.forName("UTF-16BE") };
    
      private static Charset getEncoding(InputStream in) throws IOException {
        charsetLoop: for (Charset encodings : UTF_ENCODINGS) {
          byte[] bom = "\uFEFF".getBytes(encodings);
          in.mark(bom.length);
          for (byte b : bom) {
            if ((0xFF & b) != in.read()) {
              in.reset();
              continue charsetLoop;
            }
          }
          return encodings;
        }
        return Charset.defaultCharset();
      }
    
      private static String readText(File file) throws IOException {
        Closer res = new Closer();
        try {
          InputStream in = res.using(new FileInputStream(file));
          InputStream bin = res.using(new BufferedInputStream(in));
          Reader reader = res.using(new InputStreamReader(bin, getEncoding(bin)));
          StringBuilder out = new StringBuilder();
          for (int ch = reader.read(); ch != -1; ch = reader.read())
            out.append((char) ch);
          return out.toString();
        } finally {
          res.close();
        }
      }
    

    Usage:

      public static void main(String[] args) throws IOException {
        System.out.println(readText(new File("chinese.txt")));
      }
    

    (System.out uses the default encoding, so whether it prints anything sensible depends on your platform and configuration.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following code to save password in a txt file: String FILE_NAME=lol.txt;
The Current Setup: So, I can use the following code to save a file
I use the following code to generate a save file dialog: Response.AppendHeader(content-disposition, attachment; filename=
I use following code to open a file. When I was prompt to open/save
I use jQuery-hotkeys And the following code: $(document).bind('keydown', 'ctrl+s', function(){$('#save').click()}); but I cannot disable
I use the following code to get a screenshot of the screen and save
I wanted to use the following code from here: How can I save all
In my Default.aspx web page i use the following code in order to save
I use the following code to get data from a form and save it
I have read this jsp page encoding problem and this Save Chinese characters with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.