FileNotFoundException JFLE选择器和FileReader JAVA
我一直在编写一段代码,它从JFileChooser获取文件的绝对路径,并使用它通过BufferedReader读取它 代码如下:FileNotFoundException JFLE选择器和FileReader JAVA,java,swing,java-io,filenotfoundexception,Java,Swing,Java Io,Filenotfoundexception,我一直在编写一段代码,它从JFileChooser获取文件的绝对路径,并使用它通过BufferedReader读取它 代码如下: package TestPackage; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOExcep
package TestPackage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.text.ParseException;
import java.util.concurrent.ExecutionException;
import javax.swing.JOptionPane;
/**
*
* @author MRx
*/
public class MainFrame extends javax.swing.JFrame {
/** Creates new form MainFrame */
public MainFrame() {
initComponents();
this.setLocationRelativeTo(null);
}
File f;
String filename;
JFileChooser chooser;
private void openModelActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("TEXT FILES", "txt", "text");
chooser.setFileFilter(filter);
chooser.showOpenDialog(null);
f = chooser.getSelectedFile();
filename = f.getAbsolutePath();
}
private void btnRunActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JOptionPane.showMessageDialog(null, filename);
{
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MainFrame().setVisible(true);
}
});
}
public String readSpecification() {
String spec = "";
// trying to read from file the specification...
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = reader.readLine();
while(line!=null) {
spec += line + "\n";
line = reader.readLine();
}
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
return spec;
}
String modelSpec = readSpecification();
Update在构建对象时,即在选择文件之前,您正在调用
readSpecification
方法。移动线路
String modelSpec = readSpecification();
进入openModelActionPerformed
方法
基于不完整信息的旧答案
文件NotFoundException的消息应包括未找到的文件名。由于它不在那里,程序必须使用空字符串
“
作为文件名,这也是您为filename
变量指定的默认值。这表明您可能正在为此分配使用局部变量:
filename = f.getAbsolutePath();
确保使用了正确的变量
要进行验证,请在打开之前检查要打开的文件:
System.out.println("Trying to open ["+filename+"]");
BufferedReader reader = new BufferedReader(new FileReader(filename));
这无法编译,请发布您实际使用的相关代码。
C:\Users\MRx\Desktop
是一个目录,您不能像打开普通文件一样打开它。你想做什么?这些是相关的部分。我认为问题在这里,在OpenModel方法和readSpecification方法中。new FileReader找不到我在openModelActionPerformed方法中选择的文件。编辑:f.getSelectedFile()和getAbsolutePath()返回我(即C:\Users\MRx\Destop\sample.txt“1)new FileReader(文件名)
最好是..new FileReader(f)
2)…这些是相关的部分。我认为问题出在这里“您认为可能是错误的,大多数人在没有可运行代码的情况下不会仔细研究问题。为了更快地获得更好的帮助,请发布or。使用JOptionPane.showMessageDialog(文件名),我得到的目录路径为“C:\Users\Mrx\Destop\sample.txt”,因此变量filename实际上获取所选文件的绝对路径。但是,如果我返回变量spec,字符串是空的。请用实际使用的代码更新问题。
System.out.println("Trying to open ["+filename+"]");
BufferedReader reader = new BufferedReader(new FileReader(filename));