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 7193571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:11:50+00:00 2026-05-28T20:11:50+00:00

When I try to output an ASCII value to a file, with some characters

  • 0

When I try to output an ASCII value to a file, with some characters it returns the wrong value. Example:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.PrintWriter;

    public class test {
    public static void main(String args[]){
                                  //Supposed to:
        writeFile("./test.txt"); //write ASCII 147
        readFile("./test.txt"); //read ASCII 147
    }

    public static boolean writeFile(String path){
        try{
            PrintWriter fo = new PrintWriter(new FileOutputStream(new File(path)));
            fo.print((char) 147); //WRITES "?" TO FILE (ASCII 63, NOT 147)
            fo.close();
        }catch(Exception e){
            return true;
        }
        return false;
    }

    public static boolean readFile(String path){
        try {
            BufferedReader fi = new BufferedReader(new FileReader(path));
            char c[] = fi.readLine().toCharArray();
            System.out.println((int) c[0]); //OBVIOUSLY PRINTS 63 INSTEAD OF 147
            fi.close();
            return true;
        } catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}

What am I doing wrong? any help would be great. Thanks.

  • 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-28T20:11:51+00:00Added an answer on May 28, 2026 at 8:11 pm

    There’s no such thing as “character 147” in ASCII.

    You should give a specific encoding which can represent the characters you’re interested in – UTF-8 is usually a good choice – and use the same encoding for both reading and writing.

    Unfortunately FileWriter and FileReader don’t allow you to specify the encoding, so you’ll need FileOutputStream and FileInputStream wrapped in an OutputStreamWriter and InputStreamReader:

    import java.io.*;
    import java.nio.charset.*;
    
    public class Test {
    
        private static Charset UTF8 = Charset.forName("UTF-8");
    
        public static void main(String args[]) throws IOException {
            writeFile("./test.txt");
            readFile("./test.txt");
        }
    
        public static void writeFile(String path) throws IOException {
            Writer writer = new OutputStreamWriter(new FileOutputStream(path), UTF8);
            try {
                writer.write((char) 147);
            } finally {
                writer.close();
            }
        }
    
        public static void readFile(String path) throws IOException {
            Reader reader = new InputStreamReader(new FileInputStream(path), UTF8);
            try {
                int c = reader.read();
                System.out.println(c);
            } finally {
                reader.close();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have having some trouble printing a char from an ASCII value stored in
I need to read the output from ffmpeg in order to even try the
Try loading this normal .jpg file in Internet Explorer 6.0. I get an error
Try as I might, I can't get a JNLP file to run locally (via
look at that: import urllib print urllib.urlencode(dict(bla='Ã')) the output is bla=%C3%BC what I want
I'm processing a text file output by a scientific instrument. I do not have
I'm trying to get Mako render some string with unicode characters : tempLook=TemplateLookup(..., default_filters=[],
I thought characters in java are 16 bits as suggested in java doc .
import java.io.FileNotFoundException; import java.lang.SecurityException; import java.util.Formatter; import java.util.FormatterClosedException; import java.util.NoSuchElementException; import javax.swing.JOptionPane; public class
import java.io.FileNotFoundException; import java.lang.SecurityException; import java.util.Formatter; import java.util.FormatterClosedException; import java.util.NoSuchElementException; import javax.swing.JOptionPane; public class

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.