【问题标题】:Where to save text file to be able to read it into Android project在哪里保存文本文件以便能够将其读入 Android 项目
【发布时间】:2012-03-11 18:38:21
【问题描述】:

我四处寻找可以保存通用文本文件以读入我的 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>

【问题讨论】:

    标签: java android readfile


    【解决方案1】:

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

    变量的名称可以是任何合法标识符 — 长度不限的 Unicode 字母和数字序列,以字母、美元符号“$”或下划线字符“_”开头。

    第二:文件应保存在assets文件夹中。

    第三:要读取文件,您可以使用Scanner。如果您的 Strings 中没有空格,则可以这样做:

    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
    

    如果您的Strings 中有空格,您应该使用逗号并告诉Scanner 使用,s 作为分隔符:

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

    【讨论】:

    • 谢谢,这看起来像我所追求的。我在文件的路径上遇到了问题。我曾尝试使用“file:///android_asset/foo.txt”,但这不起作用。通过网上搜索尝试了几种方法,但似乎没有一种方法有效。我错过了什么?
    • 仅供参考,这些都不起作用,它们都强制一个 IOException: InputStream inStream = this.getAssets().open("foo.txt"); InputStream inStream = this.getAssets().open("file:///android_asset/foo.txt");
    • 这些例子也不起作用,file.exsists() 返回零: File file = new File("file:///android_asset/foo.txt");文件 file = new File("foo.txt");
    • 路径应该是assets/foo.txt
    • 这很奇怪,该路径也不起作用。文件 file = new File("assets/foo.txt");返回零值。试图将我的目录图像上传到 imgur 供您查看,但它没有以某种方式工作。我已将文件直接放在 Android 项目中自动创建的“assets”文件夹中。该文件也在 Ubuntu 的文件系统中的“assets”文件夹中。我尝试将文件的内容更改为一个单词,但没有帮助。这不应该是 API 的问题吗?
    【解决方案2】:

    将其保存到您的资产文件夹中。

    【讨论】:

    • 谢谢,似乎工作正常。然后我可以按照示例here 直接将文件名更改为“foo.txt”吗?当我读完一行时,我可以使用哪些方法来读取该行的一个字符串、浮点数或整数?
    猜你喜欢
    • 1970-01-01
    • 2011-11-23
    • 2021-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2019-08-27
    • 1970-01-01
    相关资源
    最近更新 更多