2013-07-11 3 views
-3

나는 앱을 만들려고하는데, AppInventor의 TinyDB처럼 문자열을 저장해야합니다. 그래서, 내가 찾고있는 건 http://developer.android.com/guide/topics/data/data-storage.html#filesInternal 내부 저장소에 데이터를 저장하는 것이지만, 그것을 읽는 법을 모르겠다.안드로이드 - 내부 데이터 읽기

  1. 전화 openFileInput()와 읽기 그것을 파일의 이름을 전달 :

    내부 저장소에서 파일을 읽으려면 : 그들은 말한다. 이것은 FileInputStream을 반환합니다.

  2. read()로 파일에서 바이트를 읽습니다.

  3. 은 다음 닫기과 스트림()

을 닫습니다하지만 내 코드가 작동하지, 어떻게 모르겠어요. 그래서 내부 저장소에서 읽는 방법을 봤는데 코드는 전혀 작동하지 않았습니다. 내부 저장소에서 텍스트를 읽는 방법을 알려주시겠습니까? :)

이 코드입니다 :

EditText tagbox = (EditText)findViewById(R.id.editText1); 
    String tag = tagbox.toString(); 
    Context context = getApplicationContext(); 


    FileInputStream fis = context.openFileInput(tag); 
    InputStreamReader in = new InputStreamReader(fis); 
    BufferedReader br = new BufferedReader(in); 
    String data = br.readLine(); 

     Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show(); 

좋아, 그래서 나는 해결책을 찾아 냈다. 가장 쉬운 방법은 된 SharedPreferences를 사용하고, AppInventor에서 TinyDB와 같은 데이터를 저장하기 :

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 

Edtior editor = preferences.edit(); 

데이터 저장하기 :

editor.putString("key", "value"); 

읽으려면을 데이터 :

String value = preferences.getString("key"); 
+0

포스트 코드 그래서 우리가 할 수있는 도움을 받으십시오 - 우리는 당신의 코드를 작성하지 않을 것입니다. –

+0

좋습니다, 코드가 있습니다 – StfgMccx

답변

0

확인 그래서 해결책을 찾았습니다. AppInventor에 TinyDB 같은 데이터를 저장하는 가장 쉬운 방법은, 된 SharedPreferences를 사용하고 있습니다 :

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); 

Edtior editor = preferences.edit(); 

데이터를 저장하려면 :

editor.putString("key", "value"); 

데이터를 읽으려면 :

String value = preferences.getString("key"); 
0
 File selectedFile = new File(path + "/" + selectedFromList + ".txt"); 

     FileInputStream fstream = null; 
     ArrayList sal = new ArrayList(); 
     try { 
      fstream = new FileInputStream(selectedFile); 

      BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); 
      String strLine; 
      //Read File Line By Line 

      while ((strLine = br.readLine()) != null) { 
       // Print the content on the console 
       sal.add(strLine); 
       } 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
      //Close the input stream 
      try { 
      in.close(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
+0

문자열로 변환하는 방법? 나는 다음을 사용했다 : String note = sal.toString(); 그리고 그것은 충돌합니다, 그것은 잘못 바꾸 었습니까? – StfgMccx

+0

sal은 배열입니다. 따라서 배열에서 어떤 항목을 사용할지 결정해야합니다. 그래서 sal [0] .toString();을 사용할 수 있습니다. for (int i = 0; i PartTimeLegend

+0

Eclipse에서 sal [0]에 오류가 발생합니다. 오류는 다음과 같습니다. "표현식 유형이 배열 유형이어야하지만 ArrayList로 해석되었습니다." – StfgMccx

관련 문제