I'm trying to take some random samples from a file and do some operations and then write them to another file. But I'm having trouble writing to a file in the grepLine method which can be found below.
Inside the while loop of the grepLine function, I get a blank file with many empty lines when I use writer1.write. Please try to avoid the logic of the program. Thanks in advance.
public List<String> list1 = new ArrayList<String>();
private void init() throws Exception {
BufferedWriter writer = new BufferedWriter(new FileWriter("D:\\Dataset14.txt",false));
try {
lines = Files.readAllLines(Paths.get(fileName),
StandardCharsets.UTF_8);
} catch (IOException e) {
System.out.println("File can't be opened.");
}
for(int i=0;i<100;i++)
{
int randomWordIndex = getRandomNumber(1, lines.size());
String eachLine=lines.get(randomWordIndex);
String lineArray= eachLine.substring(0, eachLine.indexOf(","));
list1.add(lineArray);
writer.write(lineArray);
writer.newLine();
}
writer.close();
for(int j=0;j<100;j++){
new RandomSampling().grepLineNumber("D:\\Dataset13.txt", list1.get(j));
}}
public int grepLineNumber(String file, String word,) throws Exception {
BufferedWriter writer1 = new BufferedWriter(new FileWriter("D:\\Dataset15.txt",false));
BufferedReader buf = new BufferedReader(new InputStreamReader(new DataInputStream(new FileInputStream(file))));
String line;
String lineCopy;
int totalLines= 100000;
int height,width;
while ((line = buf.readLine()) != null) {
lineCopy=line;
String [] LineArray=lineCopy.split(",");
lineCopy=LineArray[0];
if (word.equals(lineCopy)) {
System.out.println(line);
writer1.write(line);
}
writer1.newLine();
}
writer1.close();
buf.close();
return -1;
}
private int getRandomNumber(int min, int max) {
return min + (int) (Math.random() * ((max - min) + 1));
}
}
Dataset13.txt :
0,0,1,1
0,0,2,2
0,0,469,469
2,2,0,0
2,2,3,3
3,3,2,2
3,3,4,4
3,3,419,419
3,3,422,422
4,4,3,3
4,4,5,5
4,4,98,98
4,4,420,420
5,5,4,4
5,5,6,6
5,5,98,98
6,6,1,1
6,6,5,5
7,7,8,8
7,7,9,9
7,7,79,79
8,8,7,7
8,8,33,33
9,9,7,7
9,9,10,10
9,9,84,84
10,10,9,9
10,10,11,11
10,10,84,84
10,10,110,110
11,11,10,10
11,11,12,12
11,11,110,110
12,12,11,11
12,12,13,13
12,12,95,95
12,12,108,108
13,13,12,12
13,13,14,14
13,13,94,94
13,13,95,95
14,14,13,13
14,14,15,15
14,14,16,16
14,14,77,77
15,15,14,14
16,16,14,14
16,16,17,17
17,17,16,16
17,17,18,18
17,17,77,77
17,17,3254,3254
18,18,17,17
18,18,19,19
18,18,3254,3254
19,19,18,18
19,19,20,20
19,19,23,23
20,20,19,19
20,20,21,21
20,20,22,22
21,21,20,20
22,22,20,20
23,23,19,19
23,23,24,24
23,23,25,25
24,24,23,23
25,25,23,23
25,25,26,26
25,25,27,27
26,26,25,25
27,27,25,25
27,27,28,28
27,27,29,29
28,28,27,27
29,29,27,27
29,29,30,30
29,29,3255,3255
30,30,29,29
30,30,31,31
30,30,3247,3247
30,30,3253,3253
31,31,30,30
31,31,32,32
31,31,3246,3246
32,32,31,31
32,32,33,33
32,32,2203,2203
33,33,8,8
33,33,32,32
33,33,34,34
34,34,33,33
35,35,36,36
35,35,50,50
35,35,1199,1199
36,36,35,35
36,36,37,37
36,36,35885,35885
36,36,1645159,1645159
37,37,36,36
37,37,38,38
37,37,1641586,1641586
38,38,37,37
38,38,39,39
38,38,1641586,1641586
39,39,38,38
39,39,40,40
39,39,1641587,1641587
40,40,39,39
40,40,41,41
40,40,1641577,1641577
41,41,40,40
41,41,42,42
41,41,1641355,1641355
42,42,41,41
This is only some part of it. The original file has some many nodes and edges.
I write only the first node to the next file.