1

ファイルを順番に読み取るにはどうすればよいですか?

public static void main(String[] args){
    String fichier ="E:\\fichiers\\test.txt";

    int tab[] = {2, 2, 20, 8, 20, 8, 4, 3, 7, 3, 3, 3, 18, 139};
    String tabS[] = new String[14];


    for(int i=0; i<tab.length; i++){

        char cbuf[] = new char[tab[i]];

        try {

            InputStream       ips  = new FileInputStream(fichier); 
            InputStreamReader ipsr = new InputStreamReader(ips);
            BufferedReader    br   = new BufferedReader(ipsr);

            br.read(cbuf, 0, tab[i]);

            tabS[i] = new String(cbuf);
            System.out.println(tabS[i]);

        } catch (Exception e){
            System.out.println(e.toString());
        }
    }
}

私のファイルの内容は次の行のみです:

BOUUUUUUUUUUUUUUUUUUUUUU!

readMeメソッドを実行すると、次のようになりました。

BO
BO
BOUUUUUUUUUUUUUUUUUU
BOUUUUUU
BOUUUUUUUUUUUUUUUUUU
BOUUUUUU
BOUU
BOU
BOUUUUU
BOU
BOU
BOU
BOUUUUUUUUUUUUUUUU
BOUUUUUUUUUUUUUUUUUUUUUU!

ここでの問題は、ファイルを読み始めるたびに位置が 0 になることです。

何か助けてください?

4

2 に答える 2

2

あなたが持っているように見えます:

int tab[] = {2, 2, 20, 8, 20, 8, 4, 3, 7, 3, 3, 3, 18, 139};

あなたの出力は次のとおりです。

BO                        // length 2
BO                        // length 2
BOUUUUUUUUUUUUUUUUUU      // length 20
BOUUUUUU                  // length 8
BOUUUUUUUUUUUUUUUUUU
BOUUUUUU
BOUU
BOU
BOUUUUU
BOU
BOU
BOU
BOUUUUUUUUUUUUUUUU
BOUUUUUUUUUUUUUUUUUUUUUU!

与えられた意味は次のとおりです。

        br.read(cbuf, 0, tab[i]);
        tabS[i] = new String(cbuf);
        System.out.println(tabS[i]);

次があるため、各反復で位置 0 から開始します。

        InputStream       ips  = new FileInputStream(fichier); 
        InputStreamReader ipsr = new InputStreamReader(ips);
        BufferedReader    br   = new BufferedReader(ipsr);

forループ内。これをループから削除するだけで、状況を改善できます。

try 
{
        InputStream       ips  = new FileInputStream(fichier); 
        InputStreamReader ipsr = new InputStreamReader(ips);
        BufferedReader    br   = new BufferedReader(ipsr);

        for(int i=0; i<tab.length; i++)
        {
            char cbuf[] = new char[tab[i]];
            br.read(cbuf, 0, tab[i]);
            tabS[i] = new String(cbuf);

            System.out.println(tabS[i]);
        } 

}
catch (Exception e)
{
    System.out.println(e.toString());
}
于 2013-07-17T21:22:28.260 に答える
2

ストリームが作成された後、for ループを try/catch 内に移動するだけでよいように思えます。

public static void main(String[] args){
    String fichier ="E:\\fichiers\\test.txt";

    int tab[] = {2, 2, 20, 8, 20, 8, 4, 3, 7, 3, 3, 3, 18, 139};
    String tabS[] = new String[14];

    try {

        InputStream       ips  = new FileInputStream(fichier); 
        InputStreamReader ipsr = new InputStreamReader(ips);
        BufferedReader    br   = new BufferedReader(ipsr);

        for(int i=0; i<tab.length; i++){

            char cbuf[] = new char[tab[i]];

            br.read(cbuf, 0, tab[i]);

            tabS[i] = new String(cbuf);
            System.out.println(tabS[i]);
        }

    } catch (Exception e){
        System.out.println(e.toString());
    }
}
于 2013-07-17T21:19:05.113 に答える