-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIOFile.java
More file actions
80 lines (79 loc) · 2.66 KB
/
IOFile.java
File metadata and controls
80 lines (79 loc) · 2.66 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.JOptionPane;
public class IOFile {
public static String errorMessage = String.format("wrong!!!\nplease try again...");
//this method take a path from user and return its String value
public static String getPath() {
Path x = Paths.get(JOptionPane.showInputDialog(null, "please enter your text file directory"));
return x.toString();
}
//this method write the argument in a binary file
public static void binaryWrite(String x) {
try {
FileOutputStream fileOpener = new FileOutputStream(getPath());
ObjectOutputStream writer = new ObjectOutputStream(fileOpener);
writer.writeObject(x);
writer.close();
fileOpener.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null,errorMessage, "ERROR",JOptionPane.ERROR_MESSAGE);
Main.askForFileWriting(x);
}
}
//this method read a binary file and return its content
public static String binaryRead() {
String x = null;
try {
FileInputStream fileOpener = new FileInputStream(getPath());
ObjectInputStream reader = new ObjectInputStream(fileOpener);
x = (String) reader.readObject();
reader.close();
fileOpener.close();
return x;
} catch (IOException e) {
JOptionPane.showMessageDialog(null,errorMessage, "ERROR", JOptionPane.ERROR_MESSAGE);
return Main.askForFileReading();
} catch (ClassNotFoundException e) {
JOptionPane.showMessageDialog(null, errorMessage,"ERROR",JOptionPane.ERROR_MESSAGE);
return Main.askForFileReading();
}
}
//this method write the argument in a character file
public static void textWrite(String x) {
try {
FileWriter fileOpener = new FileWriter(getPath());
BufferedWriter writer = new BufferedWriter(fileOpener);
writer.write(x);
writer.close();
fileOpener.close();
} catch (IOException e) {
JOptionPane.showMessageDialog(null, errorMessage, "ERROR", JOptionPane.ERROR_MESSAGE);
Main.askForFileWriting(x);
}
}
//this method read a character file and returns its content
public static String textRead() {
String x = null;
try {
FileReader fileOpener = new FileReader(getPath());
BufferedReader reader = new BufferedReader(fileOpener);
x = reader.readLine();
reader.close();
fileOpener.close();
return x;
} catch (IOException e) {
JOptionPane.showMessageDialog(null, errorMessage, "ERROR", JOptionPane.ERROR_MESSAGE);
return Main.askForFileReading();
}
}
}