有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我认为我的代码读取的文件是正确的,但当我运行应用程序时,输出是“Hello World”。这告诉我文本字段没有被覆盖。我认为问题在于while循环。以下是我的java代码:

package com.example.fileio.app1_fileio;

import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.util.Log;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.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\\安卓.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 安卓:text="@string/hello_world" 安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:id="@+id/textField" />


共 (1) 个答案

  1. # 1 楼答案

    如果你想读入文本文件,然后用文本文件的内容更新文本视图,你可以试试这个

    首先,将文本文件添加到res文件夹的“raw”文件夹中的项目中。如果没有原始文件夹,可以通过右键单击res文件夹并选择New->;Android资源目录。然后在下一个对话框中,从“资源类型”下拉列表中选择“原始”。将新目录命名为“raw”,单击“确定”创建它

    创建原始文件夹后,只需将文本文件放入该文件夹即可

    既然文本文件是项目的一部分,您可以阅读它,并使用上下文更新文本视图,操作如下:

    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());