I am writing a java code to write ASCII characters to a file. But when I try to write any character of ASCII value of more than 140 or less than 32 to a text file, I get a blank file. It does not write anything to the file. My program is working successfully for all values between 32 and 140. Please help…..
this is the code
public class IO {
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
FileOutputStream fs=new FileOutputStream("C:/Users/Shantanu/Desktop/abc.txt");
fs.write(143);
fs.close();
System.out.println("finished");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
When I run this
I get
Since you write 1 byte, you should expect it to be 1 byte long.
However if you attempt to read this as a UTF-8 or some other encoding, this may be less than 1 character (e.g. in UTF-8 you need 2-3 bytes for characters about 127)