Java android开发打开文件txt并返回内容

Java android开发打开文件txt并返回内容,java,android,file-io,Java,Android,File Io,在internet上搜索,但找不到工作代码。 如何获取txt文档的内容并将其返回 假设我在(src/my.proovi.namespace/data.txt)中有一个txt文件 我创建了一个名为refresh_all_data()的方法;我希望在那里收集并返回数据。 在main activity方法中,我只需要将内容获取为(String content=refresh_all_data()),就这样 应该很容易,但就是找不到有效的答案。 非常感谢。在一个函数中实现以下代码,并在任何需要的地方调用

在internet上搜索,但找不到工作代码。 如何获取txt文档的内容并将其返回

假设我在(src/my.proovi.namespace/data.txt)中有一个txt文件 我创建了一个名为refresh_all_data()的方法;我希望在那里收集并返回数据。 在main activity方法中,我只需要将内容获取为(String content=refresh_all_data()),就这样

应该很容易,但就是找不到有效的答案。
非常感谢。

在一个函数中实现以下代码,并在任何需要的地方调用它

try{
      // Open the file that is the first 
      // command line parameter
      FileInputStream fstream = new FileInputStream("textfile.txt");
      // Get the object of DataInputStream
      DataInputStream in = new DataInputStream(fstream);
      BufferedReader br = new BufferedReader(new InputStreamReader(in));
      String strLine;
      //Read File Line By Line
      while ((strLine = br.readLine()) != null)   {
      // Print the content on the console
      System.out.println (strLine);
      }
      //Close the input stream
      in.close();
        }catch (Exception e){//Catch exception if any
      System.err.println("Error: " + e.getMessage());
      }

将文件放在项目的
/assets
文件夹中,然后通过
AssetManager
打开文件,即可获得:

InputStream in = getAssets().open("data.txt");
然后,您可以从文件中读取行,并使用以下命令将其添加到:

好的,我得到了什么

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String test = null;
    try {
        test = refresh_all_data();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    TextView day1Label = new TextView(this);  
    day1Label.setText(test);
    setContentView(day1Label);

}
以及刷新所有数据();方法


谢谢Jave。

Mybe我只是个初学者,这就是为什么我可以让它工作的原因。我删除了所有并发布了此消息,因此无法提供我的treid。在这里,strLine将包含文件的所有内容。因此,在方法中,您可以返回该字符串,请给出否决投票的原因。我错过了什么。所以我可以改进,对不起,我可以让它工作。我已经试过很多次了。我想我只是需要更多的实践和知识来让它工作,但无论如何谢谢你。getAssets无法解决。类型的方法readLine()未定义Reader@RasimMehtijev,对不起,我的输入错误,读卡器应该是
BufferedReader
类型,而不仅仅是
reader
,而且我错过了
getAssets()
后面的括号。未处理的异常类型IOException。显示在getAssets()上打开(“data.txt”);这是我对Eclipse建议使用try/catch进行搜索感到非常困惑的部分。这就是一切的方向,请仔细阅读。对于这样一个简单的例子,如果您用
try{//all code from the method goes here}catch(IOException e){e.printStackTrace();return”“;}
包围方法的内容就足够了,非常感谢您让它工作起来。将添加一个完整的代码。
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String test = null;
    try {
        test = refresh_all_data();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    TextView day1Label = new TextView(this);  
    day1Label.setText(test);
    setContentView(day1Label);

}
private String refresh_all_data() throws IOException
{ 

    InputStream in = getAssets().open("data.txt");
    //The buffered reader has a method readLine() that reads an entire line from the file, InputStreamReader is a reader that reads from a stream.
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    //This is the StringBuilder that we will add the lines to:
    StringBuilder sb = new StringBuilder(512);
    String line;
    //While we can read a line, append it to the StringBuilder:
    while((line = reader.readLine()) != null){
        sb.append(line);
    }
    //Close the stream:
    reader.close();
    //and return the result:
    return sb.toString();
}