0

嗨,如果文件不存在,我正在尝试创建文件。然后,如果文件尚不存在,我想在 11 行中写入 11 个零,然后将其读取到二维数组中。但是应用程序崩溃了,我在 logcat 中收到了消息“java.io.FileNotFoundException: /FILENAME: open failed: ENOENT (No such file or directory)”。如果有人可以帮助我,我将不胜感激。这是我的代码:

public void empt(String fileName) {

    File file = getBaseContext().getFileStreamPath(fileName);
    if(file.exists()){
        return 1;
    }
    else return 2;
 }


 public int[][] readFile2(String fileName) {

     empt(fileName);
     if(empty == 1){

         FileOutputStream outputStream;
         String row = "0 0 0 0 0 0 0 0 0 0 0";
         String newline = "\n";

         try {
           outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
           for(int i = 0; i <11; i++)
             {
                 if(i == 10){
                     outputStream.write(row.getBytes());
                 }
                 else {
                 outputStream.write(row.getBytes());
                 outputStream.write(line.getBytes());
                 }
             }
           outputStream.close();
         } catch (Exception e) {
           e.printStackTrace();
         }

     }


      String line = "";
      int[][] data = new int [11][11];
      try {
       BufferedReader br = new BufferedReader(new FileReader(fileName)); 
      int i = 0;
       while((line = br.readLine()) != null) { // line becomes the whole line ( 11 numbers on a row)
        String[] theline = line.split(" "); // theline becomes an array with 11 numbers


            for(int k = 0; k<11; k++)
            {   
                data[i][k] = (Integer.parseInt(theline[k]));
            }
        i++;
       }
       br.close();
      }
      catch(FileNotFoundException fN) {
       fN.printStackTrace();
      }
      catch(IOException e) {
       System.out.println(e);
      }
      return data;
     }
4

1 回答 1

0

要从您必须使用的内部存储中读取openFileInput

改变

BufferedReader br = new BufferedReader(new FileReader(fileName)); 

FileInputStream fstream = openFileInput(fileName);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
于 2013-10-30T20:48:42.440 回答