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.