[英]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.