2

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.

Kaushik
  • 553
  • 3
  • 11
  • 27

1 Answers1

1

In this section of your code

 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();


}
writer.close();

You should probably replace writer.close()with writer1.close(); for the only other writer variable that appears in your code is a local variable in your init method, so I think it is just a typing mistake.

Since your are inserting a call to writer1.newLine()after the test of the condition, therefore you will still have the writer inserting a line skip which leads to you having a blank file with many lines.

So first change that section of code to:

 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();
    }

Now, you will probably have a blank file with no empty lines inside. You need to debug and see why the condition is never fulfilled it may be that you are having a case issue so just replace

if (word.equals(lineCopy)) {

by:

 if (word.equalsIgnoreCase(lineCopy)) {
    ....
 }

That way if the word your are looking is for example Testbut the word from the file is tEsTor any other letter uppercase or lowercase combination, it would still find it.

There may be other reasons for your issues but it will be difficult to investigate without further explanations about your code

[EDIT] OK, since the condition is satisfied, how about flushing the writer after each line like:

 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.flush();
    }

Also this:

for(int j=0;j<100;j++){
    new RandomSampling().grepLineNumber("D:\\Dataset13.txt", list1.get(j));
}

means that it will call the grepLineNumbermethod as many time as j values which is a 100 times and each it will process only one node value. That's why you are having only three lines written to your file. Change the following line

BufferedWriter writer1 = new BufferedWriter(new 
    FileWriter("D:\\Dataset15.txt",false));

to

BufferedWriter writer1 = new BufferedWriter(new 
    FileWriter("D:\\Dataset15.txt",true));

For the last boolean parameter append means

  • if true it will append the new writing operations at the end of the file
  • if false it will generate a new file.

This should finaly fixe your issues

alainlompo
  • 4,414
  • 4
  • 32
  • 41
  • But, the condition is satisfied, because i get the output through System.out.println() – Kaushik May 18 '15 at 20:34
  • @KaushikS I have edited my answer: try a `writer1.flush();` after the newLine instruction; – alainlompo May 18 '15 at 20:38
  • Actually, I've two nodes in the file seperated by comma. I take only the first node and compare it with each line in another file and write only those which matches. After flushing, it writes only three lines to the file with the same start node and different end nodes. – Kaushik May 18 '15 at 20:43
  • @KaushikS then the main issue was due to lack of flushing. Now the second issue you are speaking about is related to the logic you are implementing. Maybe you could share sample contents of the two files and edit your question with additionnal informations so we may be able to help – alainlompo May 18 '15 at 20:46
  • @KaushikS you are most welcome!!! It was a pleasure to `java` with you. See you around. Best wishes. – alainlompo May 18 '15 at 21:15