Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/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_File_Assets - Fatal编程技术网

在android应用程序中逐行读取文本文件

在android应用程序中逐行读取文本文件,android,file,assets,Android,File,Assets,我正在尝试使用android studio制作一个android应用程序。我的应用程序需要逐行读取文本文件,并在每行的字符串上执行任务。我正在为此使用bufferedread,但我经常收到一个文件未找到错误。我在/app/src/main/assets/中创建了一个assets文件夹,并将文本文件复制到此assets文件夹中。任何帮助都将不胜感激!请在下面找到我的代码:- package com.example.spandanmadan1.hinglish; import android.co

我正在尝试使用android studio制作一个android应用程序。我的应用程序需要逐行读取文本文件,并在每行的字符串上执行任务。我正在为此使用bufferedread,但我经常收到一个文件未找到错误。我在/app/src/main/assets/中创建了一个assets文件夹,并将文本文件复制到此assets文件夹中。任何帮助都将不胜感激!请在下面找到我的代码:-

package com.example.spandanmadan1.hinglish;

import android.content.res.AssetManager;
import android.provider.UserDictionary;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;


public class MyActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        Button bsubmit = (Button) findViewById(R.id.Hinglishbutton);

        bsubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AssetManager assManager = getApplicationContext().getAssets();
                InputStream fis = assManager.open("HindiSlang.txt");
                BufferedReader bfr = new BufferedReader(new InputStreamReader(fis));

                String line=null;
                while ((line = bfr.readLine()) != null) {
                    UserDictionary.Words.addWord(getApplicationContext(),line,1,"",null);
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_my, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}

请尝试以下操作之一:

try {
    fIn = getApplicationContext().getResources().getAssets()
            .open("HindiSlang.txt", Context.MODE_WORLD_READABLE);

    input = new BufferedReader(new InputStreamReader(fIn));
    String line = "";
    while ((line = input.readLine()) != null) {
        // process the line..
    }
} catch (Exception e) {
    e.getMessage();
}
或编写一个函数:

public String LoadData(String inFile) {
    String tContents = "";

  try {
     InputStream stream = getAssets().open(inFile); // you will get the method getAssets anywhere from current activity.

     int size = stream.available();
     byte[] buffer = new byte[size];
     stream.read(buffer);
     stream.close();
     tContents = new String(buffer);
     } catch (IOException e) {
      // Handle exceptions here
   }

   return tContents;

}

下面是一个Kotlin示例,用于逐行读取输入流(或文件):

InputStream.bufferedReader().useLines { lines ->
  lines.forEach { line ->
    System.out.println(line)
  }
}

非常感谢!我通过将文件存储在/res/raw/目录并使用getResoruces()实现了这一点。您应该使用Context.MODE\u PRIVATE而不是Context.MODE\u WORLD\u READABLE