













Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
An overview of java i/o and file exceptions, focusing on the basic i/o-related exception classes such as eofexception, filenotfoundexception, interruptedioexception, and ioexception. It also covers the syntax for handling exceptions in java i/o operations using try-catch blocks. Examples of creating files, reading and writing characters using filereader and filewriter, and copying characters from files.
Typology: Thesis
1 / 21
This page cannot be seen from the preview
Don't miss anything!
Mohammed Husain Bohara
EOFException – signals that end of the file is reached unexpectedly during input. FileNotFoundException – file could not be opened InterruptedIOException – I/O operations have been interrupted IOException – signals that I/O exception of some sort has occurred – very general I/O exception.
import java.io.; class CountBytesNew {*
public static void main (String[] args) throws FileNotFoundException, IOException { FileInputStream in; try{ in = new FileInputStream("FileIn.txt"); int total = 0; while (in.read() != -1) total++; System.out.println("Total = " + total); } catch(FileNotFoundException e1) { System.out.println("FileIn.txt does not exist!"); } catch(IOException e2) { System.out.println("Error occured while read file FileIn.txt"); } }}
There are 2 ways of initialising file stream objects: Passing file name directly to the stream constructor Similar to previous example Passing File Object: Create File Object File inFile = new File( "FileIn.txt"); Pass file object while creating stream: try { in = new FileInputStream( inFile ); }
Reader StringReader CharacterArrayReader
PipedReader
BufferedReader FileInputStream InputStreamReader FileterReader
FileReader PushbackReader
public int read() Reads a character and returns as a integer 0-
public int read(char[] buf, int offset, int count)
Reads and stores the
read.
public boolean() Returns true if the stream is
ready to be read.
public void close() Closes stream
Writer BufferedWriter CharacterArrayWriter
FilterWriter
PrinterWriter PipedWriter OutputStreamWriter StringWriter
FileWriter
public abstract void write(int ch)
public void write(char[] buf, int offset, int count)
public void write(char[] buf)
public void write(String str, int offset, int count)
public void flush() Flushes the stream.
public void close() Closes stream
import java.io.*; public class FileCopy { public static void main (String[] args) { if(args.length != 2) { System.out.println("Error: in sufficient arguments"); System.out.println("Usage - java FileCopy SourceFile DestFile"); System.exit(-1); } try { FileReader srcFile = new FileReader(args[0]); FileWriter destFile = new FileWriter(args[1]); int ch; while((ch=srcFile.read()) != -1) destFile.write(ch); srcFile.close(); destFile.close(); } catch(IOException e) { System.out.println(e); System.exit(-1); } } }
java FileCopy FileIn.txt Fileout.txt
java FileCopy abc Fileout.txt java.io.FileNotFoundException: abc (No such file or directory)
java FileCopy FileIn.txt Error: in sufficient arguments Usage - java FileCopy SourceFile DestFile
Use a BufferedReader to read a file one line at a time and print the lines to standard output
import java.io.*; class ReadTextFile { public static void main(String[] args) throws FileNotFoundException, IOException { BufferedReader in; in = new BufferedReader( new FileReader(“Command.txt”)); String line; while (( line = in.readLine()) != null ) { System.out.println(line); } } }
public class WriteBytes {
public static void main (String[] args) { byte cities[] = {'M', 'e', 'l', 'b', 'o', 'u', 'r', 'n', 'e', '\n', 'S', 'y','d', 'n', 'e', 'y', '\n„ }; FileOutputStream outFile; try{ outFile = new FileOutputStream("City.txt"); outFile.write(cities); outFile.close(); } catch(IOException e) { System.out.println(e); System.exit(-1); } }}