2012-05-11 36 views
1

我正在写一个Android的应用程序。有两项活动,一项是TextEdit,用来输入'hello message',另一项是按钮来保存内部存储器中的信息。其次是主要活动。你好消息应该在应用程序启动后出现。FileInputStream.read中的IO异常。 Android

次活动:

String s = ((EditText) findViewById(R.id.message_act_editText_hello)).getText().toString(); 
    FileOutputStream fos = openFileOutput(Lab2AndroidActivity.FILENAME, Context.MODE_PRIVATE); 
    fos.write(s.getBytes()); 
    fos.close(); 

第一(主)活动:

static String FILENAME = "message_file.zip"; 
    FileOutputStream fos; 
    try { 
     //piece of code to guarantee that file exists 
     fos = openFileOutput(Lab2AndroidActivity.FILENAME, Context.MODE_APPEND); 
     fos.close(); 
    } catch (FileNotFoundException e1) { 
     e1.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 


    try { 
     fis = openFileInput(FILENAME); 
     messageString = new StringBuffer(""); 
     while ((length = fis.read(buffer)) != -1) { 
      String temp = new String(buffer, 0,length); 
      messageString.append(temp); 
      fis.close(); 
     } 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    Toast t = Toast.makeText(this, messageString, 3000); 
    t.show(); 

我得到IO异常在logcat中的行:

while ((length = fis.read(buffer)) != -1) 

,但应用程序似乎工作正确(在应用程序启动后出现定义的消息)。我试图找到解释,我发现了几个主题,但都是根据大文件或资产文件或压缩文件。 我想我的名字文件中像

static String FILENAME = "message_file.zip", 
static String FILENAME = "message_file.txt", 

尝试不同的扩展,但总是我得到同样的IO异常。

感谢您的建议。

回答

0

我找到原因补充。问题是在片段:

while ((length = fis.read(buffer)) != -1) { 
     String temp = new String(buffer, 0,length); 
     messageString.append(temp); 
     fis.close(); 
    } 

有什么收获?

fis.close(); 

应该在之后。我没有注意到,昨天...

0
当然

你会得到一个IO异常文件不退出,并要求你打开它 你忘了的代码

File myFile = new File("/sdcard/mysdfile.txt"); 

这peice的在你的第一个活动,你可以使用此代码

public class MainActivity extends Activity { 
    /** Called when the activity is first created. */ 
    EditText txtData; 
    Button btnWriteSDFile; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    // bind GUI elements with local controls 
    txtData = (EditText) findViewById(R.id.txtData); 
    txtData.setHint("Enter some lines of data here..."); 

    btnWriteSDFile = (Button) findViewById(R.id.btnWriteSDFile); 
    btnWriteSDFile.setOnClickListener(new OnClickListener() { 

    public void onClick(View v) { 
     // write on SD card file data in the text box 
     try { 
      File myFile = new File("/sdcard/mysdfile.txt"); 
      myFile.createNewFile(); 
      FileOutputStream fOut = new FileOutputStream(myFile); 
      OutputStreamWriter myOutWriter = 
            new OutputStreamWriter(fOut); 
      myOutWriter.append(txtData.getText()); 
      myOutWriter.close(); 
      fOut.close(); 
      Toast.makeText(getBaseContext(), 
        "Done writing SD 'mysdfile.txt'", 
        Toast.LENGTH_SHORT).show(); 
      Intent i = new Intent(getApplicationContext(),SecondActivity.class); 
      startActivity(i); 
     } catch (Exception e) { 
      Toast.makeText(getBaseContext(), e.getMessage(), 
        Toast.LENGTH_SHORT).show(); 
     } 
    }// onClick 
    }); 
} 
} 

在第二个,你可以使用这个:

public class SecondActivity extends Activity { 

    private TextView txtData2; 
    @Override 
     public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main2); 
     txtData2 = (TextView) findViewById(R.id.textView2); 
     try { 
      File myFile = new File("/sdcard/mysdfile.txt"); 
      FileInputStream fIn = new FileInputStream(myFile); 
      BufferedReader myReader = new BufferedReader(
        new InputStreamReader(fIn)); 
      String aDataRow = ""; 
      String aBuffer = ""; 
      while ((aDataRow = myReader.readLine()) != null) { 
       aBuffer += aDataRow + "\n"; 
      } 
      txtData2.setText(aBuffer); 
      myReader.close(); 
      Toast.makeText(getBaseContext(), 
        "Done reading SD 'mysdfile.txt'", 
        Toast.LENGTH_SHORT).show(); 
     } catch (Exception e) { 
      Toast.makeText(getBaseContext(), e.getMessage(), 
        Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

的冷杉牛逼latout使用包含一个EditText和一个按钮

第二个是LinearLayout中,只有一个TextView一个的LinearLayout

尝试,如果你发现问题,让我知道它工作正常!

啊我忘了,你必须在你的清单

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

我想使用内部存储,而不是外部。我的文件存在,openFileOutput(String name,int mode)创建文件,如果不存在。我在'while((length = fis.read(buffer))!= -1)'中得到IO异常,而不是FileNotFoundException。 –