UNIT NO 4 Java
UNIT NO 4 Java
● java.io – For reading from and writing to files, memory, and other
I/O sources.
● java.nio – A newer package offering a more modern and scalable
I/O approach, focusing on non-blocking I/O and memory-mapped
files.
// Read a string
System.out.print("Enter your name: ");
String name = scanner.nextLine();
// Read an integer
System.out.print("Enter your age: ");
int age = scanner.nextInt();
// Read a double
System.out.print("Enter your height: ");
double height = scanner.nextDouble();
System.out.println("Hello, " + name + "! You are " + age + " years old
and " + height + " meters tall.");
2. Using BufferedReader
The BufferedReader class is useful when you need efficient reading of
text.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
Example:-
import java.io.*;
public class PrintWriterExample {
public static void main(String[] args) {
try {
PrintWriter writer = new PrintWriter("output.txt");
writer.println("Hello, World!");
writer.println(100);
writer.printf("Formatted number: %.2f", 10.1234);
writer.close();
System.out.println("Data written successfully.");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.*;
public class ByteStreamRead {
public static void main(String[] args) {
try {
FileInputStream input = new FileInputStream("image.jpg");
int byteData;
while ((byteData = input.read()) != -1) {
System.out.print(byteData + " ");
}
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
character streams
character streams use 16-bit Unicode characters and are meant for
processing text. They are different from Byte Streams, which deal with 8-bit raw
bytes.
Common Classes:
b) Writer Classes
Class Description
FileWriter Writes characters to a file.
Writes text to a character-output stream, buffering
BufferedWriter
for efficiency.
Converts character streams into byte streams.
OutputStreamWriter
Useful for different encodings.
CharArrayWriter Writes characters to a character array.
Class Description
StringWriter Writes characters to a string.
Example: Writing to a file using FileWriter
import java.io.FileWriter;
import java.io.IOException;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
try (BufferedReader br = new BufferedReader(new
FileReader("sample.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
● Deserialization
Deserialization is the reverse process of serialization, where the byte
stream is converted back into an object.
Key Points:
⮚ Networking:-
networking in Java is handled using the java.net package, which provides
a powerful set of classes and interfaces for network communication.
Java supports networking through sockets, URLs, HTTP connections, and
more. Here’s an overview of key networking concepts in Java:
import java.net.*;
● UDP Client
import java.net.*;
System.out.println("Message sent.");
} catch (Exception e) {
e.printStackTrace();
}
}
}