When I try to output an ASCII value to a file, it sometimes writes 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.

Recommended Answers

All 8 Replies

When I try to output an ASCII value to a file, it sometimes writes 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.

maybe you must set the default encoding to ascii:http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding on my Netbeans the code gives me '"'
you could also try:

FileInputStream fis =  new FileInputStream("out.dat");
      BufferedReader r = 
        new BufferedReader(new InputStreamReader(fis, "ACSII"));

when attempting to read and write

Thanks for the suggestion. I'm playing around with it, but I still haven't figure it out...

See the screenshot, I get a value of 147! No change in your code...

PS, What do you mean by 'sometimes'? Usually programs give the same output, whether you run them once or 100 million times...

Strange. That's not the results that I am getting.

By "sometimes," I meant "for some values." Sorry for my lack of clarity. For most ASCII values the correct number is returned, but for a few-- like 147-- it's wrong.

Very interesting. What IDE, OS and Java version are you using?
Maybe a good idea to post this on StackOverflow, there are a lot of experts, who know really ANYTHING about ANY coding questions.

I hate to rain on your parade, but there's no such thing as an ASCII value of 147.
The ANSI ASCII standard code is a 7 bit code, covering values 0 - 127.
There are many many variants on 8 bit codes (code pages in DOS terms), which are consistent with ASCII up to 127 the go their own ways 128-255.
Java uses Unicode, which is a 16 bit code that us consistent with ASCII for values where the first 9 bits are all zero.
When you convert a Unicode value in the range 128-255 to/from an 8 bit value you are at the mercy of whatever code page(s) may be used by your system at the time.
http://en.wikipedia.org/wiki/ASCII

So the question remains: how can I output the character-casted 147 to a file and read in the integer-casted number of the character.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.