Session 8 : Java IO



Java IO

Q? What is Stream
/> all the I/O operations in java depends on the abstraction of stream( flow of data)
/> this flow of data can come from : file in the diskstandard input/outputnetwork connection- whatever hw devices
/> this I/O operations works in the same way for all the kinds of stream
/> library classes are in package : java.io
/> the exception are the subclasses of IOException

Q? how do we read the character (InputStream)
/> int read(); method
/> reads the single character and returns -1 if End Of Stream occurs
/> to read a line we can use this read() method in the loop


Q? how do we write the character (OutputStream)
/> int write(); method
/> write the char in output stream

Q? how do we use BufferedReader to read a file from the stream;
/> we use the BufferedReader Class : this class provides the method for efficient reading of char, arrays, and lines.
/> the best part is that it uses a default-sized input buffer to start buffering the char-input stream
/> FileReader is the class used for reading char files
/> readLine() method reads a single line of text excluding the termination character, or null if an end of the stream has been reached
/> here I have also discussed the implementation of using IOException to handle error in case of file reading
BufferedReader reader = null;

try {
File file = new File("fileName.txt");
reader = new BufferedReader(new FileReader(file));

String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}





Q? what Java File class consits of
/> static methods that operate on files, directories, or other types of files.
/>Java File class represents the files and directory pathnames in an abstract manner.
/>This class is used for creation of files and directories, file searching, file deletion etc.

Q? what is StringTokenizer
/> The StringTokenizer class provides the first step in this parsing process. So given the input string, you can enumerate the individual tokens contained in it.
/> this works on Stream of data
/> to use this class we have to specify an input string and a string that contains delimiters. Delimiters are characters that separate tokens.
/> set of delimiters are (blank, “,”,\t,\n,\r,\f) : blank is the default delimiter
/> import java.util.StringTokenizer;
class STDemo {
static String in = "title=Jumping-Fox;" +
"author=Niranjan K;" +
"publisher=niranjanandkhatri.blogspot.it;" +
"funnyAct 2014;";
public static void main(String args[]) {
StringTokenizer st = new StringTokenizer(in, "=;");
while(st.hasMoreTokens()) {
String key = st.nextToken();
String val = st.nextToken();
System.out.println(key + "\t" + val);
}
}
}

Output:
title Jumping-Fox
author
Niranjan K
publisher
niranjanandkhatri.blogspot.it funnyAct 2014


Q? what is serialization in java
/>mechanism, where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object.
To perfom serialization two things must be done :
/> must implement the java.io.Serializable interface.
/> All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

Ex:
public class Employee implements java.io.Serializable
{
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck()
{
System.out.println("Mailing a check to " + name
+ " " + address);
}
}
import java.io.*;

public class SerializeDemo
{
public static void main(String [] args)
{
Employee e = new Employee();
e.name = "Niranjan Khatri";
e.address = "Turin, Italy";
e.SSN = 11122333;
e.number = 101;
try
{
FileOutputStream fileOut =
new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
}catch(IOException i)
{
i.printStackTrace();
}
}
}
System.out.println(key + "\t" + val);
}
}
}

Comments

Popular Posts