简体   繁体   English

我正在尝试读取文本文件,然后拆分行,以便获得两组不同的名称

[英]I'm trying to read a text file and then split the lines so that i get two different sets of names

The first group should contain odd lines and second should create even but its not working? 第一组应包含奇数行,第二组应创建偶数行,但不起作用? (in java) (在Java中)

code: 码:

import java.io.*;

class InArray2 {

    public static void main(String[] args) {
        try {
            FileReader fr = new FileReader("names.txt");
            BufferedReader br = new BufferedReader(fr);
            BufferedReader gr = new BufferedReader(fr);
            String register1;
            String register2;
            int counter = 0;
            System.out.println("Coursework A will be done by :");
            while ((register1 = br.readLine()) != null ) {
            counter++;
            if (counter == 1 || counter == 3 || counter == 5 || counter == 7 || counter == 9 ) 
               System.out.println(register1); 
            }

            System.out.println("Coursework B will be done by :");
            while ((register2 = gr.readLine()) ! = null ) {
            counter++;
            if (counter == 2 || counter == 4 || counter == 6 || counter == 8 || counter == 10 )
                    System.out.println(register2);
        }


            br.close();
        } catch (IOException e) {
            System.out.println("file not found");
        }

    }
}

Change your buffered reader to : 将缓冲的阅读器更改为:

   BufferedReader br = new BufferedReader(new FileReader("names.txt"));
   BufferedReader gr = new BufferedReader(new FileReader("names.txt"));

I think the other solution don't do an copy but an reference streamed. 我认为其他解决方案不复制,而是流式传输参考。


But I thinks the good way is to store the data into an two array with one loop and print the result after. 但是我认为一个好方法是将数据存储到一个循环的两个数组中,然后打印结果。 Normaly you need only one buffer for read a file. 通常,您只需要一个缓冲区即可读取文件。 And only one fileReader for one file :) Check your algo 一个文件只有一个fileReader :)检查您的算法

Your BufferredReader 's share the same FileReader instance. 您的BufferredReader共享相同的FileReader实例。

It seems that changing code to: 似乎将代码更改为:

    FileReader fr = new FileReader("names.txt");
    FileReader fr1 = new FileReader("names.txt");
    BufferedReader br = new BufferedReader(fr);
    BufferedReader gr = new BufferedReader(fr1);

will work. 将工作。

But it won't work. 但这是行不通的。 You need to reset counter to 0 before printing the second group: 您需要在打印第二组之前将计数器重置为0:

counter = 0;
System.out.println("Coursework B will be done by :");

One last thing: use counter % 2 == 0 to check if the counter value is even 最后一件事:使用counter % 2 == 0检查计数器值是否为偶数

And another last thing: you have massive code duplication here. 最后一件事:您在这里有大量的代码重复。 It's better to create a method that'll print even or odd lines. 最好创建一个可以打印偶数行或奇数行的方法。

public static void printOnlyModulo(int modulo, String header) {
    System.out.println(header);
    try {
        int counter = 0;
        BufferedReader reader = new BufferedReader(new FileReader("names.txt"));
        String line;
        while ((line = reader.readLine()) != null) {
            counter++;
            if (counter % 2 == modulo) {
                System.out.println(line);
            }
        }
        reader.close();
    }
    catch (IOException e) {
        System.out.println("file not found");
    }
}

And then call this method from main : 然后从main调用此方法:

public static void main(String[] args) {
    printOnlyModulo(1, "Coursework A will be done by :");
    printOnlyModulo(0, "Coursework B will be done by :");
}

It's cleaner. 比较干净

You only need to read the file one time. 您只需要读取一次文件。

... blah.
int lineNumber = 1;
List<String> theAList = new LinkedList<String>();
List<String> theBList = new LinkedList<String>();
while ((line = reader.readLine()) != null)
{
    if (lineNumber % 2) // even are on the b list.
    {
        theBList.add(line);
    }
    else // odd are on the a list.
    {
        theAList.add(line);
    }
}

System.out.println("A list");
for (String current : theAList)
{
    System.out.println(current);
}

System.out.println("B list");
for String current : theBList)
{
    System.out.println(current);
}

暂无
暂无

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

相关问题 我正在尝试阅读此文本文件 - I'm trying to read this text file 我正在尝试读取文本文件并将其存储在对象数组列表中 - I'm trying to read a text file and store it in an arraylist of objects 如何将文本文件拆分为两个不同的数组? - How Am I able to split a text file into two different arrays? 尝试在android中的AsyncTask中读取文本文件,但结果为空 - Trying to read text file in AsyncTask in android, but i get empty result 我正在尝试读取文本文件并查找特定匹配项并打印它,但我的代码不起作用 - I'm trying to read a text file and find specific matches and print it , but my code does not working 在Java中,我需要读取一个文本文件并将每一行放在单独的数组中。 但是每次阅读文本文件时,我都无法分开 - In Java I need to read a text file and put each line in a separate array. But every time I read the text file I cannot split the lines 我正在尝试读取CSV文件并将其在excel中显示为CSV文件 - I'm trying to read a CSV file and display it in excel as a csv file 我试图让循环在 b 的条件下运行,但它不会读取它 - I'm trying to get the loop to run on the conditions of b but it wont read it 如何使用Java获取文本文件的两行之间的文本 - How can I get the text between two lines of a text file using java 我正在尝试摆脱空格并将文本文件转换为单行 - I'm trying to get rid of spaces and convert a text file into a single line
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM