简体   繁体   English

我收到FileNotFoundException

[英]I'm getting FileNotFoundException

I'm getting FileNotFoundException. 我收到FileNotFoundException。 The same function used at other place is working fine. 在其他地方使用的相同功能也可以正常工作。 Please tell me what is wrong with this code: 请告诉我这段代码出了什么问题:

try {
    if (redFolder.isDirectory() && redFile.isFile()) {
        Functions.matched_file_names=new ArrayList<>();
        obj.compare_With_TreeFolder(redFile, redFolder);
        StringBuffer bfr=new StringBuffer();
        for(String item:Functions.matched_file_names)
            bfr.append(item+"\n");
        matchedfileTextArea.setText(bfr.toString());
     } else if(redFile.isFile() && redFolder.isFile()){
         compareTwoTextualFiles cttf=new compareTwoTextualFiles();
         matchedfileTextArea.setText(cttf.compareFiles(redFile, redFolder));
     }
} catch (NullPointerException e) {
    JOptionPane.showMessageDialog(null, "Please Select File First.");
} catch (Exception e) {
   System.out.println("ExceptionCaught. "+e.getMessage());
}

The if block is working fine but the else if block is giving this exception: if块工作正常,但else if块给出此异常:

/Users/esna786/File2.txt
java.io.FileNotFoundException: File2.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at java.io.FileReader.<init>(FileReader.java:58)
    at compareTwoTextualFiles.compareFiles(compareTwoTextualFiles.java:27)
    at comparisonForm$1.valueChanged(comparisonForm.java:58)
    at javax.swing.JTree.fireValueChanged(JTree.java:2926)
    at javax.swing.JTree$TreeSelectionRedirector.valueChanged(JTree.java:3387)
    at javax.swing.tree.DefaultTreeSelectionModel.fireValueChanged(DefaultTreeSelectionModel.java:635)
    at javax.swing.tree.DefaultTreeSelectionModel.notifyPathChange(DefaultTreeSelectionModel.java:1093)
    at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPaths(DefaultTreeSelectionModel.java:294)
    at javax.swing.tree.DefaultTreeSelectionModel.setSelectionPath(DefaultTreeSelectionModel.java:188)
    at javax.swing.JTree.setSelectionPath(JTree.java:1633)
    at javax.swing.plaf.basic.BasicTreeUI.selectPathForEvent(BasicTreeUI.java:2393)
    at javax.swing.plaf.basic.BasicTreeUI$Handler.handleSelection(BasicTreeUI.java:3609)
    at javax.swing.plaf.basic.BasicTreeUI$Handler.mousePressed(BasicTreeUI.java:3548)
    at java.awt.Component.processMouseEvent(Component.java:6522)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4530)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

The else if part is calling this block of code: else if部分正在调用此代码块:

