Java I/O Streams: Understanding Input and Output Operations, Lecture notes of Java Programming

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

2017/2018

Uploaded on 09/02/2018

Hanadi1
Hanadi1 🇱🇧

3 documents

1 / 36

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java I/O : Files Handling
Chapter 3
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24

Partial preview of the text

Download Java I/O Streams: Understanding Input and Output Operations and more Lecture notes Java Programming in PDF only on Docsity!

Java I/O : Files Handling

Chapter 3

  • (^) Java I/O (Input and Output) is used to process

the input and produce the output.

  • (^) Java uses the concept of stream to make I/O

operation fast.

  • (^) The java.io package contains all the classes

required for input and output operations.

  • (^) We can perform file handling in java by Java I/O

API.

Stream Types  (^) Standard Streams

 All the programming languages provide support for

standard I/O where the user's program can take input

from a keyboard and then produce an output on the

computer screen

 Java provides the following three standard streams

1. Standard Input : System.in.

2. Standard Output : System.out.

3. Standard Error :System.err

 (^) Binary Stream

 Java byte streams are used to perform input and output of 8-

bit bytes

1. FileInputStream and FileOutputStream

2. BufferedInputStream and BufferedOutputStream

3. DataInputStream and DataOutputStream

 (^) Character Stream

 Streams are used to perform input and output for 16-bit

unicode

1. FileReader, FileWriter,

2. BufferedReader, BufferedWriter

3. PrintWriter

4. InputStreamReader and OutputStreamWriter.

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

Java application uses an output stream to write data

to a destination, it may be a file, an array, peripheral

device or socket.

 (^) InputStream

Java application uses an input stream to read data

from a source, it may be a file, an array, peripheral

device or socket.

FileInputStream

 (^) 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

  • (^) The Java.io.FileOutputStream clas s is an output stream for writing data to a File
  • (^) This class is meant for writing streams of raw bytes such as image data
  • (^) If you have to write primitive values into a file, use FileOutputStream class

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(); } } }