我有一项任务是创建一个程序来读取一个文本文件babynames.txt
,该文件有一长串的男孩和女孩名字,然后生成带有男孩名字和女孩名字的两个文件名。我有同一文件夹中的文本文件,我没有收到任何错误消息,但它没有产生任何输出。
它应该创建两个文本文件吗?我不太了解这一点,我似乎无法自己解决这个问题。
以下是说明:
编写一个读入working_example_1 / babynames.txt的程序,并生成两个
files, boynames.txt
和girlnames.txt
,将男孩和女孩的数据分开。(您将阅读
babynames.txt
文件并将男孩名称与女孩名称分开,并创建男孩和女孩文本文件。您将从babyname.txt
文件创建两个文本文件。)
这是我的代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Scanner;
public class babyNames {
public static void main(String[] args) throws FileNotFoundException {
HashSet<String> maleNames = new HashSet<String>();
HashSet<String> femaleNames = new HashSet<String>();
Scanner fileRead = new Scanner(new File("babynames.txt"));
while (fileRead.hasNext()) {
int rank = fileRead.nextInt();
String maleName = fileRead.next();
int matches = fileRead.nextInt();
double percent = fileRead.nextDouble();
String femaleName = fileRead.next();
matches = fileRead.nextInt();
percent = fileRead.nextDouble();
maleNames.add(maleName);
femaleNames.add(femaleName);
}
fileRead.close();
writeBabyNames("male_baby_names", maleNames);
writeBabyNames("female_baby_names", femaleNames);
}
public static void writeBabyNames(String filename, HashSet<String> names) throws FileNotFoundException {
PrintWriter fileWrite = new PrintWriter(new File(filename));
for (String name : names) {
fileWrite.println(name);
}
fileWrite.close();
}
}
babynames.txt:
1 Michael 663322 0.0 Jessica 469368 0.0
2 Christopher 554622 0.0 Jennifer 440774 0.0
3 Matthew 458723 0.0 Amanda 369646 0.0
4 Joshua 396431 0.0 Ashley 352093 0.0
5 David 383426 0.0 Sarah 272470 0.0
6 James 356238 0.0 Stephanie 218084 0.0
7 Daniel 345284 0.0 Melissa 217868 0.0
8 Robert 321498 0.0 Nicole 210417 0.0
9 John 320905 0.0 Elizabeth 198889 0.0
10 Joseph 299243 0.0 Heather 191814 0.0
11 Jason 291919 0.0 Tiffany 158582 0.0
12 Justin 289704 0.0 Michelle 153954 0.0
13 Andrew 284916 0.0 Amber 153907 0.0
14 Ryan 278765 0.0 Megan 153470 0.0
15 William 248150 0.0 Amy 150626 0.0
16 Brian 233884 0.0 Rachel 146632 0.0
17 Brandon 232099 0.0 Kimberly 146426 0.0
18 Jonathan 229812 0.0 Christina 145955 0.0
19 Nicholas 223197 0.0 Lauren 145093 0.0
20 Anthony 211723 0.0 Crystal 140984 0.0
21 Eric 204115 0.0 Brittany 140814 0.0
22 Adam 194962 0.0 Rebecca 136195 0.0
23 Kevin 188558 0.0 Laura 134533 0.0
24 Thomas 177651 0.0 Danielle 134337 0.0
25 Steven 177345 0.0 Emily 131717 0.0
26 Timothy 172767 0.0 Samantha 121081 0.0
27 Richard 154647 0.0 Angela 116565 0.0
28 Jeremy 153366 0.0 Erin 115792 0.0
29 Jeffrey 144747 0.0 Kelly 109517 0.0
30 Kyle 141855 0.0 Sara 104621 0.0
31 Benjamin 141267 0.0 Lisa 102579 0.0
32 Aaron 138271 0.0 Katherine 99128 0.0
33 Charles 131815 0.0 Andrea 98225 0.0
34 Mark 129327 0.0 Jamie 96540 0.0
35 Jacob 124134 0.0 Mary 95807 0.0
36 Stephen 113556 0.0 Erica 93123 0.0
37 Patrick 111321 0.0 Courtney 84287 0.0
38 Scott 108114 0.0 Kristen 83216 0.0
39 Nathan 105906 0.0 Shannon 78408 0.0
40 Paul 104301 0.0 April 75930 0.0
41 Sean 103781 0.0 Katie 72830 0.0
42 Travis 102887 0.0 Lindsey 71921 0.0
43 Zachary 101758 0.0 Kristin 70543 0.0
44 Dustin 97282 0.0 Lindsay 68477 0.0
45 Gregory 93612 0.0 Christine 67976 0.0
46 Kenneth 89090 0.0 Alicia 66956 0.0
47 Jose 86521 0.0 Vanessa 65689 0.0
48 Tyler 86395 0.0 Maria 65597 0.0
49 Jesse 85862 0.0 Kathryn 62431 0.0
50 Alexander 81883 0.0 Allison 62312 0.0
51 Bryan 80464 0.0 Julie 61664 0.0
52 Samuel 73359 0.0 Anna 57247 0.0
53 Derek 72873 0.0 Tara 56322 0.0
54 Bradley 69325 0.0 Kayla 55281 0.0
55 Chad 69028 0.0 Natalie 54402 0.0
56 Shawn 65558 0.0 Victoria 53227 0.0
57 Edward 60774 0.0 Monica 51829 0.0
58 Jared 60267 0.0 Jacqueline 51538 0.0
59 Cody 60241 0.0 Holly 49643 0.0
60 Jordan 57122 0.0 Kristina 45937 0.0
61 Peter 56608 0.0 Patricia 45113 0.0
62 Corey 53584 0.0 Cassandra 44894 0.0
63 Keith 52615 0.0 Brandy 44519 0.0
64 Marcus 51760 0.0 Whitney 44364 0.0
65 Juan 51380 0.0 Chelsea 43150 0.0
66 Donald 50841 0.0 Brandi 42883 0.0
67 Ronald 46646 0.0 Catherine 42398 0.0
68 Phillip 45675 0.0 Cynthia 41649 0.0
69 George 45657 0.0 Kathleen 39391 0.0
70 Cory 44338 0.0 Veronica 39208 0.0
71 Joel 43545 0.0 Leslie 38699 0.0
72 Shane 43143 0.0 Natasha 38195 0.0
73 Douglas 41728 0.0 Krystal 37492 0.0
74 Antonio 41581 0.0 Stacy 36984 0.0
75 Raymond 41477 0.0 Diana 36872 0.0
76 Carlos 41430 0.0 Erika 36817 0.0
77 Brett 41270 0.0 Dana 36808 0.0
78 Gary 40218 0.0 Jenna 36576 0.0
79 Alex 38655 0.0 Meghan 36196 0.0
80 Nathaniel 38629 0.0 Carrie 36076 0.0
81 Craig 38077 0.0 Leah 36031 0.0
82 Ian 37747 0.0 Melanie 35925 0.0
83 Luis 37373 0.0 Brooke 34973 0.0
84 Derrick 36338 0.0 Karen 34895 0.0
85 Erik 36319 0.0 Alexandra 34770 0.0
86 Casey 35965 0.0 Valerie 34611 0.0
87 Philip 35693 0.0 Caitlin 33591 0.0
88 Frank 33557 0.0 Julia 33311 0.0
89 Evan 32463 0.0 Alyssa 32948 0.0
90 Gabriel 32273 0.0 Jasmine 32931 0.0
91 Victor 32226 0.0 Hannah 32667 0.0
92 Vincent 32014 0.0 Stacey 32122 0.0
93 Larry 31273 0.0 Brittney 31528 0.0
94 Austin 31268 0.0 Susan 30517 0.0
95 Brent 31257 0.0 Margaret 30337 0.0
96 Seth 31110 0.0 Sandra 30008 0.0
97 Wesley 31078 0.0 Candice 29844 0.0
98 Dennis 30445 0.0 Latoya 29531 0.0
99 Todd 30314 0.0 Bethany 29332 0.0
100 Christian 30220 0.0 Misty 29113 0.0
101 Curtis 29886 0.0 Katrina 28825 0.0
102 Jeffery 29568 0.0 Tracy 28485 0.0
103 Randy 29495 0.0 Casey 27911 0.0
104 Jeremiah 28313 0.0 Kelsey 27746 0.0
105 Adrian 28101 0.0 Kara 27496 0.0
106 Jesus 27356 0.0 Nichole 27232 0.0
107 Luke 27015 0.0 Alison 27010 0.0
108 Alan 26784 0.0 Heidi 26371 0.0
109 Trevor 26667 0.0 Alexis 26304 0.0
110 Russell 26640 0.0 Molly 25880 0.0
111 Mario 26370 0.0 Tina 25827 0.0
112 Lucas 26068 0.0 Pamela 25734 0.0
113 Jerry 25743 0.0 Rachael 25661 0.0
114 Miguel 25512 0.0 Nancy 25470 0.0
115 Carl 25451 0.0 Jillian 25442 0.0
116 Blake 25376 0.0 Candace 25156 0.0
117 Cameron 24574 0.0 Denise 25109 0.0
118 Mitchell 24521 0.0 Sabrina 24625 0.0
119 Troy 24517 0.0 Gina 24444 0.0
120 Tony 24471 0.0 Renee 24407 0.0
121 Shaun 24464 0.0 Jill 24401 0.0
122 Terry 24156 0.0 Kendra 24381 0.0
123 Johnny 23483 0.0 Morgan 24276 0.0
124 Martin 23183 0.0 Brenda 24132 0.0
125 Ricardo 23064 0.0 Monique 23885 0.0
126 Bobby 22010 0.0 Teresa 23393 0.0
127 Johnathan 21888 0.0 Krista 22859 0.0
128 Allen 21885 0.0 Linda 22800 0.0
129 Devin 21695 0.0 Miranda 22714 0.0
130 Jorge 21693 0.0 Robin 22688 0.0
131 Andre 21566 0.0 Dawn 22402 0.0
132 Henry 20688 0.0 Kristy 22380 0.0
133 Billy 20645 0.0 Theresa 22379 0.0
134 Caleb 20631 0.0 Tanya 22293 0.0
135 Marc 20499 0.0 Wendy 22260 0.0
136 Garrett 20465 0.0 Melinda 22206 0.0
137 Ricky 20150 0.0 Joanna 22188 0.0
138 Kristopher 20067 0.0 Anne 22098 0.0
139 Francisco 20056 0.0 Felicia 21941 0.0
140 Danny 19999 0.0 Desiree 21808 0.0
141 Manuel 19961 0.0 Jaclyn 21798 0.0
142 Lee 19776 0.0 Alisha 21744 0.0
143 Lawrence 19622 0.0 Lori 21641 0.0
144 Jonathon 19576 0.0 Tamara 21219 0.0
145 Jimmy 19509 0.0 Marissa 21093 0.0
146 Lance 19443 0.0 Kelli 21047 0.0
147 Taylor 19406 0.0 Lacey 20771 0.0
148 Randall 19321 0.0 Abigail 20636 0.0
149 Micheal 18731 0.0 Christy 20224 0.0
150 Mathew 18617 0.0 Jenny 19710 0.0
151 Albert 18480 0.0 Tabitha 19573 0.0
152 Jamie 18407 0.0 Colleen 19453 0.0
153 Isaac 18350 0.0 Meredith 19193 0.0
154 Roger 17982 0.0 Barbara 19106 0.0
155 Rodney 17865 0.0 Angelica 19067 0.0
156 Roberto 17803 0.0 Carolyn 18625 0.0
157 Jon 17695 0.0 Rebekah 18314 0.0
158 Colin 17648 0.0 Ebony 18256 0.0
159 Walter 17562 0.0 Deanna 18150 0.0
160 Clinton 17525 0.0 Tonya 17982 0.0
161 Louis 17339 0.0 Caroline 17923 0.0
162 Clayton 16918 0.0 Kristi 17706 0.0
163 Willie 16893 0.0 Kari 17543 0.0
164 Arthur 16780 0.0 Michele 17254 0.0
165 Chase 16539 0.0 Brianna 17127 0.0
166 Joe 16500 0.0 Bridget 17096 0.0
167 Jack 15886 0.0 Angel 16739 0.0
168 Jay 15803 0.0 Marie 16509 0.0
169 Angel 15784 0.0 Sharon 16493 0.0
170 Calvin 15766 0.0 Sheena 16399 0.0
171 Ross 15710 0.0 Tasha 16252 0.0
172 Darren 15687 0.0 Meagan 16248 0.0
173 Oscar 15635 0.0 Jaime 16208 0.0
174 Drew 15572 0.0 Cindy 16012 0.0
175 Maurice 15568 0.0 Priscilla 15999 0.0
176 Gerald 15506 0.0 Ann 15923 0.0
177 Alejandro 15274 0.0 Ashlee 15844 0.0
178 Spencer 15169 0.0 Stefanie 15827 0.0
179 Hector 15085 0.0 Cassie 15756 0.0
180 Ruben 14844 0.0 Adrienne 15665 0.0
181 Wayne 14739 0.0 Tammy 15647 0.0
182 Brendan 14572 0.0 Ana 15484 0.0
183 Grant 14503 0.0 Beth 15371 0.0
184 Javier 14459 0.0 Dominique 15093 0.0
185 Bruce 14315 0.0 Mallory 15083 0.0
186 Roy 14285 0.0 Latasha 15031 0.0
187 Dylan 14156 0.0 Cristina 15013 0.0
188 Logan 13965 0.0 Virginia 14981 0.0
189 Edwin 13895 0.0 Deborah 14770 0.0
190 Omar 13651 0.0 Katelyn 14770 0.0
191 Brad 13564 0.0 Audrey 14651 0.0
192 Reginald 13495 0.0 Regina 14570 0.0
193 Fernando 13177 0.0 Carla 14442 0.0
194 Darrell 13155 0.0 Cheryl 14319 0.0
195 Sergio 13131 0.0 Olivia 14009 0.0
196 Frederick 13040 0.0 Autumn 13820 0.0
197 Julian 12914 0.0 Jordan 13784 0.0
198 Jaime 12835 0.0 Claudia 13778 0.0
199 Jermaine 12803 0.0 Nina 13616 0.0
200 Geoffrey 12720 0.0 Taylor 13605 0.0
答案 0 :(得分:0)
确保为这两个文件指定正确的文件路径 而且你也可以使用FileWriter和BufferedWriter而不是PrintWriter。 您可以使用以下代码作为示例
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
确保内容为字符串类型,其中包含格式化数据,方式要求它出现在文件中。
希望能帮助到你!! :)