-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileReaderController.java
More file actions
103 lines (77 loc) · 2.4 KB
/
Copy pathfileReaderController.java
File metadata and controls
103 lines (77 loc) · 2.4 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
* Andrew Pereira
* August 12, 2020
*/
package JavaFXAPIFileReader;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.*;
public class fileReaderController {
@FXML
private Button btView;
@FXML
private TextField inputText;
@FXML
private TextArea outputText;
@FXML
private Button btClear;
@FXML
private Label errorOutput;
@FXML
private MenuItem btClose;
@FXML
void clearHandler(ActionEvent event) {
outputText.setText("");
}
@FXML
void viewHandler(ActionEvent event) {
try {
readInput(); //calls method to read input file
writeOutput(); //calls method to write file
errorOutput.setText(""); //removes error message if there was an error shown
}
catch (Exception e){
errorOutput.setText("File not found");
System.out.println("Error: " + e);
outputText.setText("");
}
}
@FXML
void openHandler(ActionEvent event){
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Choose a file"); //user can choose file from C drive
File file = fileChooser.showOpenDialog(new Stage());
if (file != null){
inputText.setText("" + file.getName());
errorOutput.setText("");
}
else{
errorOutput.setText("File not found");
}
}
@FXML
void closeHandler(ActionEvent event) {
inputText.setText("");
outputText.setText("");
}
/**
*
* @throws IOException
*/
void readInput() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File(inputText.getText())));
String line;
while ((line = reader.readLine()) != null) {
outputText.appendText(line + "\n");}
} //method for reading input from txt file
void writeOutput() throws IOException {
File outFile = new File("BagelShopReceipt.txt");
FileOutputStream outFileStream = new FileOutputStream(outFile);
PrintWriter outStream = new PrintWriter(outFileStream);
outStream.write(String.valueOf(outputText.getText()));
outStream.close();
} //method for writing contents of selected file to new file. I chose a new filename.
}