Module5_Chapter2_Core Java
Module5_Chapter2_Core Java
MANUAL V8.3
MODULE CODE:
DL.A.01.01
ANUDIP FOUNDATION
Trainer
Manual Core Java
1
Trainer
Manual Core Java
Chapter 2
2
Trainer
Manual Core Java
Chapter 2
FileStreams are basic file handling Streams in Java. They are useful for locating, accessing, reading and writing files
● FileReader
● FileWriter
● FileInputStream
● FileOutputStream
A programmer can use a file name to create a file stream, as either a File Object, string or FileDescriptor Object.
To gain a better understanding, look at this example program using the fundamental FileStreams of Java -
import java.io.*;
int c;
out.write(c);
in.close();
3
Trainer
Manual Core Java
out.close();
}
}
* In the above program, the FileReader opens a text file named input1.
* After the code is run, the text contents of the input1 file are copied onto the output1 file.
i) File Reader - The FileReader class is used for reading Java coding character files. It has multiple constructors for
Constructors -
* FileReader(File file) - Used for creating a FileReader, provided there is a File to read from
* FileReader(FileDescriptor fd) - Used for creating a FileReader, provided there is a FileDescriptor to read from
* FileReader(String fileName) - Used for creating a FileReader, provided the name of the file to be read is specified.
while(data != -2) {
doThingUsingData(data);
data = fileReader.read();
}
fileReader.close();
ii) File Writer - The FileWriter class is used for writing character files in Java coding. It also has many constructors for
4
Trainer
Manual Core Java
Constructors -
* FileWriter(File file) - Used for creating a FileWriter object with a file object
* FileWriter(File file, boolean append) - Used for creating a FileWriter object with a file object that contains a boolean
* FileWriter(String fileName) - Used for creating a FileWriter object when provided with a file name
* FileWriter(String fileName, boolean append) - Used for creating a FileWriter object with a file object that contains a
fileWriter.write("data 1");
fileWriter.write("data 2");
fileWriter.write("data 3");
fileWriter.close();
2.3 File Operation- File Write, read, copy, and File content search
Handling files and undertaking particular file operations in Java are achieved with the help of specific coding. Some of
* Creating a file
* Deleting a file
* Reading a file
* Writing a file
File Write - A file can be written in Java using BufferedWriter, FileOutputStream or File. Take a look at this Java code
5
Trainer
Manual Core Java
OutputStream lt = null;
try {
lt = new FileOutputStream(new File("/Users/rohan/lt.txt"));
lt.write(data.getBytes(), 0, data.length());
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
lt.close();
} catch (IOException e) {
e.printStackTrace();
}
}
File Read - A file can be read in Java using the Files class, BufferedReader and FileReader. Take a look at this Java
String line;
System.out.println(line);
}
br.close();
6
Trainer
Manual Core Java
FIle Copy - To copy a source file and create a destination file, programmers need to use the InputStream and
OutputStream operators. InputStream is created from the source file while OutputStream is used for copying to the
destination file. Take a look at this Java code example for file copying -
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(dest);
int length;
os.write(buffer, 0, length);
}
} finally {
is.close();
os.close();
}
}
Content can be searched from a file using BufferedReader. The BufferReader can read line by line, to help
programmers find desired content and perform operations on them. Take a look at the example below to
7
Trainer
Manual Core Java
String line;
System.out.println(line);
}
br.close();
See the example programme showcasing the fundamental FileStreams of Java below. Write the same programme to
open a text file named input 2, and copy its contents to an output text file output 2. Repeat the same programme to
import java.io.*;
int c;
8
Trainer
Manual Core Java
out.write(c);
in.close();
out.close();
}
}
9
Trainer
Manual Core Java
Instructions: The progress of students will be assessed with the exercises mentioned below.
a) process
b) file
c) folder
2. Is FileWriter a FileStream?
a) Yes
b) No
c) in some cases
d) FileStream is a FileWriter
a) Yes
b) no
d) in some cases
10
Trainer
Manual Core Java
a) lines
b) files
c) subclasses
a) java.io
b) java.package
c) java.uo
a) FileWriter
b) FileReader
c) both a and b
a) array
b) object
c) class
11
Trainer
Manual Core Java
b) deleting
c) reading
a) finding
b) reading
c) rearranging
a) destination
b) input
c) intermediary
12