Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/370.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FileOutputStream创建文件,但FileInputStream_Java_Android_File_Exception_Fileinputstream - Fatal编程技术网

Java FileOutputStream创建文件,但FileInputStream

Java FileOutputStream创建文件,但FileInputStream,java,android,file,exception,fileinputstream,Java,Android,File,Exception,Fileinputstream,我正在使用FileOutputStream在不是我的main活动的活动中创建文件。文件已创建,当我销毁该活动时,会写入所需的数据,但当我从myMainActivity重新启动该活动时,找不到该文件。我可以在代码中更改哪些内容,以避免出现fileNotFoundException?相关代码如下: try { fis = new FileInputStream("words"); ois = new ObjectInputStream(fis); } catch (FileNotFoundExc

我正在使用
FileOutputStream
在不是我的
main活动的活动中创建文件。文件已创建,当我销毁该活动时,会写入所需的数据,但当我从my
MainActivity
重新启动该活动时,找不到该文件。我可以在代码中更改哪些内容,以避免出现
fileNotFoundException
?相关代码如下:

try {
 fis = new FileInputStream("words");
 ois = new ObjectInputStream(fis);
} catch (FileNotFoundException e1) {
 fnfexception = e1;

} catch (IOException ioe) {
 ioe.printStackTrace();
}
EOFException eof = null;

int counter = 0;
if (fnfexception == null) {
 while (eof == null) {
  try {
   if (words == null) words = new Dict[1];
   else words = Arrays.copyOf(words, counter + 1);
   words[counter] = (Dict) ois.readObject();
   counter++;
  } catch (EOFException end) {
   eof = end;
  } catch (IOException ioe) {
   ioe.printStackTrace();
  } catch (ClassNotFoundException e1) {
   e1.printStackTrace();
  }
 }
}
wordStartCount = counter;
wordCount = counter;
fnfexception = null;

try {
 fos = openFileOutput("words", Context.MODE_PRIVATE);
 oos = new ObjectOutputStream(fos);

} catch (FileNotFoundException e1) {
 fnfexception = e1;

} catch (IOException ioe) {
 ioe.printStackTrace();
}

您使用了错误的方式读取内部文件,请使用以下代码

try {
    FileInputStream fis = context.openFileInput("file_name");
    int content;
    StringBuilder str = new StringBuilder();
    while ((content = fis.read()) != -1)
        str.append((char) content);
    fis.close();
    String savedText = str.toString();
} catch (IOException e) {
    e.printStackTrace();
}

我不认为相对路径在Android中有任何意义。根据文档,它有。谷歌使用以下代码:stringfilename=“hello\u file”;String=“你好,世界!”;FileOutputStream fos=openFileOutput(文件名,Context.MODE\u PRIVATE);fos.write(string.getBytes());fos.close();这是不同的,因为openFileOutput()在私有应用程序目录中打开一个文件,FileOutputStream在文件系统中创建一个文件,所以它应该是绝对的。不要编写这样的代码,包括所有异常引用。取决于try块中代码成功与否的代码应位于同一try块中。仍然获取一个FileNotFoundException确保您创建的文件没有异常,并且您成功写入了该文件,还确保您使用了相同的文件名我已经确认它创建的文件没有异常例外情况。您可能使用了错误的文件名,在我的代码中,我刚刚使用了文件名,将其替换为您自己的文件名是的,我的意思是在我的代码中,我使用了“文件名”作为文件名。在您的文件中,使用“words”,因为它是您用来创建文件的名称