Programming Examples of Java Files
We will see here some programming examples related to Java Files.
Problem 1: Write a program to display the contents of a file to the output stream.
FileTest05.java
// Displaying contents of a file to the output stream
import java.io.*;
public class FileTest05 {
public static void main(String[] args) {
File f;
FileReader in = null;
int c;
try {
f = new File("a.txt");
in = new FileReader(f);
while((c = in.read()) != -1) {
System.out.print((char)c);
}
}
catch(IOException e) {
System.out.println(e);
}
finally {
try {
in.close();
}
catch(Exception e) {
System.out.println(e);
}
}
}
}
Suppose the contents of file “a.txt” is:
It’s Merry Christmas..
Output:
C:\file>java FileTest05
It's Merry Christmas..
Problem 2: Write a program to write keyboard input to a file.
FileTest06.java
// Writing keyboard input to a file
import java.io.*;
public class FileTest06 {
public static void main(String[] args) {
File f;
FileWriter out = null;
char c;
try {
f = new File("output.txt");
out = new FileWriter(f);
System.out.println("Enter characters (type '@' to quit): ");
while((c = (char)System.in.read()) != '@') {
out.write(c);
}
}
catch(IOException e) {
System.out.println(e);
}
finally {
try {
out.close();
}
catch(Exception e) {
System.out.println(e);
}
}
}
}
Output:
C:\file>java FileTest06
Enter characters (type '@' to quit):
Hello from Tech Guru. Website is techguruspeaks.com. Its Good Bye from Me.@
The characters before the ‘@‘ symbol will now be written to “output.txt” file. Now, see this file for checking.
Problem 3: Write a program to merge contents of two files into a third one.
FileTest07.java
// Merging contents of two files into a third one
import java.io.*;
public class FileTest07 {
public static void main(String[] args) {
File f1;
File f2;
File f3;
FileReader in1 = null;
FileReader in2 = null;
FileWriter out = null;
int c;
try {
f1 = new File("a.txt");
f2 = new File("b.txt");
f3 = new File("c.txt");
in1 = new FileReader(f1);
in2 = new FileReader(f2);
out = new FileWriter(f3);
//copy from 1st file
while((c = in1.read()) != -1) {
out.write(c);
}
//copy from 2nd file
while((c = in2.read()) != -1) {
out.write(c);
}
}
catch(IOException e) {
System.out.println(e);
}
finally {
try {
in1.close();
in2.close();
out.close();
}
catch(Exception e) {
System.out.println(e);
}
}
}
}
Suppose the contents of file “a.txt” is:
It’s Merry Christmas..
And of file “b.txt” is:
Enjoy this festive season..
The contents of output file “c.txt” will be:
It’s Merry Christmas.. Enjoy this festive season..
Problem 4: Write a program to display size and length of a file.
FileTest08.java
// Displaying size and length of a file
import java.io.*;
public class FileTest08 {
public static void main(String[] args) throws IOException {
System.out.print("Enter file name : ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
File f = new File(in.readLine());
if (f.exists()) {
// calculating number of lines in the file
FileReader fr = new FileReader(f);
LineNumberReader line = new LineNumberReader(fr);
int count = 0;
while (line.readLine() != null) {
count++;
}
// calculating size of the file
long file_size = f.length();
System.out.println("Size of the file : " + file_size);
System.out.println("Total line no: " + count);
}
else {
System.out.println("File does not exist.");
System.exit(0);
}
}
}
Output:
C:\file>java FileTest08
Enter file name : mytext.txt
Size of the file : 103
Total line no: 4