1

ばかげた質問をしているだけだと確信していますが、このデータを含むtxtファイルがあると言ってください

UniPub;112 Binara St;ACT
MooseHeads;54 Cohen St;ACT
Cube;24 Mawson St;ACT

このコードを使用して、アプリケーションでそれを読み取ります。

package au.edu.canberra.g30813706;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

import android.app.Activity;
import android.os.Environment;


public class FileReader extends Activity{{

    ArrayList<String> sInfo = new ArrayList<String>();

    String txtName = "AccomodationTxt.txt";
    File root = Environment.getExternalStorageDirectory();
    File path = new File(root, "CanberraTourism/" + txtName);

    try {

        BufferedReader br = new BufferedReader (
                            new InputStreamReader(
                            new FileInputStream(path)));
        String line;
        String[] saLineElements;
        while ((line = br.readLine()) != null)
        {
            //The information is split into segments and stored into the array
            saLineElements = line.split(";");
            for (int i = 0; i < saLineElements.length; i++) 
                  sInfo.add(saLineElements[i]);
            //sInfo.addAll(Arrays.asList(saLineElements[0], saLineElements[1], saLineElements[3]));     
        }
         br.close();


    } 
    catch (FileNotFoundException e) {

        System.err.println("FileNotFoundException: " + e.getMessage());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }}
}

配列内の行をどのように区別しますか?

例えば

名前のテキストを 1 ページに表示したいので、

UniPub
MooseHeads
キューブ

4

1 に答える 1

1

個々の要素の代わりに String 配列を ArrayList に追加するのはどうですか。
このような:

ArrayList<String[]> sInfo = new ArrayList<String[]>();
String line;
String[] saLineElements;
while ((line = br.readLine()) != null)
{
    //The information is split into segments and stored into the array
    saLineElements = line.split(";");
        sInfo.add(saLineElements);
}

次に、sInfo をループして、sInfo の各配列の最初の要素を使用できます。

于 2013-05-02T12:42:35.660 に答える