-
Notifications
You must be signed in to change notification settings - Fork 4
/
FileException.java
56 lines (55 loc) · 1.69 KB
/
FileException.java
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
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class FileException {
public static void main(String[] args) {
final String path = "Output.txt";
FileInputStream fis = null;
try {
fis = new FileInputStream(path);
int ch;
String data = "";
while ((ch = fis.read()) != -1) {
data += (char) ch;
}
if (data != "")
System.out.println("File contents : " + data);
else
System.out.println("File Empty");
} catch (FileNotFoundException e) {
System.out.println("File does not already exist.");
} catch (IOException e) {
System.out.println(e);
} finally {
try {
if (fis != null)
fis.close();
} catch (IOException e) {
System.out.println(e);
}
}
Scanner sc = null;
FileOutputStream fout = null;
try {
sc = new Scanner(System.in);
fout = new FileOutputStream(path);
System.out.print("Enter a string to save: ");
String data = sc.nextLine();
byte b[] = data.getBytes();
fout.write(b);
} catch (IOException e) {
System.out.println(e);
} finally {
try {
if (sc != null)
sc.close();
if (fout != null)
fout.close();
} catch (IOException e) {
System.out.println(e);
}
}
}
}