简体   繁体   中英

printing out a 2 text files in java

here is my code for printing out two text files but the out put nor right it keeps printing the first line over and over for example the text in the file is : 1 2 3 4 1 2 3

when i call the function the output will be : 1 1 1 1 1 1 1

    public static void printUSER()
{
    BufferedReader br = null;
    BufferedReader br1 = null;
    try {
        br = new BufferedReader(new FileReader("info.txt"));
        br1 = new BufferedReader(new FileReader("info AI.txt"));
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     String line = null;
     String line1 = null;

     try {
        while((line = br.readLine())!= null) {

            while((line1 = br1.readLine())!= null){
           System.out.println(line+"  ===  "+line1);
            }
         }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

You put your loop to read the second file (B) inside the one for the first file (A), so prints out each line of the B paired with the first line from A; it will eventually finish reading B, go back and read the next line of A, but because you read all of B, nothing new will get printed.

It isn't clear what you are attempting to do instead, if only because your example only refers to a single file. And shouldn't the output have some = 's in it? And line breaks?

The reading of info AI.txt has to restart.

For all combinations of lines:

try (BufferedReader br = new BufferedReader(new FileReader("info.txt"))) {
    while ((line = br.readLine()) != null) {

        try (BufferedReader br1 = new BufferedReader(new FileReader("info AI.txt"))) {
            while ((line1 = br1.readLine()) != null) {

For the files one after another:

try (BufferedReader br = new BufferedReader(new FileReader("info.txt"))) {
    while ((line = br.readLine()) != null) {

    }
}
try (BufferedReader br1 = new BufferedReader(new FileReader("info AI.txt"))) {
     while ((line1 = br1.readLine()) != null) {

     }
}

For lines side-by-side:

try (BufferedReader br = new BufferedReader(new FileReader("info.txt"));
        BufferedReader br1 = new BufferedReader(new FileReader("info AI.txt"))) {
    while ((line = br.readLine()) != null
            || (line1 = br1.readLine()) != null) {
        if (line != null) {

        }
        if (line1 != null) {

        }
     }
}

It's because of the double while loop, each round of the first while, the second one restarts. Unless you want to recursively read the entire br1 for each line of br, just change the inner while statement by an if statement:

public static void printUSER()
{
BufferedReader br = null;
BufferedReader br1 = null;
try {
    br = new BufferedReader(new FileReader("info.txt"));
    br1 = new BufferedReader(new FileReader("info AI.txt"));
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
 String line = null;
 String line1 = null;

 try {
    while((line = br.readLine())!= null) {
      line1 = br1.readLine()
        if(line1 != null){
       System.out.println(line+"  ===  "+line1);
        }
     }
 } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
 }
}

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM