2013-10-02 3 views
1

assets 폴더에서 숫자의 텍스트 파일을 읽은 다음 해당 숫자를 배열에 복사하고 각 숫자의 값을 두 배로 늘리고 새로운 배수로 된 세트를 쓰는 Android 애플리케이션을 만들려고합니다. 앱의 외부 파일 디렉토리에있는 숫자. 두 파일 이름을 입력하면 내 응용 프로그램이 충돌합니다. 내 실수의 상당 부분이 외부 파일에 쓰려고하는 방식이라고 생각합니다. 나는 많은 다른 포스트를 읽고 정확히 어떻게 정확하게 쓰는지 알아낼 수 없다. 또한Android 읽기 및 쓰기 앱

public void readArray() { 

    EditText editText1; 
    EditText editText2; 
    TextView tv; 

    tv = (TextView) findViewById(R.id.text_answer); 
    editText1 = (EditText) findViewById(R.id.input_file); 
    editText2 = (EditText) findViewById(R.id.output_file); 

    int numGrades = 0; 
    int[] gradeList = new int[20]; 

    String fileLoc = (String) (editText1.getText().toString()); 

    try { 
     File inFile = new File("file:///android_asset/" + fileLoc); 
     Scanner fsc = new Scanner(inFile); 

     while (fsc.hasNext()) { 
      gradeList[numGrades] = fsc.nextInt(); 
      numGrades++; 
     } 
    } catch (FileNotFoundException e) { 
     tv.append("error: file not found"); 
    } 

    for (int i = 0; i < gradeList.length; i++) { 
     gradeList[i] = gradeList[i] * 2; 
    } 

    String fileLoc2 = (String) (editText2.getText().toString()); 

    FileWriter fw; 

    //new code 
    File root = Environment.getExternalStorageDirectory(); 
    File file = new File(root, fileLoc2); 

    try { 
     if (root.canWrite()) { 
      fw = new FileWriter(file); 

      //PrintWriter pw = new PrintWriter(fw); 
      BufferedWriter out = new BufferedWriter(fw); 

      for (int i = 0; i < gradeList.length; i++) { 
       out.write(gradeList[i]); 
      } 
      out.close(); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

사전

+1

로그를 공유 할 수 있습니까? 앱이 충돌 할 때 콘솔에 표시됩니다. 이 줄에는 문제가있는 것 같습니다 :'File inFile = new File ("file : /// android_asset /"+ fileLoc);' – Peshal

+0

제쳐두고, 여러분은 질문에 언급하지 않지만 [WRITE_EXTERNAL_STORAGE ] (http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE) 사용 권한을 확인하십시오. – Karl

답변

0

에 nooblike 질문 죄송합니다 당신은 자산에서 파일을 열 AssetManager 클래스를 사용하여 시도해야합니다 :

여기에 대한 자세한 내용보기 : http://developer.android.com/reference/android/content/res/AssetManager.html

당신은 읽을 수 파일 형식 :

AssetManager am = context.getAssets();  
InputStream is = am.open("text");  
BufferedReader in = new BufferedReader(new InputStreamReader(is));  
String inputLine;  
while ((inputLine = in.readLine()) != null)   
    System.out.println(inputLine); 
in.close();