我已经四处寻找保存一般文本文件的位置以便阅读我的Android项目,但找不到明确的答案。当我按照某人的建议将我的“foo.txt”文件保存到我的res / raw文件夹中时(我必须创建原始文件夹),R.java文件会因这些行而出错:
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>
答案 0 :(得分:5)
首先:1_1
不是有效的变量名称。根据Java文档:
变量的名称可以是任何合法的标识符 - Unicode字母和数字的无限长度序列,以字母开头,美元符号“$”或下划线字符“_”。
秒:该文件应保存在assets
文件夹中。
第三次:要阅读该文件,您可以使用Scanner
。如果您的String
中没有空格,则可以使用:
Scanner in = new Scanner(new File("assets/foo.txt");
in.next(); // Read the next String
in.nextInt(); // Read the next int
in.nextFloat(); // Read the next float
如果String
中有空格,则应使用逗号并告诉Scanner
使用,
作为分隔符:
Scanner in = ...;
in.useDelimiter(",");
...
答案 1 :(得分:2)
将其保存到资源文件夹中。