如何读出输出所需的.txt文件特定字段中的所有行?

时间:2018-01-14 12:37:02

标签: android text-files

我是Android应用开发的新手。需要你帮助解决这个问题。我没有得到预期的结果。我的.txt文件中大约有108行。对于任何给定的输入,它只读取相同的行。 Wherhas必须根据用户从这108行中的选定值显示该行。

我的文本文件看起来像这样

10

60

40

Z185 -.......

10

60

50

Z186 ..

10

60

90

Z190 -....

20

60

50

Z181 -....。 。 。

35

75

100

Z,...

。 所以108行

如果用户为汽油提供这样的输入值:10,柴油:60,空气:40然后

预期产出:

Z185-...

对此的任何帮助都将受到高度赞赏

这是代码

公共类MainActivity扩展了AppCompatActivity {

private EditText editTextPetrol = null;

private EditText editTextDiesel = null;

private EditText editTextAir = null;

private TextView textViewResult = null;

private Button buttonSubmit = null;

private Context context = null;

FileOutputStream outputStream = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate( savedInstanceState );
    setContentView( R.layout.activity_main );
    context = getApplicationContext();

    editTextPetrol = (EditText) findViewById( R.id.editTextPetrol );
    editTextDiesel = (EditText) findViewById( R.id.editTextDiesel );
    editTextAir = (EditText) findViewById( R.id.editTextAir );
    textViewResult = (TextView) findViewById( R.id.textViewResult );


    buttonSubmit = (Button) findViewById( R.id.buttonSubmit );

    buttonSubmit.setOnClickListener( new MyListener( editTextPetrol, editTextDiesel, editTextAir, textViewResult, context ) );
}

    private void writeFile(String filename, String data)
{

    try {
        outputStream = context.openFileOutput( filename, Context.MODE_PRIVATE );
        outputStream.write( data.getBytes() );
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}


class MyListener implements View.OnClickListener {
    private EditText editTextPetrol = null;
    private EditText editTextDiesel = null;
    private EditText editTextAir = null;
    private TextView textViewResult = null;
    private Context context = null;

    private String petrol = null;
    private String diesel = null;
    private String air = null;
    private String filename = null;
    private String result = null;

    MyListener(EditText editTextPetrol, EditText editTextDiesel, EditText editTextAir, TextView textViewResult, Context context) {
        this.editTextPetrol = editTextPetrol;
        this.editTextDiesel = editTextDiesel;
        this.editTextAir = editTextAir;
        this.textViewResult = textViewResult;
        this.context = context;
    }

    @Override
    public void onClick(View view) {
        petrol = editTextPetrol.getText().toString();
        diesel = editTextDiesel.getText().toString();
        air = editTextAir.getText().toString();
        filename = "file.txt";

        result = foo( filename, petrol, diesel, air );

        if (result == null) {
            result = "No entry found!";
        }

        textViewResult.setText( result );
    }


    File file = null;
    FileReader fileReader = null;
    BufferedReader br = null;
    String currentPetrol = null, currentDiesel = null, currentAir = null, currentResult = null;


    private String foo (String filename,String petrol,String diesel,String air)
    {
        try {
            file = new File( context.getFilesDir(), filename );
            fileReader = new FileReader( file );
            br = new BufferedReader( fileReader );

            while (true) {
                currentPetrol = br.readLine();

                if (currentPetrol == null) {
                    currentResult = null;
                    break;
                }

                currentDiesel = br.readLine();

                if (currentDiesel == null) {
                    currentResult = null;
                    break;
                }

                currentAir = br.readLine();

                if (currentAir == null) {
                    currentResult = null;
                    break;
                }

                currentResult = br.readLine();

                if (currentResult == null) {
                    break;
                }

                if (currentPetrol.equals( petrol ) && currentDiesel.equals( diesel ) && currentAir.equals( air )) {
                    break;
                }
            }

            fileReader.close();
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return currentResult;
    }
}

}

1 个答案:

答案 0 :(得分:0)

以下代码(已测试)在sample.txt中创建名为internal_storage的文件,并尝试根据用户输入访问sample.txt - petrol, diesel & air最后在屏幕上显示结果。

MainActivity.java -

package com.example.sakethkatari.myapplication;

import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;

public class MainActivity extends AppCompatActivity
{
    private EditText editTextPetrol = null;
    private EditText editTextDiesel = null;
    private EditText editTextAir = null;
    private TextView textViewResult = null;
    private Button buttonSubmit = null;

    private Context context = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = getApplicationContext();
        createFile("sample.txt", "10\n20\n30\nSaketh\n40\n50\n60\nKatari\n");

        editTextPetrol = (EditText) findViewById(R.id.editTextPetrol);
        editTextDiesel = (EditText) findViewById(R.id.editTextDiesel);
        editTextAir = (EditText) findViewById(R.id.editTextAir);
        textViewResult = (TextView) findViewById(R.id.textViewResult);
        buttonSubmit = (Button) findViewById(R.id.buttonSubmit);

        buttonSubmit.setOnClickListener( new MyListener(editTextPetrol, editTextDiesel, editTextAir, textViewResult, context) );
    }

    private void createFile(String filename, String data)
    {
        FileOutputStream fos = null;

        try
        {
            fos =context.openFileOutput(filename, Context.MODE_PRIVATE);
            fos.write( data.getBytes() );
            fos.close();
        }

        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

class MyListener implements View.OnClickListener
{
    private EditText editTextPetrol = null;
    private EditText editTextDiesel = null;
    private EditText editTextAir = null;
    private TextView textViewResult = null;
    private  Context context = null;

    private String petrol = null;
    private String diesel = null;
    private String air = null;
    private String filename = null;
    private String result = null;

    MyListener(EditText editTextPetrol, EditText editTextDiesel, EditText editTextAir, TextView textViewResult, Context context)
    {
        this.editTextPetrol = editTextPetrol;
        this.editTextDiesel = editTextDiesel;
        this.editTextAir = editTextAir;
        this.textViewResult = textViewResult;
        this.context = context;
    }

    @Override
    public void onClick(View view)
    {
        petrol = editTextPetrol.getText().toString();
        diesel = editTextDiesel.getText().toString();
        air = editTextAir.getText().toString();
        filename = "sample.txt";

        result = foo(filename, petrol, diesel, air );

        if( result == null )
        {
            result = "No entry found!";
        }

        textViewResult.setText(result);
    }

    private String foo(String filename, String petrol, String diesel, String air)
    {
        FileReader fileReader = null;
        BufferedReader br = null;
        String currentPetrol = null, currentDiesel = null, currentAir = null, currentResult = null;
        File file = null;


        try
        {
            file = new File(context.getFilesDir(), filename);
            fileReader = new FileReader(file);
            br = new BufferedReader(fileReader);

            while(true)
            {
                currentPetrol = br.readLine();

                if( currentPetrol == null )
                {
                    currentResult = null;
                    break;
                }

                currentDiesel = br.readLine();

                if( currentDiesel == null )
                {
                    currentResult = null;
                    break;
                }

                currentAir = br.readLine();

                if( currentAir == null )
                {
                    currentResult = null;
                    break;
                }

                currentResult = br.readLine();

                if( currentResult == null )
                {
                    break;
                }

                if( currentPetrol.equals(petrol) && currentDiesel.equals(diesel) && currentAir.equals(air) )
                {
                    break;
                }
            }

            fileReader.close();
            br.close();
        }

        catch(Exception e)
        {
            e.printStackTrace();
        }

        return currentResult;
    }
}

activity_main.xml -

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewPetrol"
        android:text="Petrol"
        android:layout_marginTop="60dp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editTextPetrol"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewDiesel"
        android:text="Diesel"
        android:layout_marginTop="30dp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editTextDiesel"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewAir"
        android:text="Air"
        android:layout_marginTop="30dp"
        />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editTextAir"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textViewResult"
        android:layout_marginTop="40dp"
        android:layout_gravity="center"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/buttonSubmit"
        android:text="Submit"
        android:layout_gravity="center"
        android:layout_marginTop="60dp"
        />

</LinearLayout>