2012-03-11 2 views
1

나는 안드로이드 프로젝트를 읽기 위해 일반 텍스트 파일을 어디에 저장할 것인지 찾아 보았지만 확실한 답을 찾을 수 없었다.안드로이드 프로젝트에 읽을 수 있도록 텍스트 파일을 저장할 위치

public static final class raw { 
    public static final int 1_1=0x7f050000; 
} 

이 내 때문이다 : 누군가가 제안 내 입술/원시 폴더에 내 "에 foo.txt"파일을 저장하면 R.java 파일이이 라인에 대한 오류를 가져옵니다 (I 원시 폴더를 만들 수 있었다) 파일에는 첫 번째 줄에 "1_1"이라는 문자열이 포함되어 있습니다. 폴더 구조에서 파일을 읽을 수있게해야합니까? 파일은 Android에서 생성되지는 않지만 수동으로 생성됩니다.

누군가 다음 형식으로 파일을 읽는 방법에 대해 조언 해 줄 수 있습니까? 문자열과 숫자를 하나씩 읽고 Android 프로젝트의 자바 변수에 삽입하고 싶습니다. 쉼표 나 공백으로 구분하는 것이 가장 좋습니다.

package com.my.package; 

import java.io.File; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.ImageButton; 
import android.widget.TextView; 
import android.widget.Toast; 

//public class GameActivity extends FragmentActivity implements OnClickListener { 
public class GameActivity extends Activity implements OnClickListener{ 

private ImageButton leftPauseButton; 
private ImageButton rightPauseButton; 

private ImageButton leftButton1; 
private ImageButton leftButton2; 
private ImageButton leftButton3; 

private ImageButton rightButton1; 
private ImageButton rightButton2; 
private ImageButton rightButton3; 


    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.testlayout); 
     TextView txtView = (TextView) (findViewById(R.id.testID_canBeRemoved)); 

      //Did not work 
     //int resourceId = this.getResources().getIdentifier("com.my.package:raw/foo.txt", null, null); 
     //File f = new File("com.my.package:raw/foo.txt"); 

      //Does not work - file.exists() returns a zero value 
     File file = new File("assets/foo.txt"); 

     if (file.exists()){ 
      txtView.setText("Exists"); 
     } 
     else{ 
      txtView.setText("Does not exist"); 
     } 


//   InitiateUIComponents(); 

    } 

     //This is for using another xml layout 
    private void InitiateUIComponents(){ 

     leftPauseButton = (ImageButton) (findViewById(R.id.leftPauseButtonID)); 
     rightPauseButton = (ImageButton) (findViewById(R.id.rightPauseButtonID)); 

     leftButton1 = (ImageButton) (findViewById(R.id.leftMenuButton1ID)); 
     leftButton2 = (ImageButton) (findViewById(R.id.leftMenuButton2ID)); 
     leftButton3 = (ImageButton) (findViewById(R.id.leftMenuButton3ID)); 

     rightButton1 = (ImageButton) (findViewById(R.id.rightMenuButton1ID)); 
     rightButton2 = (ImageButton) (findViewById(R.id.rightMenuButton2ID)); 
     rightButton3 = (ImageButton) (findViewById(R.id.rightMenuButton3ID)); 

     leftPauseButton.setOnClickListener(this); 
     rightPauseButton.setOnClickListener(this); 

     leftButton1.setOnClickListener(this); 
     leftButton2.setOnClickListener(this); 
     leftButton3.setOnClickListener(this); 

     rightButton1.setOnClickListener(this); 
     rightButton2.setOnClickListener(this); 
     rightButton3.setOnClickListener(this); 

    } 

      //This is for using another xml layout 
    @Override 
    public void onClick(View v) { 

     switch (v.getId()) { 
     case R.id.leftPauseButtonID: 
      Toast.makeText(this, "Left pause button clicked!", Toast.LENGTH_SHORT).show(); 
      break; 
     case R.id.rightPauseButtonID: 
      Toast.makeText(this, "Right pause button clicked!", Toast.LENGTH_SHORT).show(); 
      break; 
     case R.id.leftMenuButton1ID: 
      Toast.makeText(this, "Left menu button 1 clicked!", Toast.LENGTH_SHORT).show(); 
      break; 
     case R.id.leftMenuButton2ID: 
      Toast.makeText(this, "Left menu button 2 clicked!", Toast.LENGTH_SHORT).show(); 
      break; 
     case R.id.leftMenuButton3ID: 
      Toast.makeText(this, "Left menu button 3 clicked!", Toast.LENGTH_SHORT).show(); 
      break; 
     case R.id.rightMenuButton1ID: 
      Toast.makeText(this, "Right menu button 1 clicked!", Toast.LENGTH_SHORT).show(); 
      break; 
     case R.id.rightMenuButton2ID: 
      Toast.makeText(this, "Right menu button 2 clicked!", Toast.LENGTH_SHORT).show(); 
      break; 
     case R.id.rightMenuButton3ID: 
      Toast.makeText(this, "Right menu button 3 clicked!", Toast.LENGTH_SHORT).show(); 
      break; 


     default: 
      break; 
     } 

    } 

} 

을 그리고 여기에 XML을이 테스트 파일 :

1_1 
String 
Int 
Int String String Int Int Float Float Int Int 
Int String String Int Int Float Float Int Int 
Int String String Int Int Float Float Int Int 
Int String String Int Int Float Float Int Int 
Int String String Int Int Float Float Int Int 
Int String String Int Int Float Float Int Int 

는 더 많은 코드와 업데이트 된 자산의 폴더에 저장

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/testID_canBeRemoved" 
    android:text="Blabla" 
> 

</TextView> 
</LinearLayout> 

답변

관련 문제