File's in java, (Character-oriented)key notes

Character-Oriented Streams

  • These are the Streams,which will allow the data in the form of characters.From input devices to java program and Form java program to output devices.

  • The length of the data in characters-oriented stream is 2 bytes
  • There are two types of character-oriented streams
    • Reader
    • Writer

Reader

  • It is a character-oriented stream,it will allow the data in the form of characters from input devices to java program.

  • This are the subclass for Reader class(CharArrayReader , FilterReader, BufferedReader, FileReader, InputStreamReader)

Writer

  • It is a character-oriented stream,it will allow the data in the form of characters from java program to output devices.

  • This are the subclass for writer (CharArrayWriter, FilterWriter, FileWriter, PrintWriter , BufferedWriter)

FileWriter

  • Java FileWriter class is used to write character-oriented data to a file. It is character-oriented class which is used for file handling in java.
  • Unlike FileOutputStream class, you don't need to convert string into byte array because it provides method to write string directly.
  • Constructor
    • FileWriter(String file)   // Creates a new file. It gets file name in string.
    • FileWriter(File file)       // Creates a new file. It gets file name in File Object
  • Methods of FileWriter class
    • void write(String text) - It is used to write the string into FileWriter.
    • void write(char c) - It is used to write the char into FileWriter.
    • void write(char[] c) - It is used to write the char array into FileWriter.
    • void flush() - It is used to flushes the data(Empty the buffer) of FileWriter.
    • void close() - It is used to close the FileWriter.

Example:

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
           try{    
FileWriter fw=new FileWriter("H:\\file2.txt"); // Create an object and mention target file name
           fw.write("Welcome to Sookshmas");    // Write data to file
           fw.close();    // Close connection	
          }
	catch(Exception e){
		System.out.println(e);  
          }System.out.println("Successfully write the data to the target file,,,");
    }
}

Output:

Successfully write the data to the target file,,,
check your file2.txt 
Welcome to Sookshmas

FileReader

  • FileReader class is used to read data from the file. It returns data in byte format like FileInputStream class.
  • It is character-oriented class which is used for file handling in java.
  • Constructer
    • FileReader(String file) // It gets filename in string
    • FileReader(File file)     // It gets filename in file instance.
  • Methods of FileReader class
    • int read() - It is used to return a character in ASCII form. It returns -1 at the end of file.
    • void close() - It is used to close the FileReader class.

Example:

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException {
          FileReader fr=new FileReader("H:\\file2.txt");    
          int i;    
          while((i=fr.read())!=-1)  //reading data from file, it returns ASCII value of the character  
          System.out.print((char)i);   //convert ASCII value to character
          System.out.println();
          fr.close();   //close file
    }}

Output:

Welcome to Sookshmas

BufferedWriter Class

  • Java BufferedWriter class is used to provide buffering for Writer instances.
  • It makes the performance fast. It inherits Writer class.
  • The buffering characters are used for providing the efficient writing of single arrays, characters, and strings.
  • Constructors
    • BufferedWriter(Writer wrt)- It is used to create a buffered character output stream that uses the default size for an output buffer.
    • BufferedWriter(Writer wrt, int size) - uses the specified size for an output buffer.
  • Class methods
    • void newLine() - It is used to add a new line by writing a line separator.
    • void write(int c) - It is used to write a single character.
    • void write(char[] cbuf, int off, int len) - It is used to write a portion of an array of characters.
    • void write(String s, int off, int len) - It is used to write a portion of a string.
    • void flush() - It is used to flushes the input stream.
    • void close() - It is used to closes the input stream

Example: (writing the data to a text file using Java BufferedWriter)

package pkg5lab2;
import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
    FileWriter writer = new FileWriter("H:\\file3.txt");  
    BufferedWriter buffer = new BufferedWriter(writer);  
    buffer.write("BufferedWriter");  
    buffer.close();  
    System.out.println("Success");
}
}

output:

Success
check your file3.txt
BufferedWriter

BufferedReader Class

  • Java BufferedReader class is used to read the text from a character-based input stream.
  • It can be used to read data line by line by readLine() method.
  • It makes the performance fast.
  • It inherits Reader class.
  • Constrocter
    • BufferedReader(Reader rd) – It is used to create a buffered character input stream that uses the default size for an input buffer.
    • BufferedReader(Reader rd, int size) - It is used to create a buffered character input stream that uses the specified size for an input buffer.
  • BufferedReader class methods
    • int read() - It is used for reading a single character.
    • int read(char[] cbuf, int off, int len) - It is used for reading characters into a portion of an array.
    • boolean markSupported() - It is used to test the input stream support for the mark and reset method.
    • String readLine() - It is used for reading a line of text.
    • boolean ready() - It is used to test whether the input stream is ready to be read.
    • long skip(long n) - It is used for skipping the characters.
    • void reset() - It repositions the stream at a position the mark method was last called on this input stream.
    • void mark(int readAheadLimit) - It is used for marking the present position in a stream.
    • void close() - It closes the input stream and releases any of the system resources associated with the stream

Example: (reading the data from the text file Java BufferedReader class)

import java.io.*;
public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
    FileReader fr=new FileReader("H:\\file3.txt");    
          BufferedReader br=new BufferedReader(fr);    
          int i;    
          while((i=br.read())!=-1){  
          System.out.print((char)i);  
          }  
          br.close();    
          fr.close();
          System.out.println();
}
}

Output:

BufferedWriter

Continue reading with the other related topic:

Difference between Byte and Character Oriented

File's in java, (Byte-oriented)key notes

Posted on by