Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
when i open file rahul.txt so it shows characters which i stored not in form of bytes and second problem is that when i use FileInputStream to read file rahul.txt so it did not read .
package com.fileh;
import java .io.*;

public class Writedata {
public static void main(String args[])
{
try{
FileOutputStream fout= new FileOutputStream("rahul.txt");
String s="my name is rahul";
byte[] b =s.getBytes();
fout.write(b);
fout.close();
}
catch(IOException ioe)
{
}
}
}
Posted

1 solution

No, it is saved in the form of bytes.

Probably you just did not realize it. It's possible that you don't understand what the byte is and how it is presented. If you think that the "byte" would look in the file read by some text editor, something as "0x2F", think again. Or, if you need that, format each byte as a string (2 or 4-character, as in this example) and output the resulting string. Right now, you can see those the bytes in some binary editor.

Also, you need to understand that your "may name is rahul" is represented in Unicode, and the result of String.getBytes is "a sequence of bytes using the platform's default charset, storing the result into a new byte arra":
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#getBytes%28%29[^].

This is not very good due to this uncertainty. To be more certain, you would need to specify exact encoding. For further detail, please see this tutorial: https://docs.oracle.com/javase/tutorial/i18n/text/string.html[^].

For example, if you choose UTF-16, you will get two bytes per characters. With UTF-8, you get variable number of strings per characters, and full Unicode character repertoire can be covered. With ASCII, you will get exactly one byte per character, but ASCII works only for code points below 128 (less or equal to 127). As in your example all characters fit in the ASCII range, you will get 1 byte per character, same bytes for ASCII and UTF-8, but other characters not supported by ASCII, in case you use it, may be replaced with '?', which is unacceptable. So, always use UTFs for all general-purpose strings.

—SA
 
Share this answer
 
v4
Comments
Member 11607403 15-Apr-15 1:58am    
no if it stored in form of bytes so when i open rahul.txt so it shows as"it is may name is rahul"not shows any other form of byte.
Sergey Alexandrovich Kryukov 15-Apr-15 2:00am    
Read the solution again; you did not understand anything.
—SA
Member 11607403 15-Apr-15 2:14am    
i think i can not understand it .but i am beginner of java that's why can u help to explain it in easy way.
Sergey Alexandrovich Kryukov 15-Apr-15 2:31am    
My guess was: you are also the beginner who voted 1 for the solution just because you did not understand it. Am I right?

In all cases, I'll gladly explain it better, but you have to tell me what was not "easy" and ask your follow-up questions.

—SA
Member 11607403 15-Apr-15 2:36am    
ok sorry for that. fine but please explain this in easy way...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900