




























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
An overview of java i/o streams, explaining how they are used for fast input and output operations in java. It covers various types of byte streams, character streams, and filtered streams, and discusses their applications in reading and writing files, terminal input and output, and communicating through sockets. The document also includes code examples using fileinputstream, fileoutputstream, bufferedinputstream, bufferedoutputstream, datainputstream, and dataoutputstream.
Typology: Lecture notes
1 / 36
This page cannot be seen from the preview
Don't miss anything!





























Stream Types (^) Standard Streams
(^) Binary Stream
(^) Character Stream
Constructors File(File parent, String child) This constructor creates a new File instance from a parent abstract pathname and a child pathname string. File(String pathname) This constructor creates a new File instance by converting the given pathname string into an abstract pathname. File(String parent, String child) This constructor creates a new File instance from a parent pathname string and a child pathname string. File(URI uri) This constructor creates a new File instance by converting the given file: URI into an abstract pathname.
import java.io.File; import java.io.IOException; public class Reading { File f = null; public Reading(){ f = new File("C:\Users\user\Desktop\myFile.txt"); try { f.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(f.exists()) System. out.println("file exsit"); else System. out.println("not found"); } public static void main(String []args){ new Reading(); }} import java.io.File; import java.io.IOException; public class Reading { File f = null; public Reading(){ f = new File("C:\Users\user\Desktop\myFile.txt"); try { f.createNewFile(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(f.exists()) System. out.println("file exsit"); else System. out.println("not found"); } public static void main(String []args){ new Reading(); }}
Binary Stream (^) InputStream and OutputStream are abstract c lasses that define the lowest-level interface for all byte streams. (^) They contain methods for reading or writing an unstructured flow of byte-level data. (^) OutputStream
(^) InputStream
(^) The java.io.FileInputStream class obtains input bytes from a file in a file system. What files are available depends on the host environment. (^) It extends abstract class InputStream (^) Constructors (^) FileInputStream(File file) This creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.Throws FileNotFoundException if file not found. (^) FileInputStream(FileDescriptor fdObj) This creates a FileInputStream by using the file descriptor fdObj , which represents an existing connection to an actual file in the file system. (^) FileInputStream(String name) This creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.
import java.io.FileInputStream; import java.io.FileNotFoundExceptio n; import java.io.IOException; public class Reading1 { FileInputStream input ; int i=0; char c; public Reading1(){ try { input = new FileInputStream("C:\ myFile.txt"); } catch (FileNotFoundException e) { import java.io.FileInputStream; import java.io.FileNotFoundExceptio n; import java.io.IOException; public class Reading1 { FileInputStream input ; int i=0; char c; public Reading1(){ try { input = new FileInputStream("C:\ myFile.txt"); } catch (FileNotFoundException e) { try { while((i=input.read())!=-1){ c = ( char)i; System. out.print(c);} } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(input != null) try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String []args){ try { while((i=input.read())!=-1){ c = ( char)i; System. out.print(c);} } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(input != null) try { input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String []args){
FileOutputStrea m
Methods Method Description protected void finalize() It is sued to clean up the connection with the file output stream. void write(byte[] ary) It is used to write ary.length bytes from the byte array to the file output stream. void write(byte[] ary, int off, int len) It is used to write len bytes from the byte array starting at offset off to the file output stream. void write(int b) It is used to write the specified byte to the file output stream. FileChannel getChannel() It is used to return the file channel object associated with the file output stream. FileDescriptor getFD() It is used to return the file descriptor associated with the stream. void close() It is used to closes the file output stream.
import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Writing { FileOutputStream output; byte[] b = {65,66,67,68,69}; public Writing(){ try { output = new FileOutputStream("C:\myFile.txt"); output.write(b); output.flush(); if(output !=null) output.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Writing { FileOutputStream output; byte[] b = {65,66,67,68,69}; public Writing(){ try { output = new FileOutputStream("C:\myFile.txt"); output.write(b); output.flush(); if(output !=null) output.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }