I am a newbie and I really want to learn the concept instead of just copying and pasting code. I wanted to learn how exactly to use the Jave IO and was confused and disapointed to see so my different versions of code.
So I made my own notes and wanted to confirm with the experts here whether I got it right. these are just for my own reference. I know is not perfect but I would appreciate if you could confirm whether they are correct.
Use BufferedWriter and FileWriter to write a text file ( as characters). Disadvantage is you cannot write a primitive data type.
Ex:
BufferedWriter bw= new BufferedWriter (new FileWriter("a.txt", true));
String x;
while ((x=bw.readLine())!=null){
bw.newLine();
bw.write(x);
bw.flush();}
Use BufferedReader and FileReader to read a text file (as characters)
Ex:
BufferedReader br = new BufferedReader (new FileReader("b.txt"));
String x;
while ((x=br.readLine())!=null){
System.out.println(x);}
Use DataOutputStream and FileOutputStream to write to a text file (in binary). Advantage is you can write primitive data types as well as strings.
Ex:
DataOutputStream dos = new DataOutputStream(new FileOutputStream("out.txt"));
dos.writeInt(cityIdA); // int cityIdA = 9897;
dos.writeUTF(cityNameA); // String cityNameA = "Green Lake City";
dos.writeInt(cityPopulationA); // int cityPopulationA = 500000;
dos.writeFloat(cityTempA); // float cityTempA = 15.50f;
dos.flush();
Use DataInputStream and FileInputStream to read a text file (in binary). Advantage is you can read primitive data types as well as strings.
Ex:
DataInputStream dis = new DataInputStream(new FileInputStream("inp.txt"));
int cityId1 =dis.readInt(); // int cityIdA = 9897;
String cityName1 =dis.readUTF(); // String cityNameA = "Green Lake City";
int cityPopulation1 =dis.readInt(); // int cityPopulationA = 500000;
float cityTemperature1 =dis.readFloat(); // float cityTempA = 15.50f;
Actual code:
import java.io.*;
class b{
public static void main (String args[]) throws IOException{
int cityIdA = 9897;
String cityNameA = "Green Lake City";
int cityPopulationA = 500000;
float cityTempA = 15.50f;
BufferedWriter bw = new BufferedWriter(new FileWriter("shahar.txt"));
bw.write("9897");
bw.write("Green Lake City");
bw.write("500000");
bw.write("15.50");
bw.flush();
bw.close();
DataOutputStream dos = new DataOutputStream(new FileOutputStream("out.txt"));
dos.writeInt(cityIdA);
dos.writeUTF(cityNameA);
dos.writeInt(cityPopulationA);
dos.writeFloat(cityTempA);
dos.flush();
BufferedReader br = new BufferedReader (new FileReader("shahar.txt"));
String x;
while ((x=br.readLine())!=null){
System.out.println(x);}
DataInputStream dos1 = new DataInputStream(new FileInputStream("out.txt"));
int cityId1 = dos1.readInt(); // int cityIdA = 9897;
System.out.println( cityId1);
String cityName1 =dos1.readUTF(); // String cityNameA = "Green Lake City";
System.out.println(cityName1);
int cityPopulation1 =dos1.readInt(); // int cityPopulationA = 500000;
System.out.println(cityPopulation1);
float cityTemperature1 =dos1.readFloat(); // float cityTempA = 15.50f;
System.out.println(cityTemperature1);
}
}