简体   繁体   English

如何从两个不同的文件中读取文件,并将每个文件中的10个随机名称放到同一输出文件中,总共20个

[英]How can I read from two different files and put 10 random names from each for a total of 20 into the same output file

I am trying to read names in from a second file but only pick 10 randomly from each file for a total of 20 in the same output file. 我试图从第二个文件中读取名称,但是从每个文件中随机选择10个,在同一输出文件中总共选择20个。 But can't figure out how to read from a second file and put into the same output. 但是无法弄清楚如何从第二个文件读取并放入相同的输出。

public class example2 {

    public static void main(String[] args) throws IOException
    {
// Read in the file into a list of strings
        BufferedReader reader = new BufferedReader(new FileReader("textfile.txt"));
//BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"));
        List<String> lines = new ArrayList<String>();

        String line = reader.readLine();

        while( line != null ) {
            lines.add(line);
            line = reader.readLine();
        }

        try (BufferedWriter writer = new BufferedWriter(new FileWriter("randomNames.txt"))) {


            Random random = new Random();
            Set<String> usedNames = new HashSet<String>(20);

            while (usedNames.size() < 20) {
                int rowNum = random.nextInt(lines.size());
                String name = lines.get(rowNum);
                if (!containsNameWithSameFirstCharacter(usedNames, name)) {
                    usedNames.add(name);
                    writer.write(name);
                    writer.newLine();
                }
            }
            writer.flush();
        }

    }
    private static boolean containsNameWithSameFirstCharacter(Collection<String> names, String name) {
        for (String anotherName: names) {
            if (anotherName.charAt(0) == name.charAt(0)) {
                return true;
            }
        }
        return false;
    }
}

It seems to me that you'd be better off reading all of both files into two lists and then taking items from each until you have your output list. 在我看来,最好将两个文件都读入两个列表,然后从每个列表中取出项目,直到获得输出列表。

Something like: 就像是:

List<String> list1 = Files.readAllLines(path1);
List<String> list2 = Files.readAllLines(path2);
List<String> output = new ArrayList<>();
Collections.shuffle(list1);
Collections.shuffle(list2);
while (output.size() < 20) {
    addFromList(list1, output);
    addFromList(list2, output);
}

private void addFromList(List<String> from, List<String> to) {
    String name = from.remove(0);
    while (containsNameWithSameFirstCharacter(to, next))
        name = from.remove(0);
    to.add(name);
}

This needs some error checking for empty lists but hopefully you get the idea. 这需要对空列表进行一些错误检查,但希望您能理解。

public static void main(String[] args){
    try {
        BufferedWriter writer = new BufferedWriter(new FileWriter(new File("outputfile.txt")));
        List<String> a = readFile(new File("textfile1.txt"));
        List<String> b = readFile(new File("textfile2.txt"));
        List<String> all = a;
        all.addAll(b);
        Random random = new Random();
        for(int i= 0; i < 10;){
            int l = random.nextInt(all.size() - 1);
            i++;
            String n = all.get(l);
            writer.newLine();
            writer.write(n);
        }
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private static List<String> readFile(File file) throws IOException {
    Path p = Paths.get(file.toURI());
    List<String> ls = Files.readAllLines(p);
    return ls;
}

This will read the two files, get its contents, add them to a list, pick 10 of them, and write them to a file. 这将读取两个文件,获取其内容,将它们添加到列表中,选择其中的10个并将它们写入文件。 One name = one line. 一个名字=一行。

Of course this is not all your code for your purpose. 当然,这并不是您所需要的全部代码。 This is just a starter that does the trick for you. 这只是为您解决问题的入门工具。

暂无
暂无

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

相关问题 如何有效地从10 G文件中读取单词,并按频率排列它们? - How can I efficiently read words from a 10 G file and put them in order of their frequency? 如何在不复制代码的情况下从两个不同的CSV文件读取并创建两个不同的数组? - How can I read from two different CSV files and create two different arrays without duplicating code? 如何在Java中使用SequenceInputStream从两个不同的文件中读取两个对象 - how can I read two object from two different file, using SequenceInputStream in java 如何在Spring Batch中从两个不同的文件夹中读取多个xml文件? - How can I read multiple xml files from two different folders in Spring Batch? 如何从文件读取数据并将其放入变量并输出到其他文件。 爪哇 - How to read data from a file and put it into variable and output to a different file. Java 如何从.txt文件读取随机文本? - How can I read random text from .txt file? 如何避免两个不同的线程从 DB 读取相同的行(Hibernate 和 Oracle 10g) - How to avoid two different threads read the same rows from DB (Hibernate and Oracle 10g) 我可以从同一个jar文件中执行两个不同的类吗? - Can I execute Two different Classes from same jar file? 如何从两个文本文件中读取并写入一个文本文件 - How can i read from two text files and write to one text file Java:我如何从文件的每一行中读取4个数字。 并将它们放入变量中? - Java: How do I read 4 numbers from each line in a file. And put them into variables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM