0% found this document useful (0 votes)
7 views7 pages

Java Notes Unit VI

This document discusses Java streams and file handling in Java. It explains that streams are used for input and output and describes byte and character streams. It then provides examples of how to create, write to, read from, and delete files using classes like File, FileWriter, Scanner.

Uploaded by

aiden.atz78
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
7 views7 pages

Java Notes Unit VI

This document discusses Java streams and file handling in Java. It explains that streams are used for input and output and describes byte and character streams. It then provides examples of how to create, write to, read from, and delete files using classes like File, FileWriter, Scanner.

Uploaded by

aiden.atz78
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Java Stream Class:-

Java performs I/O through Streams. A Stream is linked to a physical layer by java
I/O system to make input and output operation in java. A stream can be defined as
a sequence of data. The InputStream is used to read data from a source and the
OutputStream is used for writing data to a destination. InputStream and
OutputStream are the basic stream classes in Java.

Types of streams:

• Byte Stream : It provides a convenient means for handling input and output
of byte.
• Character Stream : It provides a convenient means for handling input and
output of characters. Character stream uses Unicode and therefore can be
internationalized.

1. Byte Stream:

Byte Stream Classes are used to read bytes from an input stream and write bytes to
an output stream.

InputStream Classes - These classes are subclasses of an abstract class,


InputStream and they are used to read bytes from a source(file, memory or
console).

OutputStream Classes - These classes are subclasses of an abstract class,


OutputStream and they are used to write bytes to a destination(file, memory or
console).
2. Character Stream:

Character stream is also defined by using two abstract classes at the top of the
hierarchy, they are Reader and Writer. These two abstract classes have several
concrete classes that handle Unicode characters.
• Reader classes : Abstract class that define character stream input.
• Writer classes : Abstract class that define character stream output.
Example:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BufferedInputStreamExample {
public static void main(String args[]) throws IOException {
//Creating an BufferedInputStream object
BufferedInputStream inputStream = new BufferedInputStream(System.in);
byte bytes[] = new byte[1024];
System.out.println("Enter your data ");
//Reading data from key-board
inputStream.read(bytes);
//Creating BufferedOutputStream object
FileOutputStream out= new FileOutputStream("D:/sample.txt");// writer file
BufferedOutputStream outputStream = new BufferedOutputStream(out);
//Writing data to the file
outputStream.write(bytes);
outputStream.flush();
System.out.println("Data successfully written in the specified file");
}
}

Java File Handling:-


The java.io package includes a class known as the File class that provides support
for creating files and directories. The class includes several constructors for
instantiating the File objects. To use the File class, create an object of the class,
and specify the filename or directory name.

Example:

import java.io.File; // Import the File class

File myObj = new File("filename.txt"); // Specify the filename


1. Creating Files:

To create a file in Java, you can use the createNewFile() method. This method
returns a boolean value true if the file was successfully created, and false if the file
already exists. It is necessary to enclose method in try...catch block because it
throws an IOException if an error occurs (if the file cannot be created for some
reason).

Example:

import java.io.File; // Import the File class


import java.io.IOException; // Import the IOException class to handle errors

public class CreateFile {


public static void main(String[] args) {
try {
File f = new File("filename.txt");
if (f.createNewFile()) {
System.out.println("File created: " + f.getName());
} else {
System.out.println("File already exists.");
}
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

2. Writing to the Files:

The FileWriter class together with its write() method to write some text to the file
we created. Note that when you are done writing to the file, you should close it
with the close() method.

Example:

import java.io.FileWriter; // Import the FileWriter class


import java.io.IOException; // Import the IOException class to handle errors
public class WriteToFile {
public static void main(String[] args) {
try {
FileWriter w = new FileWriter("filename.txt");
w.write("Welcome to java programming");
w.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
}

3. Reading Files:

The Scanner class is used to read the contents of the file we created in java.
Creating object for Scanner class to fetch and read the file contents.

Example:

import java.io.File; // Import the File class


import java.io.FileNotFoundException; // Import this class to handle errors
import java.util.Scanner; // Import the Scanner class to read text files

public class ReadFile {


public static void main(String[] args) {
try {
File myObj = new File("filename.txt");
Scanner myReader = new Scanner(myObj);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
} catch (FileNotFoundException e) {
System.out.println("An error occurred.");
e.printStackTrace();
} }}
4. Deleting a File:

In File class delete() method is used to delete the created file in java. The object for
File class is created to delete the file in java.

Example:

import java.io.File; // Import the File class

public class DeleteFile {


public static void main(String[] args) {
File f = new File("filename.txt");
if (f.delete()) {
System.out.println("Deleted the file: " + f.getName());
} else {
System.out.println("Failed to delete the file.");
}
}
}

You might also like