2014-06-10 99 views
0

嘿,我有裝載數據的問題,從一個txt文件

private void read() { 


      try { 
       // open the file for reading 
       InputStream instream = openFileInput(File.separator + "bla.txt"); 


      // if file the available for reading 
      if (instream.available() >0) { 
       // prepare the file for reading 
       InputStreamReader inputreader = new InputStreamReader(instream); 
       BufferedReader buffreader = new BufferedReader(inputreader); 

       String line; 

       // read every line of the file into the line-variable, on line at the time 
       while ((line = buffreader.readLine()) != null) { 
        for(int i= 0; i<=1; i++){ 
         if(i == 0){ 
          bla = Integer.parseInt(buffreader.readLine()); 
         } 
         if(i == 1){ 
          bla2 = Integer.parseInt(buffreader.readLine()); 
         } 


        } 

      // close the file again  
      instream.close(); 


      Toast.makeText(getBaseContext(), 
        "Ladevorgang war efolgreich!", 

        Toast.LENGTH_LONG).show(); 

       }} 
      } 

       catch (java.io.FileNotFoundException e) { 
        Toast.makeText(getBaseContext(), 
          "Datei nicht vorhanden", 

          Toast.LENGTH_LONG).show(); 
        } 
     catch (Exception e) { 
      Toast.makeText(getBaseContext(), e.getMessage(), 
        Toast.LENGTH_SHORT).show(); 

請幫助我,我不知道我應該改變:( 我送花兒給人拿到了機器人。 content.res.Resources $ NotFoundException:字符串資源ID#爲0x0 感謝您的幫助Strik3r

private void save() { 

    try { 

     File myFile = new File(Environment.getExternalStorageDirectory() + File.separator + "bla.txt"); 
     myFile.createNewFile(); 
     FileOutputStream fOut = new FileOutputStream(myFile); 
     OutputStreamWriter myOutWriter = 
           new OutputStreamWriter(fOut); 
     myOutWriter.append(string.toString() + "\n"); 
     myOutWriter.append(string2.toString()); 

     myOutWriter.close(); 
     fOut.close(); 
     Toast.makeText(getBaseContext(), 
       "Gespeichert", 
       Toast.LENGTH_SHORT).show(); 
    } catch (Exception e) { 
     Toast.makeText(getBaseContext(), e.getMessage(), 
       Toast.LENGTH_SHORT).show(); 
    } 
} 

這是保存數據

+1

該文件保存在哪裏? –

+0

文件已保存的工作 – Strik3r

+2

後,那麼整個logcat的 –

回答

2
// open the file for reading 

    InputStream instream = openFileInput(File.separator + "bla.txt"); 

似乎不是一個有效的文件。 請改爲

InputStream instream = openFileInput(Environment.getExternalStorageDirectory() +File.separator + "bla.txt"); 
+0

是的,我也試過這個,但我desnt工作:( – Strik3r

相關問題