2012-03-11 102 views
1

我環顧四周以找出將一般文本文件保存到Android項目中的位置,但無法找到明確的答案。當我救我「foo.txt的」文件到我的資源/原材料的文件夾中有人建議(我不得不創建原始文件夾)的文件R.java這些行得到錯誤:將文本文件保存到Android項目中的位置

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

這是因爲我的文件在第一行包含字符串「1_1」,我希望它具有。在文件夾結構中,我應該如何讓我的文件能夠讀取它?該文件不是從Android創建的,而是由我手動創建的。

有人也請告知如何閱讀以下格式的文件?我希望能夠逐個讀取字符串和數字,並在我的Android項目中插入到java變量中。用逗號或空格分隔最好嗎?

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 

更新了更多的代碼:

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文件:

<?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> 

回答

5

第一個1_1不是有效的變量名稱。按照Java文檔:

變量的名稱可以是任何合法的標識符 - Unicode編碼的字母和數字的無限長的序列,以字母開頭,美元符號「$」,或下劃線「_」。

:文件必須被保存在文件夾assets

第三個:要讀取文件,您可以使用Scanner。如果您String■不要在他們的空間,這會工作:

如果您在String■找空間,你應該使用逗號,並告訴Scanner使用, S作爲分隔符:

Scanner in = ...; 
in.useDelimiter(","); 
... 
+0

謝謝,這看起來像我之後。儘管如此,我遇到了文件路徑的問題。我曾嘗試使用「file:///android_asset/foo.txt」,但這不起作用。我通過在線搜索嘗試了幾種方法,但似乎沒有任何方法可行。我錯過了什麼? – 2012-03-12 17:34:03

+0

僅供參考這些工作都不工作,它們都強制IOException: InputStream inStream = this.getAssets()。open(「foo。(); – 2012-03-12 17:52:43

+0

這些例子也不起作用,file.exsists()返回零: File file =新文件(「file:///android_asset/foo.txt」); File file = new File(「foo.txt」); – 2012-03-12 18:41:06

2

保存到你的資產的文件夾。

+0

謝謝,似乎工作正常。然後,我可以按照[這裏]示例(http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml)直接更改文件名爲「foo.txt」嗎? 而當我讀了一行,我可以使用哪些方法來讀取一個和一個字符串,該行的浮點數或整數? – 2012-03-11 18:50:10

+0

我的答案解釋瞭如何閱讀文件... – Jon 2012-03-11 19:11:50

相關問題