File Handling in Java

-- 1 comment
Hello here is a simple java program for file Handling. Here first we store the "java Programming" string into a file named "hi.txt" then we read the "hi.txt" and write it to another file "hello.txt" as well as display in the standard output. Then we read from Standard input and insert it into namaste.txt

Here is the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.io.*;
public class FileHandler {
	public static void main(String args[]) throws IOException{
		System.out.println("Here");

		FileReader reader = null;
		FileWriter writer = null;
		InputStreamReader input = null;

		String message = "Java Programming";
		try {
			writer = new FileWriter("hi.txt");
			int i = 0;
			for(i = 0; i<message.length(); i++) {
				//System.out.println(message.charAt(i));
				int charInt = (int) message.charAt(i);
				writer.write(charInt);
			}
		} catch(Exception e){
			//handle the exception
			e.printStackTrace();
		} finally {
			//Always close reader or writer
			if(writer != null) // prevent another exception
				writer.close();
		}
		try {
			reader = new FileReader("hi.txt");
			writer = new FileWriter("hello.txt");
			int c;
			while((c = reader.read()) != -1) {
				char intChar = (char) c;
				System.out.print(intChar);
				writer.write(c);
			}
			System.out.println();
		} finally {
			if(reader != null)
				reader.close();
			if(writer != null)
				writer.close();
		}
		try {
			input = new InputStreamReader(System.in);
			writer = new FileWriter("namaste.txt");// greeting
			char c;
			System.out.println("Enter q to quite.");
			do {
				c = (char) input.read();
				if (c != 'q') { // won't store q
					writer.write(c);
				}
			} while (c != 'q');
		} finally {
			if(input != null)
				input.close();
			if(writer != null)
				writer.close();
		}
	}
}

We need to declare the variables (6,7,8) outside the try catch block as we need to close them in the finally block if they are used.
We iterate through the String to get each char to write in file (line 14), message.charAt(2) will return the char at index 2 of that String.
(Line 24) we must close the opened file. check it is not null before closing so it won't generate another exception (no file opened to close)
(Line 32) Type casting from int to char.
(Line 49) input.read() waits till users enter a char in the Terminal.

Thank you! Have a good day!

Hello This is Sagar Devkota. Studying Bachelor of Software Engineering at Pokhara University. I know something about Linux, Android, Java, Nodejs, Unity3D and 3 dots :)

1 comment: Leave Your Comments

  1. Vampires in the Enchanted Castle casino - FilmFileEurope
    Vampires in the Enchanted Castle where to order air jordan 18 retro toro mens sneakers Casino. Vampires in the Enchanted 사설토토 Castle Casino. 스포츠토토 통계 벳피스트 Vampires in get air jordan 18 retro men the Enchanted Castle Casino. Vampires in the Enchanted Castle Casino. Vampires in jordan 18 white royal blue shop the Enchanted

    ReplyDelete