为什么在我的 Java 程序中无法导入文件?

发布于 2024-10-01 17:28:23 字数 882 浏览 1 评论 0原文

我不确定为什么这段代码不允许我选择文件然后扫描它。我该如何调试它?

private String[][] importMaze(){
    String fileName;
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
          fileName = fc.getSelectedFile().getName();

    File f = new File(fileName);
    try {
        Scanner scan = new Scanner(f);
        int rows = scan.nextInt();
        int columns = scan.nextInt();
        String [][] maze = new String[rows][columns];
        int r = 0;
        while(scan.hasNext() && r<=rows){
            for(int c = 0; c<=columns;c++){
                maze[r][c]=scan.next();
            }
            r++;
        }
        return maze;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
    return null;
}

I'm not sure why this code won't allow me to choose a file and then scan it. How can I debug it?

private String[][] importMaze(){
    String fileName;
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showOpenDialog(null);
    if(returnVal == JFileChooser.APPROVE_OPTION) {
          fileName = fc.getSelectedFile().getName();

    File f = new File(fileName);
    try {
        Scanner scan = new Scanner(f);
        int rows = scan.nextInt();
        int columns = scan.nextInt();
        String [][] maze = new String[rows][columns];
        int r = 0;
        while(scan.hasNext() && r<=rows){
            for(int c = 0; c<=columns;c++){
                maze[r][c]=scan.next();
            }
            r++;
        }
        return maze;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
    return null;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别靠近我心 2024-10-08 17:28:23

我已经尝试过您的代码,它会打开对话框,您可以选择一个文件。

我认为你的问题出在这里:

if(returnVal == JFileChooser.APPROVE_OPTION) {
      fileName = fc.getSelectedFile().getName();

File f = new File(fileName);

以下代码:

fileName = fc.getSelectedFile().getName();

仅返回文件的名称,而不是完整的文件路径。这反过来会导致

File f = new File(fileName);

不打开您想要的文件,而是简单地“创建”文件(直到您将其写出为止,它实际上不会创建文件)。

您需要做的是将这三行替换为:

if(returnVal == JFileChooser.APPROVE_OPTION) {
  File f = fc.getSelectedFile();

这将使 f 引用您选择的文件。

I have tried your code and it gets to the point where the dialog opens and you can select a file.

I think your problem lies here:

if(returnVal == JFileChooser.APPROVE_OPTION) {
      fileName = fc.getSelectedFile().getName();

File f = new File(fileName);

The following code:

fileName = fc.getSelectedFile().getName();

returns only the NAME of the file, not the full file path. This in turn causes

File f = new File(fileName);

to not open the file you want it to, but to simple "create" (it does not actually create the file until you write it out) the file.

What you need to do is replace those three line with:

if(returnVal == JFileChooser.APPROVE_OPTION) {
  File f = fc.getSelectedFile();

That would make f reference the file you chose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文