2012-05-11 3 views
1

나는 안드로이드의 애플 리케이션을 writting 해요. 두 가지 활동 중 하나에는 'hello message'를 입력하는 TextEdit과 내부 저장소에 메시지를 저장하는 버튼이 있습니다. 두 번째 주요 활동입니다. 앱 시작 후 Hello 메시지가 표시됩니다.FileInputStream.read의 IO 예외입니다. 안드로이드

두 번째 활동

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 예외를 받고 있어요 :

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(); 
    } 

무엇이 catch입니까?

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(); 
     } 
    } 

} 

전나무를 글고 및 단지 텍스트 뷰와 버튼

두 번째있는 LinearLayout을 포함의 LinearLayout을 사용 latout t은

는 문제가 알려주 발견하면 잘 작동 해보십시오!

아 나는 당신이 당신의

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

내부가 아닌 외부 저장소를 사용하고 싶습니다. 내 파일이 존재하지 않으면 openFileOutput (String name, int mode) 파일이 생성됩니다. 내가 IO 예외를 받고있어, FileNotFoundException이 아니라 'while ((length = fis.read (buffer))! = -1)'. –