Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在何处保存文本文件以便能够将其读入Android项目_Java_Android_Readfile - Fatal编程技术网

Java 在何处保存文本文件以便能够将其读入Android项目

Java 在何处保存文本文件以便能够将其读入Android项目,java,android,readfile,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项目中,但找不到明确的答案。当我将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
更新了更多代码:

下面是此测试的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\u 1不是有效的变量名。根据Java文档:

变量的名称可以是任何合法标识符-Unicode字母和数字的无限长序列,以字母、美元符号$或下划线字符33;

第二:文件应保存在资产文件夹中

第三:要读取文件,可以使用。如果字符串中没有空格,则可以:

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
如果字符串中有空格,则应使用逗号并告诉扫描程序使用,s作为分隔符:

Scanner in = ...;
in.useDelimiter(",");
...
第一:1_1不是有效的变量名。根据Java文档:

变量的名称可以是任何合法标识符-Unicode字母和数字的无限长序列,以字母、美元符号$或下划线字符33;

第二:文件应保存在资产文件夹中

第三:要读取文件,可以使用。如果字符串中没有空格,则可以:

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
如果字符串中有空格,则应使用逗号并告诉扫描程序使用,s作为分隔符:

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

谢谢,看起来不错。然后我可以按照示例直接将文件名更改为foo.txt吗?当我读了一行时,我可以用什么方法来读一行和一行的字符串,float还是int?谢谢,看起来效果不错。然后我可以按照示例直接将文件名更改为foo.txt吗?当我读了一行时,我可以用什么方法来读一行和一行的字符串,float还是int?谢谢,这看起来像是我想要的。但是,我在文件路径方面遇到了问题。我试过使用file:///android_asset/foo.txt 但这是行不通的。我在网上搜索时尝试了几种方法,但似乎没有一种方法有效。我遗漏了什么?仅供参考,这两种方法都不起作用,它们都强制IOException:InputStream inStream=this.getAssets.openfoo.txt;InputStream inStream=this.getAssets。openfile:///android_asset/foo.txt;这些示例也不起作用,file.exsists返回零:file file=new Filefile:///android_asset/foo.txt; File File=new Filefoo.txt;路径应该是assets/foo.txt这很奇怪,该路径也不起作用。File File=new Fileassets/foo.txt;返回一个零值。试图将我的目录图像上传到imgur供您查看,但不知怎的,它没有起作用。我已经把文件直接放在一个Android项目中自动创建的assets文件夹中。该文件也位于Ubuntu文件系统的资产文件夹中。我试着把文件的内容改成一个单词,但没用。这应该不是API的问题吗?谢谢,这看起来像是我想要的。但是,我在文件路径方面遇到了问题。我试过使用file:///android_asset/foo.txt 但这是行不通的。我在网上搜索时尝试了几种方法,但似乎没有一种方法有效。我遗漏了什么?仅供参考,这两种方法都不起作用,它们都强制IOException:InputStream inStream=this.getAssets.openfoo.txt;InputStream inStream=this.getAssets。openfile:///android_asset/foo.txt;这些示例也不起作用,file.exsists返回零:file file=new Filefile:///android_asset/foo.txt; File File=new Filefoo.txt;路径应该是assets/foo.txt这很奇怪,该路径也不起作用。File File=new Fileassets/foo.txt;返回一个零值。试图将我的目录图像上传到imgur供您查看,但不知怎的,它没有起作用。我已经把文件直接放在一个Android项目中自动创建的assets文件夹中。该文件也位于Ubuntu文件系统的资产文件夹中。我试着把文件的内容改成一个单词,但没用。这应该不是API的问题吧?