【问题标题】:Why is my code giving IOException (System could not find the file specified)? [duplicate]为什么我的代码给出 IOException(系统找不到指定的文件)? [复制]
【发布时间】:2015-12-08 10:52:41
【问题描述】:
public static void update(String fileName, String idType, String id, String updatedData[] ) throws Exception{

    char fileOutput[];
    String wholeData;
    String tkn, idtp, idf;      

    File myFileU  = null;
    File tempFile = null;
    FileReader finU = null;
    FileWriter fwU = null;
    Scanner frU = null;



    try{

        finU = new FileReader(myFileU = new File("\\" +fileName + ".txt"));
        fileOutput = new char[(int) myFileU.length()];
        finU.read(fileOutput);
        finU.close();

        //System.out.println(myFileU.getCanonicalPath());

        tempFile = new File("temp.txt");
        tempFile.createNewFile();
        fwU = new FileWriter(myFileU, false);


         wholeData = new String(fileOutput);

        frU = new Scanner(wholeData);

        frU.useDelimiter(";");

        while(frU.hasNext()){   
            idtp = frU.next();
            idf = frU.next();
            if(idtp.equals(idType) && idf.equals(id)){
                fwU.write( ";" + idType + ";" + id);
                for(int i=0; i< updatedData.length; i++){
                    fwU.write(";" + updatedData[i]);

                }
                fwU.write(";" + System.lineSeparator());

                frU.nextLine();
            }
            if(!idf.equals(id))
            fwU.write(";" + idtp + ";" + idf);
            tkn = frU.nextLine();
            fwU.write(tkn);
            fwU.write(System.lineSeparator());
            if(idf.equals(autoSerial(fileName, idType)))
                break;
        }

        fwU.flush();
        fwU.close();


        }
    catch(IOException e){
        System.out.println("error in opening the file U " + e);     
    }
    finally{

    }
}

上述方法旨在覆盖它正在读取的文件。它应该做的是从文件中读取,用更新的数据替换用户指定的记录并用更新的数据覆盖文件,但它不会覆盖文件而是将更新的记录附加到文件的末尾和给出(尽管如果我将数据保存到单独的文件中,它会将更新后的数据正确保存到其中):

java.io.FileNotFoundException: \Schedules.txt (The system cannot find the file specified)

当文件在那里并且它也从中读取数据时?有什么线索吗?我是 Java 新手!

【问题讨论】:

  • 您的文件在哪里?您的文件的完整位置?它给出了异常,因为它找不到它。如果文件位于同一目录中,则删除 \\ 并重试 File(fileName + ".txt")
  • 有一个使用反斜杠的操作系统不是Windows?有趣。

标签: java file-io filenotfoundexception


【解决方案1】:

您的问题显然是使用 Java 打开文件。您似乎对文件路径感到困惑。以下是如何使用不同位置等打开文件的示例。

假设您的文件名为abc.txt,并且位于test_stackoverflow 目录下的C:\ 驱动器中,那么您的路径将如下所示:

FileReader reader = new FileReader(new File("C:\\test_stackoverflow\\abc.txt")); 

注意双斜线,这就是你跳过斜线的方式。

如果您的文件与您的 java 类位于同一目录中,则路径如下所示,没有任何斜杠

FileReader reader = new FileReader(new File("test.txt")); 

假设您要读取的文件是上面的一个文件夹(src),那么您的 java 类就在其中

FileReader reader = new FileReader(new File("src\\test.txt"));

如果您使用的是 OSX,那么您可以在以下几行中执行一些操作

FileReader reader = new FileReader(new File("/Users/Raf/Desktop/abc.txt"));

【讨论】:

  • 原来我写的时候没有反斜杠,但是想删除文件却没有被删除,所以在网上看到它添加了反斜杠
  • 谢谢顺便说一句,现在你能帮我多一点吗
  • 您需要帮助解决的问题似乎是什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-05
相关资源
最近更新 更多