Showing posts with label File Handling In Java. Show all posts
Showing posts with label File Handling In Java. Show all posts

Monday, January 10, 2022

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.

File Handling Program In BlueJ Using FileWriter Class

 
import java.io.*;
import java.util.*;
class B
{
   String str;
   Scanner sc=new Scanner(System.in);
    FileWriter fw;  
    
    void take() throws Exception
    {      
       fw    =new FileWriter("E:/myfile.txt");
    System.out.print("\nEnter The sentence: ");
        str=sc.nextLine();                
       fw.write(str);
       fw.close();
    }       
   }

Thursday, October 28, 2010

File Handling In Java

The following program is on file handling in java. Names of several persons are to be written in a text file in the format name-middlename-surname. In case there is no middle name, record is entered as name and surname. Records are displayed as Middle name, Name and SurName. If there is no middle name then 'XX' is displayed in middle name part.

import java.io.*;
import java.util.*;
class BlueJx
{
public static void main(String args[]) throws IOException
{
byte barr[];
StringTokenizer stk;
String name,mname,sname;
int pos;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
FileOutputStream fout;
FileInputStream fin;
fout=new FileOutputStream("record.txt",true);
String str;
/*2nd argument true means the values will be appended in the file*/

System.out.println("Enter the Name with middle Name and Surname");
str=br.readLine();
str="*"+str;
barr=str.getBytes();
fout.write(barr);
// for reading
fin=new FileInputStream("record.txt");
pos=fin.available();
barr=new byte[pos];
System.out.println("\nRecords as follows\n");
fin.read(barr);
str=new String(barr);
stk=new StringTokenizer(str,"*");
while(stk.hasMoreTokens())
{
str=stk.nextToken();
pos=str.lastIndexOf(" ");
sname=str.substring(pos).trim();
str=str.substring(0,pos).trim();
pos=str.lastIndexOf(" ");
if(pos!=-1)
{
mname=str.substring(pos).trim();
name=str.substring(0,pos).trim();
}
else
{
mname="XX";
name=str;
}
System.out.println(mname + " "+ name+" "+ sname);
}
}
}

Related Post:  BlueJ Programs on String/Sentence

Subscribe via email

Enter your email address:

Delivered by FeedBurner