Various ways to read a file
Persistence vs Permanent storage
In programming, persistence storage refers to a storage area which may forget the data/information once it looses the electronic energy supply. such as RAM – Random Access Memory. When we run an application having some variables, methods, objects, etc. That data is stored in Computer RAM. Problem with this system is when we want to share some information between two execution cycle that won’t be possible. For Example: We have developed a “Student Management System” for a college and one operator starts entering student records in our software. If we as a developer has written code which stores student information in an Array, in that case a power failure/system crash/software termination/software closing may lead to information lost and operator need to start entering details from the scratch! Here the conclusion is: We cannot not store the important or permanent data in RAM. We must choose either 1) File 2) Database. In this tutorial we gonna learn about reading a file. so that the previously written data can be fetched and processed. By this way our application will also be in a consistent state. Following are the various ways to read a file in any storage device like hard disk, floppy, pen drive, etc. The basic need to use a storage device is to mount the same and you are ready to go!
FileInputStream
FileInputStream is derived from InputStream class.
import java.io.*; class FileInputStreamDemo { public static void main(String[] s)throws IOException { byte[] b = new byte[10000]; File f = new File("data.txt"); // this wil points to the data.txt of the same directory where your program is stored. FileInputStream fis = new FileInputStream(f); int length = fis.read(b); String str = new String(b,0,length); System.out.println("Data: "+str); fis.close(); } }
FileReader
import java.io.*; class FileReaderDemo { public static void main(String[] s)throws IOException { String str = ""; char[] c = new char[100]; File f = new File("data.txt"); FileReader fr = new FileReader(f); int len = 0; len = fr.read(c); str = new String(c,0,len); System.out.println("Data: "+str); fr.close(); } }
BufferedWriter + FileWriter
import java.io.*; class FileBufferedReaderDemo { public static void main(String[] arg)throws Exception { BufferedReader br = new BufferedReader(new FileReader(new File("data.txt"))); System.out.print("Data: "); String str = br.readLine(); System.out.println(str); } }
FilterReader
FilterReader is an abstract class which does not have any abstract method. We can override the methods of it and perform our code instead. For more details visit: FilterReader
Aim
Write a program using FilterReader which removes tags from the file content. e.g. <tag> should be removed.
import java.io.*; class FilterReaderDemo { public static void main(String[] args) { try { FilterReader fr = new FilterReader(new FileReader("data.txt")){ public int read(char[] buf, int from, int len) throws IOException { boolean intag = false; int numchars = 0; while (numchars == 0) { numchars = in.read(buf, from, len); if (numchars == -1) { return -1; } int last = from; for (int i = from; i < from + numchars; i++) { if (!intag) { if (buf[i] == '<') intag = true; else buf[last++] = buf[i]; } else if (buf[i] == '>') { intag = false; } } numchars = last - from; } return numchars; } public int read() throws IOException { char[] buf = new char[1]; int result = read(buf, 0, 1); if (result == -1) return -1; else return (int) buf[0]; } }; BufferedReader in = new BufferedReader(fr); String line; while ((line = in.readLine()) != null) { System.out.println(line); } in.close(); // Close the stream. } catch (Exception e) { System.err.println(e); } } }
DataInputStream
import java.io.*; class DataInputStreamDemo { public static void main(String[] s)throws Exception { FileInputStream fis = new FileInputStream(new File("db.txt")); DataInputStream dis = new DataInputStream(fis); int avail = dis.available(); byte[] b = new byte[avail]; dis.readFully(b); System.out.println(new String(b)); dis.close(); } }
ObjectInputStream
import java.io.*; import java.util.*; class ObjectInputStreamDemo { public static void main(String[] s)throws Exception { FileInputStream fis = new FileInputStream("test.txt"); ObjectInputStream ois = new ObjectInputStream(fis); System.out.println(ois.readInt()); System.out.println(ois.readObject()); System.out.println(ois.readObject()); ois.close(); System.out.println("All Objects read!"); } }
Excellent Sir.
Was this answer helpful?
LikeDislikeSuper like. Simple and easy to understand.
Was this answer helpful?
LikeDislikeSir, Please upload Exception Handling.
Was this answer helpful?
LikeDislikeThank you.
Was this answer helpful?
LikeDislikeThank you sir.
Was this answer helpful?
LikeDislike