I tried to read from a Textfile using DataInputStream
and FileInputStream
.
I know i should use FileReader instead but i wanted to try it with Byte streams.
here is the code
import java.io.*;
public class FTest_3 {
public static void main(String[] args) throws IOException{
DataInputStream stream = new DataInputStream(new FileInputStream("lol1.txt"));
String str = stream.readUTF();
System.out.println(str);
stream.close();
}
}
If i try to read from the file lol1.txt, getting EOF Exception
and readUTf(unknownsource)
errors.
But if I create a file lol1.txt using DataOutputStream
and then read it using the above code, It works fine.
Here is my code to Write file
import java.io.*;
public class FTest_4 {
public static void main(String[] args) throws IOException{
DataOutputStream stream = new DataOutputStream(new FileOutputStream("lol1.txt"));
stream.writeUTF("hahah");
stream.close();
}
}
Also I could not find any relevant post which could answer my query. Thanks!