forked from shah-smit/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewer.java
More file actions
executable file
·85 lines (64 loc) · 1.92 KB
/
Viewer.java
File metadata and controls
executable file
·85 lines (64 loc) · 1.92 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
81
82
83
84
85
package io;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
public class Viewer {
JTextArea text;
class ButtonListener implements ActionListener {
public void actionPerformed(ActionEvent arg0) {
write(text.getText(), "MeModified.java");
}
}
public void go(String file){
show(read(file));
}
private void show(String str) {
text = new JTextArea();
text.setText(str);
text.setEditable(false);
JScrollPane scroller = new JScrollPane(text);
scroller
.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scroller
.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scroller.setPreferredSize(new Dimension(800, 800));
JPanel panel = new JPanel();
panel.add(scroller);
JButton button = new JButton("Save");
button.addActionListener(new ButtonListener());
JFrame frame = new JFrame();
frame.getContentPane().add(BorderLayout.CENTER, panel);
frame.getContentPane().add(BorderLayout.SOUTH, button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(900, 900);
frame.setVisible(true);
}
private String read(String file) {
String fileAsString = null;
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuffer stringBuff = new StringBuffer();
String line = null;
while ((line = reader.readLine()) != null)
stringBuff.append(line).append("\n");
reader.close();
fileAsString = stringBuff.toString();
} catch (IOException e) {
System.out.println("Could not read file");
}
return fileAsString;
}
private void write(String str, String file) {
try {
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(str, 0, str.length());
out.close();
} catch (IOException e) {
System.out.println("Could not write to file");
}
}
public static void main(String[] args) {
new Viewer().go(args[0]);
}
}