简体   繁体   English

访问程序文件时出现Java fileNotFoundException

[英]Java fileNotFoundException when accessing program files

I cannot seem to by pass this error for the life of me. 我似乎无法一辈子都忽略这个错误。 In my previous post i was trying to update attributes to an xml file. 在我以前的文章中,我试图将属性更新为xml文件。 I can read the file just fine, but when I try and write to it I get a file not found exception. 我可以很好地读取文件,但是当我尝试对其进行写入时,却得到了文件未发现异常。

The program doesn't have a problem with reading the XML file and finding the attribute only writing to it. 该程序在读取XML文件和查找仅写入该文件的属性时没有问题。 After trouble shooting this for awhile, it seems to be an issue with having the file in the Program Files directory. 经过一段时间的故障排除后,将文件放在Program Files目录中似乎是一个问题。 If I move the xml file to C:\\Temp\\test.xml I can write to it without any issues. 如果将xml文件移动到C:\\ Temp \\ test.xml,则可以毫无问题地写入该文件。 As soon as it goes into a folder with any type of spaces it cannot seem to find it. 一旦进入带有任何类型空格的文件夹,它似乎就找不到了。 Seems like an issue with the StreamResults. 似乎与StreamResults有关。

        File file = new File(filePath); 
        document = documentBuilder.parse(file);
        NodeList sessionNodelist = document.getElementsByTagName("session");

      if (sessionNodelist.getLength() > 0)
        {
            Element sessionElement = (Element) sessionNodelist.item(0);
            sessionElement.setAttribute("timeout", "12");
            sessionElement.setAttribute("warning", "10");   
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(document);
            try{
            StreamResult result = new StreamResult(file);
            transformer.transform(source, result);
            }catch(Exception e)
            {   
                logger.info(e.getMessage());
            }
        }

java.io.FileNotFoundException: C:\\Program%20Files\\Test.xml (The system cannot find the path specified) java.io.FileNotFoundException: C:\\Program%20Files\\Test.xml (系统找不到指定的路径)

I am not exactly sure how to get around this error. 我不确定如何解决此错误。 You would think if it can read to it and find it in the first File call, the second file call should work right? 您会认为,如果它可以读取并在第一个File调用中找到它,那么第二个文件调用应该可以正常工作吗?

UPDATE: I tried some other methods. 更新:我尝试了其他方法。

So when i have the file path set to "C:\\Program Files\\test.xml" File.exists returns ture, along with read and writing. 因此,当我将文件路径设置为“ C:\\ Program Files \\ test.xml”时,File.exists返回ture,以及进行读写操作。 if I add the %20 to the program file path, they all return false" Eg C:\\Program%20Files\\test.xml. 如果我将%20添加到程序文件路径,它们都将返回false”,例如C:\\ Program%20Files \\ test.xml。

So document = documentBuilder.parse(file); 所以document = documentBuilder.parse(file); can parse the file just perfectly fine. 可以很好地解析文件。

When StreamResults trys to open the file, it thoes the file not found error and displays the %20 in the Program files name. 当StreamResults尝试打开文件时,它会显示文件未找到错误,并在程序文件名中显示%20。

StreamResult result = new StreamResult(file);
transformer.transform(source, result);

java.io.FileNotFoundException: C:\\Program%20Files\\Test.xml (The system cannot find the path specified) java.io.FileNotFoundException: C:\\Program%20Files\\Test.xml (系统找不到指定的路径)

Is there another way to stream the results to the xml file instead of StreamResults? 还有另一种方法将结果流传输到xml文件而不是StreamResults吗?

I figured it out. 我想到了。 After doing a ton of reading about other people having a simular problem i had to do the following work around in order for it to work correctly. 在阅读了大量有关其他人遇到类似问题的信息后,我不得不做以下工作以使其正常工作。

  StreamResult result = new StreamResult(file.getPath());
  transformer.transform(source, result);

It now works. 现在可以使用了。 Odd, but it works. 奇怪,但是可以。

Instead of Using : 而不是使用:

String filePath = "C:\Program%20Files\Test.xml";

Use this : 用这个 :

String filePath = "C:\\Program%20Files\\Test.xml";

The problem lies with parsing the "\\" charatecter 问题在于解析“ \\”字符

EDIT : I do not have that much of experirnce with Java's File I/O But Here is what i found : 编辑:我对Java的文件I / O经验不足,但这是我发现的内容:

File file = new File(filePath);
System.out.println(file.canRead());  // false
System.out.println(file.canWrite());  // false

This might be the reason behind the problem here (Excpert 's wisdom neede here for clarification). 这可能是此问题背后的原因(此处需要Excpert的智慧来澄清)。

It seems that File cannot locate the file, I think because of a problem in the path. 我认为,由于路径问题, File无法找到文件。

The path could be relative or absolute. 路径可以是相对的或绝对的。 You could try to make it relative, and check back in if it worked... 您可以尝试使其相对,然后再检查是否有效...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 访问资产文件时出现 FileNotFoundException - FileNotFoundException on accessing assets files 尝试使用输入和输出文件通过CMD运行Java程序时获取FileNotFoundException - Getting FileNotFoundException when trying to run a java program through CMD using input and output files Android 中的 java.io.FileNotFoundException 访问 Google Drive 上的文件 - java.io.FileNotFoundException in Android accessing files on Google Drive 编译Java程序文件notfoundexception - Compiling java program filenotfoundexception 安装到C:/ Program Files时,Java应用程序无法写入/读取序列化文件-java.io.FileNotFoundException :(拒绝访问) - Java application cannot write/read serialization file when installed to C:/Program Files - java.io.FileNotFoundException: (Access is denied) 访问jar中的json文件时出现java.io.FileNotFoundException - java.io.FileNotFoundException when accessing json file in jar 启动Java时出现FileNotFoundException - FileNotFoundException when starting java 从JSP访问文件时的FileNotFoundException,但如果从普通的Java应用程序访问,则有效 - FileNotFoundException when accessing a file from JSP but works if accessed from plain java application 尝试在android物理设备中访问localhost文件时出现java.io.FileNotFoundException - java.io.FileNotFoundException when trying accessing localhost file in android physical device 运行Java程序时出现FileNotFoundException错误 - FileNotFoundException error while running java program
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM