简体   繁体   English

在Java中打印出2个文本文件

[英]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 这是我的代码,用于打印两个文本文件,但是输出也不正确,它会不断重复打印第一行,例如文件中的文本是:1 2 3 4 1 2 3

when i call the function the output will be : 1 1 1 1 1 1 1 当我调用该函数时,输出将是: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; 您将循环放入第一个文件(A)中的第二个文件(B)中,因此打印出B的每一行与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. 它最终将完成读取B的操作,返回并读取A的下一行,但是由于您已读取所有的B,因此不会打印任何新内容。

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. 信息AI.txt的读取必须重新开始。

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. 这是因为双while循环,第一轮的每一轮,第二轮重启。 Unless you want to recursively read the entire br1 for each line of br, just change the inner while statement by an if statement: 除非您要递归读取br每一行的整个br1,否则只需通过if语句更改内部while语句即可:

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 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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