Jason
Jason

Reputation: 556

Java ASCII output to file

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.

Upvotes: 2

Views: 24537

Answers (2)

McDowell
McDowell

Reputation: 108899

Some languages conflate the character and octet types (C and C++ being notable examples.) Java char types are implicitly UTF-16 and ASCII would have to be represented using the byte type. See here for a comparison. See here for a Java encoding guide.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500785

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();
        }
    }
}

Upvotes: 6

Related Questions