简体   繁体   English

应用程序从文件中读取文本并在文本字段上打印

[英]App to read text from file and print on text field

I think my code is reading the file right, but when I run the app, the output is "Hello World". 我认为我的代码正在正确读取文件,但是当我运行该应用程序时,输出为“ Hello World”。 Which tells me that the text field isn't getting overwritten. 这告诉我文本字段没有被覆盖。 I think the problem lies in the while loop. 我认为问题出在while循环中。 Here is my java code: 这是我的Java代码:

package com.example.fileio.app1_fileio;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Scanner;
import java.io.FileReader;


public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    BufferedReader br = null;

    try {

        String sCurrentLine;

        //get a reference to the textview in the view, this is done by referring to the textview's id: outputText
        TextView t = (TextView) findViewById(R.id.textField);


        String var1;
        String var2;

        br = new BufferedReader(new FileReader("C:\\Users\\android.txt"));

        while ((sCurrentLine = br.readLine()) != null) {
            var1 = sCurrentLine;
            var2 = sCurrentLine;
            //overwrite the text in the textview
            t.setText(var1);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }


    //create a new file using the utility class: "FileUtility"
    FileUtility myFile = new FileUtility();

    Log.i("Info", "Android File Example Main Activity Completed");

}

@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_main, 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);
}

} }

<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textField" />

If you want to read in a text file and then update a TextView with the contents of the text file, you could try this. 如果要读取文本文件,然后使用文本文件的内容更新TextView,则可以尝试此操作。

First, add the text file to your project in the "raw" folder of your res folder. 首先,将文本文件添加到您的项目中res文件夹的“ raw”文件夹中。 If you don't have a raw folder, you can add one by right clicking on your res folder and selecting New->Android Resource Directory. 如果没有原始文件夹,则可以通过右键单击res文件夹并选择“新建”->“ Android资源目录”来添加一个原始文件夹。 Then in the next dialog you would select "raw" from the "Resource Type" drop down. 然后在下一个对话框中,从“资源类型”下拉列表中选择“原始”。 Name the new directory "raw" click OK to create it. 将新目录命名为“ raw”,单击“确定”以创建它。

Once your raw folder is created, simply place your text file into this folder. 创建原始文件夹后,只需将文本文件放入此文件夹。

Now that your text file is part of your project, you can read it and update a TextView with the context doing something like this: 现在,文本文件已成为项目的一部分,您可以读取文本文件并使用上下文执行以下操作来更新TextView:

StringBuilder stringBuilder = new StringBuilder();
InputStream inputStream = getResources().openRawResource(R.raw.your_text_file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line;

try {
    while ((line = bufferedReader.readLine()) != null) {
        stringBuilder.append(line).append("\n");
    }

    bufferedReader.close();

} catch (IOException ioe) {
    ioe.printStackTrace();
}

TextView textView = (TextView) findViewById(R.id.your_text_view);
textView.setText(stringBuilder.toString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM