5.1 Console and File I/O
I/O
I/O stands for Input and Output.
- Input → data taken from user/file
- Output → data displayed or stored
Types of I/O
| Type | Description |
|---|---|
| Console I/O | keyboard and monitor interaction |
| File I/O | reading/writing files |
Console Output
println()
System.out.println("Hello");
Prints with newline.
print()
System.out.print("Hello");
Prints without newline.
printf()
System.out.printf("%d",10);
Formatted output.
Console Input Using Scanner
import java.util.Scanner;
class Test{
public static void main(String args[]){
Scanner sc =
new Scanner(System.in);
int x = sc.nextInt();
System.out.println(x);
}
}
Scanner Class Methods
| Method | Purpose |
|---|---|
| nextInt() | integer input |
| nextFloat() | float input |
| nextDouble() | double input |
| next() | single word string |
| nextLine() | full line input |
| nextBoolean() | boolean input |
5.2 Opening and Closing Files
File
A file is a collection of data stored permanently.
Opening File
FileReader fr =
new FileReader("a.txt");
Closing File
fr.close();
Important to release resources.
Creating File Object
import java.io.*;
File f = new File("a.txt");
Useful File Methods
| Method | Purpose |
|---|---|
| exists() | checks file existence |
| createNewFile() | creates file |
| delete() | deletes file |
| length() | returns file size |
| getName() | returns filename |
Example
import java.io.*;
class Test{
public static void main(String args[])
throws Exception{
File f = new File("abc.txt");
if(f.exists()){
System.out.println("File Exists");
}
}
}
5.3 Scanner Class
Definition
Scanner class is used to take input from keyboard or file.
Importing Scanner
import java.util.Scanner;
Scanner from Keyboard
Scanner sc =
new Scanner(System.in);
Scanner from File
Scanner sc =
new Scanner(new File("a.txt"));
Complete Scanner Example
import java.util.Scanner;
class Test{
public static void main(String args[]){
Scanner sc =
new Scanner(System.in);
String name =
sc.nextLine();
int age =
sc.nextInt();
System.out.println(name);
System.out.println(age);
}
}
5.4 Byte Streams and Character Streams
Stream
A stream is a flow of data.
Types of Streams
| Type | Used For |
|---|---|
| Byte Stream | binary data |
| Character Stream | text data |
Byte Stream Classes
- FileInputStream
- FileOutputStream
- BufferedInputStream
- BufferedOutputStream
Character Stream Classes
- FileReader
- FileWriter
- BufferedReader
- BufferedWriter
Difference Between Byte Stream and Character Stream
| Byte Stream | Character Stream |
|---|---|
| handles binary data | handles text data |
| 8-bit | 16-bit Unicode |
| InputStream/OutputStream | Reader/Writer |
5.5 Reading and Writing Byte Streams
FileOutputStream
Used to write byte data into file.
Writing Byte Stream
import java.io.*;
class Test{
public static void main(String args[])
throws Exception{
FileOutputStream fout =
new FileOutputStream("a.txt");
String s = "Hello";
byte b[] = s.getBytes();
fout.write(b);
fout.close();
System.out.println("Written");
}
}
FileInputStream
Used to read byte data from file.
Reading Byte Stream
import java.io.*;
class Test{
public static void main(String args[])
throws Exception{
FileInputStream fin =
new FileInputStream("a.txt");
int i;
while((i=fin.read())!=-1){
System.out.print((char)i);
}
fin.close();
}
}
5.6 Reading and Writing Character Streams
FileWriter
Used to write character data.
Writing Character Stream
import java.io.*;
class Test{
public static void main(String args[])
throws Exception{
FileWriter fw =
new FileWriter("a.txt");
fw.write("Hello Java");
fw.close();
}
}
FileReader
Used to read character data.
Reading Character Stream
import java.io.*;
class Test{
public static void main(String args[])
throws Exception{
FileReader fr =
new FileReader("a.txt");
int i;
while((i=fr.read())!=-1){
System.out.print((char)i);
}
fr.close();
}
}
BufferedReader
Used for efficient reading.
BufferedReader Example
import java.io.*;
class Test{
public static void main(String args[])
throws Exception{
BufferedReader br =
new BufferedReader(
new InputStreamReader(System.in));
String s = br.readLine();
System.out.println(s);
}
}
BufferedWriter Example
import java.io.*;
class Test{
public static void main(String args[])
throws Exception{
BufferedWriter bw =
new BufferedWriter(
new FileWriter("a.txt"));
bw.write("Hello");
bw.close();
}
}
5.7 Random Access Files
RandomAccessFile
Used to access file at any position.
Syntax
RandomAccessFile f =
new RandomAccessFile(
"a.txt","rw");
Modes
| Mode | Meaning |
|---|---|
| r | read only |
| rw | read and write |
Writing Using RandomAccessFile
import java.io.*;
class Test{
public static void main(String args[])
throws Exception{
RandomAccessFile f =
new RandomAccessFile(
"a.txt","rw");
f.writeBytes("Hello");
f.close();
}
}
Reading Using RandomAccessFile
import java.io.*;
class Test{
public static void main(String args[])
throws Exception{
RandomAccessFile f =
new RandomAccessFile(
"a.txt","r");
String s = f.readLine();
System.out.println(s);
f.close();
}
}
seek() Method
Moves pointer position.
f.seek(5);
Advantages of RandomAccessFile
- Direct access
- Fast modification
- Efficient for large files
Important I/O Classes
| Class | Purpose |
|---|---|
| Scanner | input |
| File | file operations |
| FileReader | read text file |
| FileWriter | write text file |
| FileInputStream | read byte file |
| FileOutputStream | write byte file |
| BufferedReader | efficient reading |
| BufferedWriter | efficient writing |
| RandomAccessFile | random file access |
Most Important Practical Programs
- Scanner input program
- File creation program
- FileReader program
- FileWriter program
- FileInputStream program
- FileOutputStream program
- BufferedReader program
- BufferedWriter program
- RandomAccessFile program
- seek() method program
Most Important Theory Questions
- What is I/O?
- Difference between byte stream and character stream.
- Explain Scanner class.
- Explain FileReader and FileWriter.
- Explain FileInputStream and FileOutputStream.
- Explain BufferedReader.
- What is RandomAccessFile?
- Explain seek() method.
- Difference between console I/O and file I/O.
Important Viva Questions
Which class is used for keyboard input?
Scanner
Which package contains I/O classes?
java.io
Which stream is used for text data?
Character stream.
Which stream is used for binary data?
Byte stream.
Which class is used for random file access?
RandomAccessFile
Which method closes file?
close()
Which method moves file pointer?
seek()
0 comments:
Post a Comment