Gentuzos
Gentuzos

Reputation: 265

How can I change this main method to read files sequentially

How can I read files sequentially ?

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

My file contents only this line :

BOUUUUUUUUUUUUUUUUUUUUUU!

When I run readMe method, I got this:

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

the problem here is that every time he begins to read the file it begins to position 0.

Any help please ?

Upvotes: 1

Views: 109

Answers (2)

Steve P.
Steve P.

Reputation: 14699

Look you have:

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

Your output is:

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

Which makes sense given:

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

It begins at position 0 in each iteration because you have:

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

inside the for loop. Simply remove this from the loop to remedy the situation:

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

Upvotes: 2

James Holderness
James Holderness

Reputation: 23001

It looks to me like you just need to move your for loop inside the try/catch after the streams are created.

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

Upvotes: 2

Related Questions