Added some error handling.
This commit is contained in:
parent
f2e934620b
commit
724359d651
|
@ -7,6 +7,8 @@ import javax.swing.filechooser.FileNameExtensionFilter;
|
||||||
import javax.swing.text.JTextComponent;
|
import javax.swing.text.JTextComponent;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Listener for when a user clicks to load the contents of a file into a text component.
|
* Listener for when a user clicks to load the contents of a file into a text component.
|
||||||
|
@ -32,7 +34,15 @@ public class LoadTextComponentFromFileListener implements ActionListener {
|
||||||
|
|
||||||
int result = fileChooser.showOpenDialog(textComponent);
|
int result = fileChooser.showOpenDialog(textComponent);
|
||||||
if (result == JFileChooser.APPROVE_OPTION) {
|
if (result == JFileChooser.APPROVE_OPTION) {
|
||||||
this.textComponent.setText(FileLoader.readFile(fileChooser.getSelectedFile()));
|
try {
|
||||||
|
this.textComponent.setText(FileLoader.readFile(fileChooser.getSelectedFile()));
|
||||||
|
} catch (IOException exception) {
|
||||||
|
String message = "Could not read file.";
|
||||||
|
if (exception instanceof FileNotFoundException) {
|
||||||
|
message = "File not found.";
|
||||||
|
}
|
||||||
|
JOptionPane.showMessageDialog(this.textComponent, message, "Error Reading File", JOptionPane.WARNING_MESSAGE);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,19 +33,16 @@ public class FileLoader {
|
||||||
* Reads a file into a string.
|
* Reads a file into a string.
|
||||||
* @param selectedFile The file object, as it is often selected by a JFileChooser.
|
* @param selectedFile The file object, as it is often selected by a JFileChooser.
|
||||||
* @return The string containing the file, or an empty string.
|
* @return The string containing the file, or an empty string.
|
||||||
|
* @throws IOException If the file could not be read.
|
||||||
*/
|
*/
|
||||||
public static String readFile(File selectedFile) {
|
public static String readFile(File selectedFile) throws IOException {
|
||||||
try (BufferedReader reader = new BufferedReader(new FileReader(selectedFile))) {
|
BufferedReader reader = new BufferedReader(new FileReader(selectedFile));
|
||||||
String line;
|
String line;
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
sb.append(line);
|
sb.append(line);
|
||||||
sb.append('\n');
|
sb.append('\n');
|
||||||
}
|
|
||||||
return sb.toString();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
return sb.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue