This program demonstrates copying data byte-by-byte using FileInputStream and FileOutputStream.
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
int ch;
try {
fis = new FileInputStream("MyView.txt");
fos = new FileOutputStream("ASectionCSE.txt");
while ((ch = fis.read()) != -1) fos.write(ch);
System.out.println("NBKRIST: File copied successfully!");
} catch (IOException e) {
System.err.println(e.getMessage());
} finally {
try {
if (fis != null) fis.close();
if (fos != null) fos.close();
} catch (IOException e) { System.err.println(e.getMessage()); }
}
}
}
Character streams handle text data, automatically managing Unicode encoding.
import java.io.*;
public class ReaderWriter {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("b.txt");
int data;
while ((data = fr.read()) != -1) fw.write(data);
fr.close();
fw.close();
System.out.println("NBKRIST: Character stream copy done!");
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}
Buffered streams improve efficiency by reducing physical disk access.
import java.io.*;
public class Buffering {
public static void main(String[] args) {
try {
BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
PrintWriter writer = new PrintWriter(new FileWriter("kk.txt"), true);
String line;
while ((line = reader.readLine()) != null) writer.println(line);
reader.close();
writer.close();
System.out.println("NBKRIST: Buffered stream success!");
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
}