java - 在哪里保存文本文件以便能够将其读入 Android 项目

标签 java android readfile

我环顾四周,想知道在哪里保存一般文本文件以读入我的 Android 项目,但找不到明确的答案。当我按照某人的建议将“foo.txt”文件保存到我的 res/raw 文件夹中时(我必须创建 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>

最佳答案

首先:1_1 不是有效的变量名。根据 Java 文档:

A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_".

第二:文件应该保存在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(",");
...

关于java - 在哪里保存文本文件以便能够将其读入 Android 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9657763/

相关文章:

java - 同步 不同步

android - 从 BroadcastReceiver 调用时 startActivity 不工作

php - 如何使用 javascript 和 php 更改 php 读取文件。新Q如何阻止它影响标题

PHP读取另一个扩展名的MP4文件

java - 使用 Java 将多个图像转换为灰度

java - Gradle 可以解决依赖关系并正常构建/运行,但 IntelliJ 不能

java - JPA 多对多查询

java |上一帧没有消失

android - 如何在锁定屏幕和通知栏中显示带小时的闹钟图标

java - 我的 Retrofit call.enqueue() 方法被完全跳​​过,不知道为什么