Programming Java Part12-Java Programming-Lecture Slides, Slides of Java Programming

This lecture is part of lecture series delivered by Narayan Singh for Java Programming course at Cochin University of Science and Technology. It includes: Files, Streams, Operations, Data, Flow, Byte, Character, Abstract, Classes, Reader, Writer, Buffered

Typology: Slides

2011/2012

Uploaded on 07/07/2012

proo
proo 🇮🇳

4.4

(26)

96 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
How Files and Streams Work
Java uses streams to handle I/O operations through which the
data is flowed from one location to another. For example, an
InputStream can flow the data from a disk file to
the internal memory and an OutputStream can flow the
data from the internal memory to a disk file. The disk-file
may be a text file or a binary file. When we work with a
text file, we use a character stream where one character is
treated as per byte on disk. When we work with a binary
file, we use a binary stream.
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Programming Java Part12-Java Programming-Lecture Slides and more Slides Java Programming in PDF only on Docsity!

How Files and Streams Work

Java uses streams to handle I/O operations through which the data is flowed from one location to another. For example, an InputStream can flow the data from a disk file to the internal memory and an OutputStream can flow the data from the internal memory to a disk file. The disk-file may be a text file or a binary file. When we work with a text file, we use a character stream where one character is treated as per byte on disk. When we work with a binary file, we use a binary stream.

Byte Streams

If a program needs to read/write bytes (8-bit data), it could use one of the subclasses of the InputStream or OutputStream respectively.

 import java.io.FileInputStream;  import java.io.FileOutputStream;  import java.io.IOException;  public class BytesCopy {  public static void main(String[] args) throws IOException {  FileInputStream in = null;  FileOutputStream out = null;  try {  in = new FileInputStream("xanadu.txt");  out = new FileOutputStream("outagain.txt");  int c;  while ((c = in.read()) != -1) {  out.write(c);  }  }  finally {  if (in != null) { docsity.com

 

 in.close();  }  if (out != null) {  out.close();  }  }  }  }

Reader and Writer

Working with Reader classes

Java provides the standard I/O facilities for reading text from either the file or the keyboard on the command line. The Reader class is used for this purpose that is available in the java.io package. It acts as an abstract class for reading character streams. The only methods that a subclass must implement are read(char[], int, int) and close(). the Reader class is further categorized into the subclasses.

InputStreamReader

 An InputStreamReader is a bridge from byte streams to character streams i.e. it reads bytes and decodes them into Unicode characters according to a particular platform. Thus, this class reads characters from a byte input stream. When you create an InputStreamReader , you specify an InputStream from which, the InputStreamReader reads the bytes.

 The syntax of InputStreamReader is written as: InputStreamReader <variable_name> = new InputStreamReader(System.in);

BufferedReader

The BufferedReader class is the subclass of the Reader class. It reads character-input stream data from a memory area known as a buffer maintains state. The buffer size may be specified, or the default size may be used that is large enough for text reading purposes.

BufferedReader class provides some standard methods to perform specific reading operations shown in the table. All methods throws an IOException , if an I/O error occurs.

Method (^) TypeReturn Description read( ) int Reads a single character read(char[] cbuf, int off, int len) int^

Read characters into a portion of an array.

readLine( ) String

Read a line of text. A line is considered to be terminated by ( '\n' ). close( ) void Closes the opened stream.

File I/O

 The File class deals with the machine dependent files in a machine-independent manner i.e. it is easier to write platform-independent code that examines and manipulates files using the File class. This class is available in the java.io package.

Create file

import java.io.*;

public class CreateFile { public static void main(String[] args) throws IOException { File f; f= new File("myfile.txt"); if (!f.exists()) { f.createNewFile(); System.out.println("New file "myfile.txt" has been created to the current directory"); } } }

Constructing a File Name path

import java.io.*;

public class PathFile { public static void main(String[] args) throws IOException { File f; f= new File("example" + File.separator + "myfile.txt"); f.createNewFile(); System.out.println("New file "myfile.txt" has been created to the specified location"); System.out.println("The absolute path of the file is: " +f.getAbsolutePath()); } }

import java.io.*;

public class ReadFile{ public static void main(String[] args) throws IOException{ File f; f= new File("myfile.txt"); if (!f.exists()&& f.length()<0) System.out.println("The specified file is not exist"); else { FileInputStream finp= new FileInputStream(f); byte b; do { b=( byte )finp.read(); System.out.print(( char )b); } while (b!=-1); finp.close(); } } }

FileOutputStream

This class is a subclass of OutputStream that writes data to a specified file name. The write() method of this class writes a byte or array of bytes to the file. We typically use this class in conjunction with a BufferedOutputStream and a DataOutputStream class to write binary data. To write text, we typically use it with a PrintWriter , BufferedWriter and an OutputStreamWriter class.