在哪里保存的文本文件,以便能够读取到Android项目 [英] Where to save text file to be able to read it into Android project

查看:179
本文介绍了在哪里保存的文本文件,以便能够读取到Android项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,找出保存一般的文本文件读入我的Andr​​oid项目,但找不到一个明确的答复。当我救我foo.txt的文件到我的RES /原始文件夹中有人建议(我不得不创建原始文件夹)的文件R.java这些行获取错误:

 公共静态final类原料{
    公共静态最终诠释1_1 = 0x7f050000;
}

这是因为我的文件包含在第一行,我希望它有字符串1_1。我应该在文件夹结构,其中把我的文件,能读吗?从Android的由我不会创建该文件,但手动。

可能有人还请告知如何阅读下列格式的文件?我希望能够通过一个阅读的字符串和数字之一,并插入Java变量在我的Andr​​oid项目。它是最好用逗号或空格分开?

  1_1

INT
诠释字符串字符串诠释诠释浮浮诠释诠释
诠释字符串字符串诠释诠释浮浮诠释诠释
诠释字符串字符串诠释诠释浮浮诠释诠释
诠释字符串字符串诠释诠释浮浮诠释诠释
诠释字符串字符串诠释诠释浮浮诠释诠释
诠释字符串字符串诠释诠释浮浮诠释诠释

更新更code:

 包com.my.package;进口的java.io.File;进口android.app.Activity;
进口android.os.Bundle;
进口android.util.Log;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.ImageButton;
进口android.widget.TextView;
进口android.widget.Toast;//公共类GameActivity扩展FragmentActivity实现OnClickListener {
公共类GameActivity扩展活动实现OnClickListener {私人的ImageButton leftPauseButton;
私人的ImageButton rightPauseButton;私人的ImageButton leftButton1;
私人的ImageButton leftButton2;
私人的ImageButton leftButton3;私人的ImageButton rightButton1;
私人的ImageButton rightButton2;
私人的ImageButton rightButton3;
  公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.testlayout);
        TextView的txtView =(TextView中)(findViewById(R.id.testID_canBeRemoved));            //不工作
        // INT RESOURCEID = this.getResources()则getIdentifier(com.my.package:生/ foo.txt的,NULL,NULL);
        //文件f =新的文件(com.my.package:生/ foo.txt的);            //并不奏效 - file.exists()返回零值
        档案文件=新的文件(资产/ foo.txt的);        如果(file.exists()){
            txtView.setText(存在);
        }
        其他{
            txtView.setText(不存在);
        }
// InitiateUIComponents();    }        //这是使用另一种XML布局
    私人无效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(本);
        rightPauseButton.setOnClickListener(本);        leftButton1.setOnClickListener(本);
        leftButton2.setOnClickListener(本);
        leftButton3.setOnClickListener(本);        rightButton1.setOnClickListener(本);
        rightButton2.setOnClickListener(本);
        rightButton3.setOnClickListener(本);    }            //这是使用另一种XML布局
    @覆盖
    公共无效的onClick(视图v){        开关(v.getId()){
        案例R.id.leftPauseButtonID:
            Toast.makeText(这一点,左暂停按钮按下!,Toast.LENGTH_SHORT).show();
            打破;
        案例R.id.rightPauseButtonID:
            Toast.makeText(这一点,右暂停按钮按下!,Toast.LENGTH_SHORT).show();
            打破;
        案例R.id.leftMenuButton1ID:
            Toast.makeText(这一点,左菜单按钮,点击1!,Toast.LENGTH_SHORT).show();
            打破;
        案例R.id.leftMenuButton2ID:
            Toast.makeText(这一点,左边的菜单键点击2!,Toast.LENGTH_SHORT).show();
            打破;
        案例R.id.leftMenuButton3ID:
            Toast.makeText(这一点,左边的菜单键3中单击!,Toast.LENGTH_SHORT).show();
            打破;
        案例R.id.rightMenuButton1ID:
            Toast.makeText(这一点,右菜单按钮,点击1!,Toast.LENGTH_SHORT).show();
            打破;
        案例R.id.rightMenuButton2ID:
            Toast.makeText(这一点,右菜单按钮2点击!,Toast.LENGTH_SHORT).show();
            打破;
        案例R.id.rightMenuButton3ID:
            Toast.makeText(这一点,右菜单按钮,点击3!Toast.LENGTH_SHORT).show();
            打破;
        默认:
            打破;
        }    }}

这是本次测试的xml文件:

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
><的TextView
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:ID =@ + ID / testID_canBeRemoved
    机器人:文字=布拉布拉
>< / TextView的>
< / LinearLayout中>


解决方案

第一 1_1 不是一个有效的变量名。按照Java文档:


  

    

一个变量名可以是任何合法的标识符 - 的统一code字母和数字无限长的序列,以字母,美元符号$,或下划线_开头。


  

:文件应该被保存在资产文件夹

第三:读取文件,你可以使用的 扫描 。如果你的字符串■不要在他们的空间,这将工作:

 扫描仪在=新的扫描仪(新文件(资产/ foo.txt的);in.next(); //读取下一个字符串
in.nextInt(); //读取下一个INT
in.nextFloat(); //读取下一个浮动

如果你在你的字符串取值空间,你应该使用逗号,并告诉扫描使用 S作为分隔符:

 扫描仪在= ...;
in.useDelimiter(,);
...

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. 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:

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. 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.

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. 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 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. As per the Java Documentation:

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 "_".

Second: The file should be saved in the assets folder.

Third: To read the file, you can use a Scanner. If your Strings don't have spaces in them, this would work:

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 Strings, you should use commas and tell the Scanner to use ,s as a delimiter:

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

这篇关于在哪里保存的文本文件,以便能够读取到Android项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