Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

File Handling Classes In BlueJ

 FileReader

FileReader(String file) It takes filename in string. It opens the given file in read mode. If file doesn't exist, it throws FileNotFoundException.

FileReader(File file) It takes filename in file instance. It opens the given file in read mode. If file doesn't exist, it throws FileNotFoundException.

 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.

FileWriter

FileWriter(File file) – Constructs a FileWriter object given a File object.

FileWriter (File file, boolean append) – constructs a FileWriter object given a File object.

FileWriter (String fileName) – constructs a FileWriter object given a file name.

FileWriter (String fileName, Boolean append) – Constructs a FileWriter object given a file name with a Boolean indicating whether or not to append the data written.
Methods:

public void write (int c) Throws Ioexception – Writes a single character.

public void write (char [] stir) throws IOException – Writes an array of characters.

public void write(String str)throws IOException – Writes a string.

public void write(String str,int off,int len)throws IOException – Writes a portion of a string. Here off is offset from which to start writing characters and len is number of character to write.

public void flush() Throws Ioexception Flushes the stream

public void close() throws IOException flushes the stream first and then closes the writer.

FileInputStream

FileInputStream inputStream = new FileInputStream("file_path");
or,
File file = new File("file_path");
FileInputStream inputStream = new FileInputStream(file);

int read() − This simply reads data from the current InputStream and returns the read data Byte by byte (in integer format).
This method returns -1 if the end of the file is reached.

int read(byte[] b) − This method accepts a byte array as parameter and reads the contents of the current InputStream, to the given array

This method returns an integer representing the total number of bytes or, -1 if the end of the file is reached.

int read(byte[] b, int off, int len) − This method accepts a byte array, its offset (int) and, its length (int) as parameters and reads the contents of the current InputStream, to the given array.
This method returns an integer representing the total number of bytes or, -1 if the end of the file is reached.

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamExample {
   public static void main(String args[]) throws IOException {
      //Creating a File object
      File file = new File("D:/images/javafx.jpg");
      //Creating a FileInputStream object
      FileInputStream inputStream = new FileInputStream(file);
      //Creating a byte array
      byte bytes[] = new byte[(int) file.length()];
      //Reading data into the byte array
      int numOfBytes = inputStream.read(bytes);
      System.out.println("Data copied successfully...");
   }
}

FileOutputStream 

FileOutputStream outputStream = new FileOutputStream("file_path");
or,
File file = new File("file_path"); FileOutputStream outputStream = new FileOutputStream (file);

Then write the data to a specified file using either of the variants of write() method −

int write(int b) − This method accepts a single byte and writes it to the current OutputStream.

int write(byte[] b) − This method accepts a byte array as a parameter and writes data from it to the current OutputStream.

int write(byte[] b, int off, int len) − This method accepts a byte array, its offset (int) and, its length (int) as parameters and writes its contents to the current OutputStream.


This post first appeared on Tutorial Site On Computer Programming Languages F, please read the originial post: here

Share the post

File Handling Classes In BlueJ

×

Subscribe to Tutorial Site On Computer Programming Languages F

Get updates delivered right to your inbox!

Thank you for your subscription

×