For writing a string of data into a file using fileoutputstream we go for converting for string into byte array.For reading a string of data using fileinputstream we can't use convertion. what is the reason for that?
reading:
class Test {
public static void main(String args[]) {
try {
Fileinputstream fin = new Fileinputstream("abc.txt");
int i = 0;
while ((i = fin.read()) != -1) {
System.out.print((char) i);
}
fin.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
writing:
class Test {
public static void main(String args[]) {
try {
FileOutputstream fout = new FileOutputStream("abc.txt");
String s = "Sachin Tendulkar is my favourite player";
byte b[] = s.getBytes(); //converting string into byte array
fout.write(b);
fout.close();
System.out.println("success...");
} catch (Exception e) {
system.out.println(e);
}
}
}