我环顾四周以找出将一般文本文件保存到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>
谢谢,这看起来像我之后。尽管如此,我遇到了文件路径的问题。我曾尝试使用“file:///android_asset/foo.txt”,但这不起作用。我通过在线搜索尝试了几种方法,但似乎没有任何方法可行。我错过了什么? – 2012-03-12 17:34:03
仅供参考这些工作都不工作,它们都强制IOException: InputStream inStream = this.getAssets()。open(“foo。(); – 2012-03-12 17:52:43
这些例子也不起作用,file.exsists()返回零: File file =新文件(“file:///android_asset/foo.txt”); File file = new File(“foo.txt”); – 2012-03-12 18:41:06