try {
    // Create FileReader & Writer Objects.
    FileReader File1Reader = new FileReader(File1.toString());
    FileReader File2Reader = new FileReader(File2.toString());

    // Create Buffered Object.
    BufferedReader File1BufRdr = new BufferedReader(File1Reader);
    BufferedReader File2BufRdr = new BufferedReader(File2Reader);

    // Get the file contents into String Variables.
    String File1Content = File1BufRdr.readLine();
    String File2Content = File2BufRdr.readLine();

    //New String Builder
    StringBuilder buffer = new StringBuilder();

    // Compare the Contents of the files.
    String startOfComparision = "---------START----------";
    buffer.append(startOfComparision).append("\n");

    boolean isDifferent = false;
    int lineNumber = 1;

    if (File1Content != null || File2Content != null) {

        // Check whether file1 contains data or not
        while ((File1Content != null)) {

            // Check whether file2 contains data or not
            if (((File2Content) != null)) {

                // Check whether both the files have same data in the lines
                if (!File1Content.equals(File2Content)) {
                    buffer.append("Difference in Line " + lineNumber + " :- " + File1.getName() + " contains :" + File1Content + "           " + File2.getName() + " Contains : " + File2Content).append("\n");
                    isDifferent = true;
                }
                lineNumber = lineNumber + 1;
                File2Content = File2BufRdr.readLine();
            } else {
                buffer.append("Difference in Line " + lineNumber + " :- " + File1.getName() + " contains :" + File1Content + "             " + File2.getName() + " Contains - " + File2Content).append("\n");
                isDifferent = true;
                lineNumber = lineNumber + 1;
            }

            File1Content = File1BufRdr.readLine();

        }

        // Check for the condition : if File2 has Data & File1 doesn't contain data.
        while ((File2Content != null) && (File1Content == null)) {
            buffer.append("Difference in Line " + lineNumber + " :- " + File1.getName() + " contains :" + File1Content + "           " + File2.getName() + " Contains - " + File2Content).append("\n");
            isDifferent = true;
            lineNumber = lineNumber + 1;
            File2Content = File2BufRdr.readLine();
        }

    } else {
        // Mention that both the files don't have Data.
        buffer.append(File1.getName() + " and " + File2.getName() + " do not contain any data.");
        isDifferent = true;
    }

    // Check is there any difference or not.
    String endOfComparision = "-----------END----------";
    if (isDifferent) {
        buffer.append(endOfComparision).append("\n");
    } else {
        buffer.append("No Difference Found \nThe Contents Of The Files Are Identical.").append("\n");
        buffer.append(endOfComparision).append("\n");
        Functions.matched_file_names.add("Path: " + File2.getAbsolutePath() + "\nFile Name: " + File2.getName());

    }

    //Close the streams.
    File1Reader.close();
    File2Reader.close();
    File1BufRdr.close();
    File2BufRdr.close();

    float percentage = (float) (getCommonWords(File1, File2) / get_Total_Number_Of_Words(File1)) * 100;

    return buffer.toString() + " \n\nThe Total number of common words of " + File1.getName() + " and " + File2.getName() + " are: " + getCommonWords(File1, File2) + "\n\nThe " + File1.getName() + " is " + Math.ceil(percentage) + " % matched with " + File2.getName() + ".";
} catch (FileNotFoundException e) {
    e.printStackTrace();
    //JOptionPane.showMessageDialog(null, "Please Select Files." + e.getMessage());
}

return null;

Please help me with this issue. 请帮我解决这个问题。

Try printing the path . 尝试打印路径 Check if that's the location you were expecting the file to be in. 检查这是否是您期望文件所在的位置。

I finally resolved it byreplacing the above code as: 我最终通过将上面的代码替换为:

try {
    // Create FileReader & Writer Objects.
    FileReader File1Reader = new FileReader(File1.getAbsolutePath());
    FileReader File2Reader = new FileReader(File2.getAbsolutePath());

暂无
暂无

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

相关问题 进行战争时出现FileNotFoundException - I'm getting FileNotFoundException while running a war 我正在获取HTTPS URL的java.io.FileNotFoundException - I'm getting java.io.FileNotFoundException for HTTPS URL 尝试httpcon.getInputStream()时出现FileNotFoundException。 有没有一种方法可以轻松检查它是否是404网页? - I'm getting a FileNotFoundException while trying to httpcon.getInputStream(). Is there a way to easily check if it's a 404 webpage? 我收到一个错误:“异常 FileNotFoundException 永远不会在相应的 try 语句的主体中抛出”并且无法弄清楚原因 - I'm getting an error: “exception FileNotFoundException is never thrown in body of corresponding try statement” and cannot figure out why 为什么我会收到 FileNotFoundException? - Why am I getting a FileNotFoundException? 我收到 FileNotFoundException 但文件在那里 - I am getting a FileNotFoundException but the file is there 如果我要读取的文件与我正在使用的Java文件位于同一软件包中,为什么我仍然收到“ FileNotFoundException”? - If the file I am trying to read is in the same package as the java file I'm working with, why am I still getting a “FileNotFoundException”? 尝试为我自己的XML样式文件格式编写解析器,并获得FileNotFoundException,特别是我做错了什么吗? - Attempting to write a parser for my own XML style file format, getting FileNotFoundException, is there anything in particular I'm doing wrong? 获取FileNotFoundException - Getting FileNotFoundException 为什么我不断收到FileNotFoundException? - why am I keep getting a FileNotFoundException?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM