簡體   English   中英

保存文本文件的位置,以便能夠將其讀入Android項目

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

我環顧四周,以找出將普通文本文件保存到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>

首先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 S,你應該使用逗號,並告訴Scanner使用, S作為分隔符:

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

將其保存到資產文件夾中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM