Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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
android文本文件导入我在哪里保存文本文件什么文件夹?_Android_Save_Directory_Text Files - Fatal编程技术网

android文本文件导入我在哪里保存文本文件什么文件夹?

android文本文件导入我在哪里保存文本文件什么文件夹?,android,save,directory,text-files,Android,Save,Directory,Text Files,这是一个相当尴尬的问题,但我有一个文本文件,java会读取其中的所有单词并将其添加到数组中,我不知道将文本文件放在哪里,比如哪个文件夹,这样公司就可以得到它?谁能告诉我。我的代码在一个普通的java应用程序中工作,所以它应该在android上工作 您可以将文件放入资产文件夹并使用 InputStream stream = getAssets().open(filename); 要获取输入流您可以将文件放入资产文件夹并使用 InputStream stream = getAssets().op

这是一个相当尴尬的问题,但我有一个文本文件,java会读取其中的所有单词并将其添加到数组中,我不知道将文本文件放在哪里,比如哪个文件夹,这样公司就可以得到它?谁能告诉我。我的代码在一个普通的java应用程序中工作,所以它应该在android上工作

您可以将文件放入资产文件夹并使用

InputStream stream = getAssets().open(filename); 

要获取输入流

您可以将文件放入资产文件夹并使用

InputStream stream = getAssets().open(filename); 
要获取输入流,可以使用

<your-context>.getAssets();
然后可以使用open()方法打开输入流

InputStream对象是IO包中的标准Java对象。您可以使用所需的对象装饰器(Reader、BufferedReader等)装饰此流

如果您希望将该文件从APK(未充气)移动到手机,您可以使用输出流从输入流复制该文件的字节。请注意,您必须在写入目录中拥有权限(如果您的手机是根目录,并且您已经创建了一个shell接口以通过JNI运行本机shell命令,则可以执行此操作)

更新

try {
    InputStream inputStream = this.getAssets().open("test.txt");
    BufferedReader buffer = new BufferedReader(new Reader(inputStream));

    String line;
    while((line = buffer.readLine()) != null) {
        tots.add(line);
    }
}
catch(IOException e) {
    e.printStackTrace();
}
还没有测试过,但我认为这是您想要的。

您可以使用

<your-context>.getAssets();
然后可以使用open()方法打开输入流

InputStream对象是IO包中的标准Java对象。您可以使用所需的对象装饰器(Reader、BufferedReader等)装饰此流

如果您希望将该文件从APK(未充气)移动到手机,您可以使用输出流从输入流复制该文件的字节。请注意,您必须在写入目录中拥有权限(如果您的手机是根目录,并且您已经创建了一个shell接口以通过JNI运行本机shell命令,则可以执行此操作)

更新

try {
    InputStream inputStream = this.getAssets().open("test.txt");
    BufferedReader buffer = new BufferedReader(new Reader(inputStream));

    String line;
    while((line = buffer.readLine()) != null) {
        tots.add(line);
    }
}
catch(IOException e) {
    e.printStackTrace();
}

还没有测试过,但我想这就是你想要的。

我在res文件夹中创建了新的raw文件夹,并将chapter0.txt放在这里

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.induction);

    wordss = new Vector<String>();

    TextViewEx helloTxt = (TextViewEx) findViewById(R.id.test);
    helloTxt.setText(readTxt());
}

private String readTxt() {

    InputStream inputStream = getResources().openRawResource(R.raw.chapter0);
    // getResources().openRawResource(R.raw.internals);
    System.out.println(inputStream);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return byteArrayOutputStream.toString();
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、归纳);
wordss=新向量();
TextViewEx helloTxt=(TextViewEx)findViewById(R.id.test);
setText(readTxt());
}
私有字符串readTxt(){
InputStream InputStream=getResources().openRawResource(R.raw.chapter0);
//getResources().openRawResource(R.raw.internals);
System.out.println(inputStream);
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream();
int i;
试一试{
i=inputStream.read();
而(i!=-1){
byteArrayOutputStream.write(i);
i=inputStream.read();
}
inputStream.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回byteArrayOutputStream.toString();
}

我在res文件夹中创建了新的raw文件夹,并将chapter0.txt放在这里

public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.induction);

    wordss = new Vector<String>();

    TextViewEx helloTxt = (TextViewEx) findViewById(R.id.test);
    helloTxt.setText(readTxt());
}

private String readTxt() {

    InputStream inputStream = getResources().openRawResource(R.raw.chapter0);
    // getResources().openRawResource(R.raw.internals);
    System.out.println(inputStream);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int i;
    try {
        i = inputStream.read();
        while (i != -1) {
            byteArrayOutputStream.write(i);
            i = inputStream.read();
        }
        inputStream.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return byteArrayOutputStream.toString();
}
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局、归纳);
wordss=新向量();
TextViewEx helloTxt=(TextViewEx)findViewById(R.id.test);
setText(readTxt());
}
私有字符串readTxt(){
InputStream InputStream=getResources().openRawResource(R.raw.chapter0);
//getResources().openRawResource(R.raw.internals);
System.out.println(inputStream);
ByteArrayOutputStream ByteArrayOutputStream=新建ByteArrayOutputStream();
int i;
试一试{
i=inputStream.read();
而(i!=-1){
byteArrayOutputStream.write(i);
i=inputStream.read();
}
inputStream.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回byteArrayOutputStream.toString();
}

你能解释一下第一件事吗,比如文件名是test.txt你需要一个上下文对象来提取资产,即getAssets()方法是一个上下文方法。因此,如果您的应用程序位于从ContextWrapper继承的活动或任何内容中,您只需说“this.getAssets()”。否则,您将需要向尝试获取资产管理器的函数/方法传递上下文引用。try{FileInputStream textfl=(FileInputStream)getAssets().open(“test.txt”);DataInputStream is=new DataInputStream(textfl);BufferedReader r=new BufferedReader(new InputStreamReader(is));String strLine;while((strLine=r.readLine())!=null){tots.add(strLine);//tots是数组列表}}catch(IOException e){//TODO自动生成的catch块e.printStackTrace();}我现在有了它,但它不起作用。。。。你知道一个快速解决方法吗?尝试使用InputStream而不是FileInputStream。删除DataInputStream,因为您在其装饰中没有使用任何方法,而是使用Reader。您是否可以解释第一件事,例如<>文件名是test.txt您需要一个上下文对象来提取资产,即getAssets()方法是一个上下文方法。因此,如果您的应用程序位于从ContextWrapper继承的活动或任何内容中,您只需说“this.getAssets()”。否则,您将需要向尝试获取资产管理器的函数/方法传递上下文引用。try{FileInputStream textfl=(FileInputStream)getAssets().open(“test.txt”);DataInputStream is=new DataInputStream(textfl);BufferedReader r=new BufferedReader(new InputStreamReader(is));String strLine;while((strLine=r.readLine())!=null){tots.add(strLine);//tots是数组列表}}catch(IOException e){//TODO自动生成的catch块e.printStackTrace();}我现在有了它,但它不起作用。。。。你知道一个快速解决方法吗?试着使用InputStrea