File Io 2023

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

CHAPTER 6

File I/O
Introduction
 Data stored in variables and arrays is temporary, it is lost when a
local variable goes out of scope or when the program terminates.
 Computers use files to maintain the retention of data after the
termination of the program.
 These files are stored in secondary storage devices including hard
disks DVDs CDs and more.
 So how is file handled in java?
 Java has its own file architecure
Files and Streams

 Java views each file as a sequential stream of bytes


 Every operating system provides a mechanism to determine the end of
a file, such as an end-of-file marker or a count of the total bytes in the
file that’s recorded in a system-maintained administrative data
structure.
 A Java program processing a stream of bytes simply receives an
indication from the operating system when it reaches the end of the
stream
Byte-Based vs Character-Based files

 File streams can be used to input and output data as bytes or characters.
 Byte-based streams output and input data in its binary format—a char is
two bytes, an int is four bytes, a double is eight bytes, etc.
 Character-based streams output and input data as a sequence of
characters in which every character is two bytes—the number of bytes for
a given value depends on the number of characters in that value.
 For example, the value 2000000000 requires 20 bytes (10 characters
at two bytes per character) but the value 7 requires only two bytes (1
character at two bytes per character)
Classes in Java when working with Files
 File and Directory Info
 FileInputStream and FileOutputStream
 ByteArrayInputStream and ByteArrayOutputStream
 FileReader and FileWriter
File and directory info

 We use classes and interfaces in java.nio.file package in order to


retrieve information about files and directories
 Path <interface> used to represent locations of file or directory
 Paths <class> provide static methods used to get path object
representing a file or directory
 Relative path vs Absolute path
 Files<class> provides static methods for common file and directory
manipulations, such as copying files; creating and deleting files and
directories; getting information about files and directories; reading
contents of files; and more.
 DirectoryStream <interface> objects of classes that implement this
interface can iterate through contents of a directory
Classes for
Reading and Writing to Files
in Java
FileInputStream
 FileInputStream is a class in java used for for byte-based input from a file.
 FileInputStream
– Create stream
• FileInputStream input = new FileInputStream(stringPath);
– Read
• Read() - reads a single byte
• read(byte[] array) – reads bytes and store in the array

• read(byte[] array, int start, int length)


– Available() - returns the number of bytes in a file
– skip(int length) – skips the specified number of bytes
– close()

FileOutputStream
 Java FileOutputStream is an output stream used for writing data to a
file.
 Used to write primitive values into a file.
 You can write byte-oriented as well as character-oriented data
through FileOutputStream class. But, for character-oriented data, it is
preferred to use FileWriter than FileOutputStream.
Cont.
 FileOutputStream
– Create
• FileOutputStream output = new FileOutputStream(String path, boolean value);
– If value is true the new content is appended, otherwise it will override the content in the file
– Write
• write(byte) writes a single byte to a file
• write(byte[] array)
• write(byte[] array, int start, int length)
– Flush()
• It writes all data in the output stream to the destination
– Close()
ByteArrayInputStream Class
 It can be used to read byte array as input stream.
 Java ByteArrayInputStream class contains an internal buffer which is
used to read byte array as stream. In this stream, the data is read from a
byte array.
• Create

ByteArrayInputStream input = new ByteArrayInputStream(byte[] arr);


– ByteArrayInputStream input = new ByteArrayInputStream(byte[]
arr, int start, int length);
• Reading
– Read(), read(byte[] arr), read(byte[] arr, int start, int length)
• Available()
ByteArrayOutputStream
maintains an internal array of bytes to store the data (default size
is 32 bytes)
• Create
– ByteArrayOutputStream out = new ByteArrayOutputStream();
– ByteArrayOutputStream out = new ByteArrayOutputStream(int size);
• Write
– write(int byte), write(byte[] arr), write(byte[] arr, int start, int length)
– The getBytes() method used converts a string into an array of bytes
 Accessing data from byte array output stream
– toByteArray() - returns the array present inside the output stream
– toString() - returns the entire data of the output stream in string form
Reader & Writer
 Input Stream Reader
– Used to convert data in bytes to data in characters
– It is a bridge b/n byte streams and character streams
•Create
–FileInputStream file = new FileInputStream(String path);
–InputStreamReader input = new InputStreamReader(file);
–InputStreamReader input = new InputStreamReader(file, Charset cs);
•Read
–Read(), read(char[] arr), read(char[] arr, int start, int length)
•GetEncoding()
–Used to get the type of encoding used to store the data
•close
Cont...
 Output Stream Writer
– Used to convert data in character form to data in byte to character
•Create
–FileOutputStream file = new FileOutputStream(String path);
–OutputStreamWriter output = new OutputStreamWriter(file);
•write
–write(), write(char[] arr), write(String data)
•GetEncoding()
–Used to get the type of encoding used to store the data
•close
Cont...
 File Reader
– Used to read data (in characters) from file
•Create
–FileReader input = new FileReader(String name);
•read
–Read(), read(char[] arr), read(char[] arr, int start, int length)
•GetEncoding()
–Used to get the type of encoding used to store the data
•close
Cont...
 File Writer
– Used to write data in character form to file
•Create
–FileWriter output = new FileWriter(String name);
•write
–write(), write(char[] arr), write(String data)
•GetEncoding()
–Used to get the type of encoding used to store the data
•close

You might also like