Methods | Description |
---|---|
void close() | This method closes the stream. |
void flush() | It flushes the stream. |
void print(boolean b) | Prints a boolean value. |
void print(char ch) | Prints a character value. |
void print(char[] ary) | Prints an array of character value. |
void print(double d) | Prints a double-precision floating-point number. |
void print(float f) | Prints a floating point number. |
void print(int i) | Prints an integer value. |
void print(long l) | Prints a long integer. |
Void print(object obj) | This method calls toString() method of object passed as parameter. |
void print(String str) | Prints a string. |
PrintStream printf(String format, Object... args) | It is a conversion method to write a formatted string to the output stream using the specified formatted string and arguments |
void println() | Terminates the current line by writing the line separator string. |
void println(boolean b) | Prints a boolean and then terminates the line. |
void println(char ch) | Prints a character and then terminates the line. |
void println(char[] ary) | Prints an array of characters and then terminates the line. |
void println(doubled d) | Prints a double-precision floating point and then terminates the line. |
void println(float f) | Prints a floating-point number and then terminates the line. |
void println(Object obj) | Prints the string returned by toString() method of object passed as parameter and then terminates the line. |
void println(String str) | Prints a string and then terminates the line. |
void write(byte[] buf, int off, int count) | Writes count bytes from the specified byte array(buf) starting from offset off. |
void write(int b) | Writes the specified byte to the stream. |
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.PrintStream; class Address { int houseNo; int stno; String area; Address(int h,int s,String ar) { houseNo=h; stno=s; area=ar; } public String toString() { return "House no="+houseNo+" Street no.="+stno+" Area="+area; } } public class PrintStreamClass { public static void main(String[] args) throws FileNotFoundException,IOException { //Writing In File FileOutputStream fos=new FileOutputStream("EmpData.txt"); PrintStream ps=new PrintStream(fos); String EmpName="Sandeep Kumar"; float salary=5000000;// System.out.println("Writing Employee Name..."); ps.println("Employee Name:"+EmpName);//writing string System.out.println("Writing Employee Salary..."); ps.println("Salary:"+salary);//writing float Address adr=new Address(20877, 18, "Ajit Road"); System.out.println("Writing Employee Address..."); ps.println("Employee Address:"+adr);// writing object fos.close(); ps.close(); System.out.println("Writing Process Completed...."); // Reading from file (line by line) BufferedReader rdr=new BufferedReader(new FileReader("EmpData.txt")); String line; System.out.println("Reading Started..."); while((line=rdr.readLine())!=null) { System.out.println(line); } rdr.close(); System.out.println("Reading Finished...."); } }
OUTPUT Writing Employee Name... Writing Employee Salary... Writing Employee Address... Writing Process Completed.... Reading Started... Employee Name:Sandeep Kumar Salary:5000000.0 Employee Address:House no=20877 Street no.=18 Area=Ajit Road Reading Finished....
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; public class BufferedReaderWriter { static void writeToFile() { try { //Input Data from User BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Your Name:"); String s = br.readLine(); System.out.print("Enter Your Address:"); String a = br.readLine(); System.out.print("Enter Your College/Company Name:"); String cc = br.readLine(); // Write the above in file BufferedWriter bufw=new BufferedWriter(new FileWriter("urdata.txt",true)); bufw.write("Name:"+s); bufw.newLine(); bufw.write("Address:"+a); bufw.newLine(); bufw.write("College/Company:"+cc); bufw.newLine(); bufw.flush();//clear the buffer bufw.close();//must be closed System.out.println("Writing Process Completed......."); } catch(Exception ex){ex.printStackTrace();} } static void readFromFile() { try{ BufferedReader rdr=new BufferedReader(new FileReader("urData.txt")); String line; System.out.println("\nReading Data From File..."); while((line=rdr.readLine())!=null) { System.out.println(line); } rdr.close(); System.out.println("Reading Finished...."); } catch(IOException ex){ ex.printStackTrace();} } public static void main(String[] args) { writeToFile(); readFromFile(); } }
OUTPUT Enter Your Name:Rajesh Bansal Enter Your Address:Ajit Road Enter Your College/Company Name:BCE & SST Writing Process Completed....... Reading Data From File... Reading Started... Employee Name:Sandeep Kumar Salary:5000000.0 Employee Address:House no=20877 Street no.=18 Area=Ajit Road Reading Finished....