Various ways to write 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 writing a file. so that the same/other application can read the data which we have written in future. By this way our application will also be in a consistent state. Following are the various ways to write 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!
FileOutputStream
FileOutputStream is derived from OutputStream class.
import java.io.*; class FileOutputStreamDemo { public static void main(String[] s)throws IOException { byte[] b; String str = "I want to write some text to file."; b = str.getBytes(); File f = new File("data.txt"); // this will points to the data.txt of the same directory where your program is stored. FileOutputStream fos = new FileOutputStream(f); fos.write(b); fos.flush(); System.out.println("File Created!"); fos.close(); } }
FileWriter
import java.io.*; class FileWriterDemo { public static void main(String[] s)throws IOException { String str = "I want to write some text to file."; File f = new File("data.txt"); // this wil points to the data.txt of the same directory where your program is stored. FileWriter fw = new FileWriter(f); fw.write(str); fw.flush(); System.out.println("File Created!"); fw.close(); } }
BufferedWriter + FileWriter
import java.io.*; class FileBufferedWriterDemo { public static void main(String[] s)throws IOException { String str = "I want to write some text to file."; File f = new File("data.txt"); // this wil points to the data.txt of the same directory where your program is stored. FileWriter fw = new FileWriter(f); BufferedWriter bw = new BufferedWriter(fw); bw.write(str); bw.flush(); System.out.println("File Created!"); bw.close(); } }
FilterWriter
FilterWriter 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: FilterWriter
Aim
Write a program which defines a FilterWriter which converts larges statements(having more than 15 chars) to a shorter statement and write a file.
e.g. If we tries to write “This is demo by Ankit”. It appends first 5 chars and last 5 chars of string with “…” and generate “This … Ankit” as a output.
import java.io.*; public class FilterDemo { public static void main(String[] args) throws IOException { // TODO code application logic here File f = new File("data.txt"); FileWriter fw = new FileWriter(f); FilterWriter mfw = new FilterWriter(fw){ @Override public void write(String str) throws IOException { int len = str.length(); String output = ""; if(len>15) { output = str.substring(0,5); output += "... "; output += str.substring(len-5,len); } super.write(output); }}; mfw.write("This is demo by Ankit"); mfw.flush(); mfw.close(); } }
DataOputputStream
import java.io.*; class DataOutputStreamDemo { public static void main(String[] s)throws Exception { FileOutputStream fos = new FileOutputStream(new File("db.txt")); DataOutputStream dos = new DataOutputStream(fos); dos.writeBytes("This is Test\n\n"); dos.writeDouble(1232.23); dos.writeInt(123); dos.flush(); dos.close(); System.out.println("File Written"); } }
ObjectOputputStream
import java.io.*; import java.util.*; class ObjectOutputStreamDemo { public static void main(String[] s)throws Exception { FileOutputStream fos = new FileOutputStream("test.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeInt(12345); oos.writeObject("Today"); oos.writeObject(new Date()); oos.flush(); oos.close(); System.out.println("Objects written!"); } }
Recent Comments