代码之家  ›  专栏  ›  技术社区  ›  FrinkTheBrave

试图读取已写入的文件时发生FileNotFoundException

  •  1
  • FrinkTheBrave  · 技术社区  · 14 年前

    我正试图将一个对象(PilotRecord)写入一个文件,然后重新读取它。我知道我不需要指定路径,因为它是我的应用程序的内部路径,所以如果卸载应用程序,我希望删除所有文件。

    以下是我的代码:

        fileoutputstream = openFileOutput("test1", Context.MODE_WORLD_WRITEABLE);
        Log.d(this.getClass().getName(), "loadPilotRecord: "+fileoutputstream.toString());
        objectoutputstream = new ObjectOutputStream(fileoutputstream);
        Log.d(this.getClass().getName(), "loadPilotRecord: "+objectoutputstream.toString());
        objectoutputstream.writeObject(pilotRecord);
        objectoutputstream.close();
        fileoutputstream.close();
    
        fileinputstream = new FileInputStream("test1");
        Log.d(this.getClass().getName(), "loadPilotRecord: "+fileinputstream.toString());
        objectinputstream = new ObjectInputStream(fileinputstream);
        Log.d(this.getClass().getName(), "loadPilotRecord: "+objectinputstream.toString());
        pilotRecord = (PilotRecord)objectinputstream.readObject();
        objectinputstream.close();
        fileinputstream.close();
    

    我的问题是,在上面的代码中,我在下面一行得到了一个fileNotFoundException: fileinputstream=new fileinputstream(“test1”); 我真的不知道如何找出它使用的路径,或者可能有一个更明显的问题,我只是看不到。 对不起,如果这是一个有点基础,但我仍在努力找到我的脚。 log.d语句只输出类名和ID。

    蒂亚

    • 弗林克
    2 回复  |  直到 11 年前
        1
  •  2
  •   plagelao    14 年前

    您是否尝试过OpenFileInput(“test1”)而不是New FileInputStream(“test1”)?

        2
  •  1
  •   Paul Richter    11 年前

    要找出实际使用的路径,请尝试:

    File f = new File("test1");
    Log.d(this.getClass().getName(), f.getAbsolutePath());
    

    如果文件是真的创建的,请查看这个位置-如果不是,您将无法读取。

    编辑:用flush删除了猜测,这是非常荒谬的。