简体   繁体   中英

Reading from a text file and storing specific values

Basically the code I have below does currently read from a text file, but what I want it to do is store a value so that I can use it later for a another function. So from the text file I would like to store the height (175) and weight (80) value. How would that be done?

Text File:

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

Judging from your comments, it sounds like you're asking how to properly read in the values into different variables rather than reading them into one String . I think the first thing you should do to achieve this is read the file in line by line with a BufferedReader . Then for each line you read in you can determine which variable to assign the value to. For instance, you could do this:

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

The BufferedReader reads in one line at a time. For example just, "Height: 175"

The line is then split on the ":", returning a String[] with two values. Continuing with our Height example, the array looks something like this: ["Height", " 175"]

The if statements (could also be case statements) then determine whether we're dealing with the name, height or weight variable.

The value is then assigned to its appropriate variable. The trim() method is called during this assignment to remove the space after the colon. You could also circumvent this by performing the split() method on ": ".

You could also stick with your current method and do some String manipulation involving splitting, Regex, or some other method, but I am of the opinion that my proposed solution will be a bit easier to read/work with in the future.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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