【发布时间】:2014-12-09 23:16:13
【问题描述】:
这个想法是创建一个从文本文件加载所需信息的主屏幕。应用程序查找文件,如果找不到,它会询问文件路径。
到目前为止我有这个代码
BufferedReader br;
try{
br = new BufferedReader(new FileReader(principal));
}catch(FileNotFoundException e){
while(!principal.contains(".INICIO")){
JOptionPane.showMessageDialog(this, "Not a valid file paht", "error",JOptionPane.ERROR_MESSAGE);
JFileChooser archivo = new JFileChooser("Seleccione el archivo principal");
principal = archivo.getSelectedFile().getPath();
}
}
之后,我希望程序使用新的文件路径重试。我的想法是这样的:
while(condition){
try{
br = new BufferedReader(new FileReader(principal));
}catch(Exception e){
//some code here
}
}
但我不知道“条件”是什么。有什么建议吗?
【问题讨论】:
-
为什么不使用
File#exists? -
不确定,如果文件不存在,它会自动抛出异常。如果文件确实存在,它甚至不会尝试做任何事情。