IOException,找不到文件

时间:2014-10-23 09:00:16

标签: java string file-io ioexception

我希望有人可以解释为什么这不起作用以及我的解决方案可能是什么。

我尝试了以下内容来了解​​会发生什么:

  String s = "\\users\\udc8\\a4471\\My Documents\\MATLAB\\blockdiagram.xml";
  String st = "\\";
  String st2 = st + s;
  System.out.println(st2);

给我以下输出:

\\users\udc8\a4471\My Documents\MATLAB\blockdiagram.xml

这是文件的正确路径。 然后我尝试使用SAX解析此文件,我得到一个IOEXception,说该文件不存在。我尝试过使用File和getPath(),getCanonicalPath()和getAbsolutePath()。 运行解析器时,我得到了msg:

Due to an IOException, the parser could not check \\users\udc8\a4471\My Documents\MATLAB\\blockdiagram.xml

这是开始解析的代码:

try {
      XMLReader parser = XMLReaderFactory.createXMLReader();
      parser.parse(st2);
      System.out.println(s + " is well-formed.");
    }
    catch (SAXException e) {
      System.out.println(s + " is not well-formed.");
    }
    catch (IOException e) { 
      System.out.println(
       "Due to an IOException, the parser could not check " 
       + s
      ); 
    }

运行一个没有自己的messege的类似程序,返回以下错误信息:

java.io.FileNotFoundException: \\users\udc8\a4471\workspace\SAX Intro\users\udc8\a4471\My Documents\MATLAB\blockdiagram.xml (The system cannot find the file specified)

该文件没有特殊限制(据我所知),在查看文件属性时没有任何内容,我可以手动重写内容。 有什么想法吗?

1 个答案:

答案 0 :(得分:0)

也许只是因为你的路径中有两个\ -signs太多了。我建议您尝试使用变量s而不是添加了附加\ -signs的s2。

老实说,我刚刚意识到我对java中的路径知之甚少,特别是当涉及到不同的操作系统时。

然而,我设法让它在如下的Windows机器上运行:

import java.io.File;
import java.io.IOException;

import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

public class SaXSample {

    public static void main(String[] args) {
        new SaXSample().run();
    }

    public void run() {

        System.out.println("This class is located under: "+getAbsolutePath());

        // using absolut pathd
        String absolutPath = "D:\\temp\\Oki.xml";
        File f = new File(absolutPath);
        System.out.println("Does the file exist using the absolut path? -> "+f.exists());
        runSaX(absolutPath);

        // using relative path (i dont know why it knows which drive C:/, D:/ to take but my .class is running from the same drive as the .xml is in)
        String relativePath = "\\temp\\Oki.xml";
        File f2 = new File(relativePath);
        System.out.println("Does the file exist using the relative path? -> "+f2.exists());
        runSaX(relativePath);

        // using a "wrong" relative path:
        String wrongRelativePath = "\\\\temp\\Oki.xml";
        File f3 = new File(wrongRelativePath);
        System.out.println("File relative path: "+f3.getPath()+" , File absolut path: "+f3.getAbsolutePath());
        runSaX(wrongRelativePath);
    }

    private void runSaX(String path) {
        try {
            XMLReader parser = XMLReaderFactory.createXMLReader();
            parser.parse(path);
            System.out.println(path + " is well-formed.");
        } catch (SAXException e) {
            System.out.println(path + " is not well-formed.");
        } catch (IOException e) {
            System.out
                    .println("Due to an IOException, the parser could not check "
                            + path);
        }
    }

    private String getAbsolutePath() {
        java.security.ProtectionDomain pd = SaXSample.class
                .getProtectionDomain();
        if (pd == null)
            return null;
        java.security.CodeSource cs = pd.getCodeSource();
        if (cs == null)
            return null;
        java.net.URL url = cs.getLocation();
        if (url == null)
            return null;
        java.io.File f = new File(url.getFile());
        if (f == null)
            return null;

        return f.getAbsolutePath();
    }

}

在我的情况下导致此输出(对不起,我不知道如何格式化控制台输出):

This class is located under: D:\devjba\jba\bin

Does the file exist using the absolut path? -> true
D:\temp\Oki.xml is well-formed.

Does the file exist using the relative path? -> true
\temp\Oki.xml is well-formed.

File relative path: \\temp\Oki.xml , File absolut path: \\temp\Oki.xml
Due to an IOException, the parser could not check \\temp\Oki.xml

由于您在评论中暗示您希望能够选择某些文件或导演,我建议您查看JFileChooser。这是一种让用户选择实际存在的东西的简单方法,它将为您提供所选文件/文件的绝对路径。

注意:我不知道为什么在相对路径中使用正确的驱动器D:/而不是其他C:/