简体   繁体   English

保存文本文件的位置,以便能够将其读入Android项目

[英]Where to save text file to be able to read it into Android project

I have looked around to find out where to save general text files for reading into my Android project, but couldn't find a definite answer. 我环顾四周,以找出将普通文本文件保存到Android项目中的位置,但找不到确切的答案。 When I save my "foo.txt" file into my res/raw folder as someone suggested (I had to create the raw folder) the R.java file gets error for these lines: 当我按照有人建议的方式将“ foo.txt”文件保存到我的res / raw文件夹中时(我必须创建原始文件夹),R.java文件在以下行中出错:

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

This is because my file contains the string "1_1" on the first line, which I want it to have. 这是因为我的文件的第一行包含字符串“ 1_1”。 Where in the folder structure should I put my file to be able to read it? 我应该将文件放在文件夹结构中的哪个位置才能读取它? The file is not created from Android but manually by me. 该文件不是从Android创建的,而是由我手动创建的。

Could someone also please advise on how to read a file in the following format? 有人还可以建议如何读取以下格式的文件吗? I want to be able to read the Strings and numbers one by one and insert into java variables in my Android project. 我希望能够一一读取字符串和数字并将其插入我的Android项目中的java变量中。 Is it best to separate with commas or spaces? 最好用逗号或空格分隔吗?

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

Updated with more code: 更新了更多代码:

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;
        }

    }

}

And here is the xml file for this test: 这是此测试的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>

First : 1_1 is not a valid variable name. 首先1_1不是有效的变量名称。 As per the Java Documentation: 根据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 "_". 变量的名称可以是任何合法的标识符-无限长度的Unicode字母和数字序列,以字母,美元符号“ $”或下划线字符“ _”开头。

Second : The file should be saved in the assets folder. 第二 :该文件应保存在assets文件夹中。

Third : To read the file, you can use a Scanner . 第三 :要读取文件,可以使用Scanner If your String s don't have spaces in them, this would work: 如果您的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

If you have spaces in your String s, you should use commas and tell the Scanner to use , s as a delimiter: 如果你在有空格String S,你应该使用逗号,并告诉Scanner使用, S作为分隔符:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM