从文本文件中读取并存储特定值

时间:2018-03-21 16:42:14

标签: java android android-studio text-files read-write

基本上我下面的代码当前从文本文件中读取,但我想要它做的是存储一个值,以便稍后我可以将它用于另一个函数。所以从文本文件中我想存储高度(175)和权重(80)值。怎么做?

文字档案:

Name: ..........
Height: 175
Weight 80

MainActivity:

package com.example.readfromfiletest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends AppCompatActivity {

    Button b_read;
    TextView tv_text;

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

        b_read = (Button) findViewById(R.id.b_read);
        tv_text = (TextView) findViewById(R.id.tv_text);

        b_read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String text = "";
                try {
                    InputStream is = getAssets().open("test.txt");
                    int size = is.available();
                    byte[] buffer = new byte[size];
                    is.read(buffer);
                    is.close();
                    text = new String(buffer);

                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                tv_text.setText(text);
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

从您的评论来看,听起来您正在询问如何将值正确地读入不同的变量,而不是将它们读入一个String。我认为你应该做的第一件事就是用BufferedReader逐行读取文件。然后,对于您读入的每一行,您可以确定要为其赋值的变量。例如,你可以这样做:

Button b_read;
TextView tv_text;
String name = "";
int height = 0;
int weight = 0;

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

    b_read = (Button) findViewById(R.id.b_read);
    tv_text = (TextView) findViewById(R.id.tv_text);

    b_read.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String text = "";
            try {
                BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(getAssets().open("test.txt")));
                String line;
                while((line = bufferedReader.readLine()) != null){
                    text = text.concat(line + "\n");
                    String[] lineVals = line.split(":");
                    if(lineVals[0].equalsIgnoreCase("name")){
                        name = lineVals[1].trim();
                    } else if(lineVals[0].equalsIgnoreCase("height")){
                        height = Integer.parseInt(lineVals[1].trim());
                    } else if(lineVals[0].equalsIgnoreCase("weight")){
                        weight = Integer.parseInt(lineVals[1].trim());
                    }
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            tv_text.setText(text);
        }
    });
}

BufferedReader一次读取一行。例如,“高度:175”

然后将该行拆分为“:”,返回带有两个值的String[]。继续我们的高度示例,数组看起来像这样:[“Height”,“175”]

if语句(也可以是case语句)然后确定我们是否处理名称,身高或体重变量。

然后将该值分配给其适当的变量。在此分配期间调用trim()方法以删除冒号后的空格。您还可以通过在“:”上执行split()方法来避免这种情况。

你也可以坚持使用当前的方法并进行一些涉及拆分,正则表达式或其他方法的String操作,但我认为我提出的解决方案更容易阅读/使用在将来。

相关问题