Lena Monika Marshall
Lena Monika Marshall

Reputation: 113

Reading multiple files using BufferedReader(with String[] args)

I want to read texts from two or more files using using main methods array. Is it possible? (my code could be a little strange because I'm a beginner)

Here is my code.

public static void main(String[] args) throws IOException {
    if(args.length != 1)
    {
        System.out.println("wrong number of arguments");
        System.exit(1);
    }
     args[0] = "data.txt"; 
     args[1] = "data2.txt"; 

    BufferedReader file = new BufferedReader(new FileReader(args[0],args[1]));
    int NumberofRows =0, NumberofCharacter =0;
    while(true)
    {
        String row = file.readLine();
        if(row == null)
            break;
        NumberofRows++;
        NumberofCharacter = NumberofCharacter + row.length();

    }
    System.out.println("The files "+" data.txt " + " and " "data2.txt "+ "contains "+ NumberofRows+ "rows and  NumberofCharacter  contains"+
    NumberofCharacter  +" character");

    file.close();

Upvotes: 0

Views: 2718

Answers (5)

Lena Monika Marshall
Lena Monika Marshall

Reputation: 113

i've found shortcut for eclipse.

1) Click on Run -> Run Configurations 2)Click on Arguments tab 3)In Program Arguments section , Enter your arguments. 4)enter image description hereClick Apply

Upvotes: 2

Asma
Asma

Reputation: 163

@Lena : i don't understand why do you affect values to args[] in the main method ?

args[0] = "data.txt";
args[1] = "data2.txt";

Instead you can pass the files' names in the args[] while you execute your main, and test if args[].length < 1, cause args[].length != 1 will always be true if you want to read two files or more. So as @ugurdonmez says : You can create a BufferedReader array with a equal size of args.

    public static void main(String[] args)throws IOException {
                if(args.length <1)
                {
                    System.out.println("wrong number of arguments");
                    System.exit(1);
                }
                for(int i=0; i<args.length; i++)
                {
                   BufferedReader file = new BufferedReader(new FileReader(args[i]));
                   int NumberofRows =0, NumberofCharacter =0;
                   while(row = file.readLine()) !=null)
                   {
                      String row = file.readLine();
                      NumberofRows++;
                      NumberofCharacter = NumberofCharacter + row.length();
                    }
                }
                System.out.println("The files "+" data.txt " + " and " "data2.txt "+ "contains "+ NumberofRows+ "rows and  NumberofCharacter  contains"+
        NumberofCharacter +" character");
               file.close();
            }
    }

Upvotes: 3

Priyamal
Priyamal

Reputation: 2989

String row = ""; 
while(row = file.readLine()) !=null)
    {
        System.out.println(row);
        NumberofRows++;
       NumberofCharacter += row.length();
   }

you might wanna consider this approach when reading the file dont force program to read lines from the file. this might throw exceptions sometimes.

Upvotes: 0

ugurdonmez
ugurdonmez

Reputation: 154

public static void main(String[] args)throws IOException {
    if (args.length != 1) {
        System.out.println("wrong number of arguments");
        System.exit(1);
    }
    args[0] = "data.txt";
    args[1] = "data2.txt";

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

        BufferedReader file = new BufferedReader(new FileReader(args[i]));
        int NumberofRows = 0, NumberofCharacter = 0;
        while (true) {
            String row = file.readLine();
            if (row == null)
                break;
            NumberofRows++;
            NumberofCharacter = NumberofCharacter + row.length();

        }

        file.close();
    }
}

Upvotes: 2

ugurdonmez
ugurdonmez

Reputation: 154

You can create a BufferedReader array with a equal size of args. Then you can construct them with a loop.

Upvotes: 1

Related Questions