0

First off I am new at coding in java. I have done extensive research prior to posting this question but have not found the exact answer to my question. I am sure it is my lack of experience, but any assistance the community can provide would be much appreciated.

I am trying to debug a utility class that I have coded. The code is working except for the bit about adding a new line to the substituted text.

Here is the piece of code that is generating an error in NetBeans IDE. The error is incompatible types: Boolean can't be converted to int

try (BufferedWriter bw = new BufferedWriter(new FileWriter (NewCSVFile),true))

What I am trying to do is get this code to read a CSV text file, substitute and the write the new csv data to a new file but preserve the original new lines in the file. And, I want to ensure that the method used is platform independent thus why I am using BufferedWriter.

Here is all the code for your review.

public class TxtFileConverter {

public static void main (String[] args) {

// Location of the file you want to work with.
File CSVFile = new File("/Users/data.csv");
File NewCSVFile = new File("/Users/NewData.csv");
String search = "[,](?!\\w)";
String replace = ",0";

try{
FileReader fr = new FileReader(CSVFile);
String s;
String totalStr = "";
try (BufferedReader br = new BufferedReader(fr)) {

    while ((s = br.readLine()) != null) {
        totalStr += s;
    }
    totalStr = totalStr.replaceAll(search, replace);
    try (BufferedWriter bw = new BufferedWriter(new FileWriter
    (NewCSVFile),true)) {
        bw.write(totalStr);
        bw.newLine();
    }
}
}catch(IOException e){
    }
  }
}
  • This might help: [How do I get a platform-dependent new line character?](https://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character). Also includes platform independent comments. – K.Nicholas Jul 30 '17 at 19:46

3 Answers3

0

What do you think that true argument is doing in the BufferedWriter constructor? The second and optional argument is the buffer size, an integer. You probably don't even need to supply that argument unless you're doing something rather odd.

try (BufferedWriter bw = new BufferedWriter(new FileWriter(NewCSVFile)))
Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
0

BufferedWriter has only two constructors:

public BufferedWriter(Writer out) //sz = defaultCharBufferSize = 8192
public BufferedWriter(Writer out, int sz) //sz   Output-buffer size, a positive integer

There is no option with second boolean argument. I recommend to use first constructor in your case.

0

I think you are trying to read some lines from a file and write it to another file. In the output file, you get all the code in a single line.

I think the bug is in this piece of code.

while ((s = br.readLine()) != null) {
    totalStr += s;
}

If you add a statement to add a new line character after reading a line from input file, you should get the desired output.

while ((s = br.readLine()) != null) {
    totalStr += s;
    totalStr += "\n";
}

The following code adds a newline character at the end of the file. bw.newLine();

sudhir shakya
  • 827
  • 1
  • 9
  • 21