简体   繁体   English

使用BufferedWriter和newLine属性在文本字符串后插入一行

[英]Using BufferedWriter and the newLine attribute to insert a line after a string of text

First off I am new at coding in java. 首先,我是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. 这是在NetBeans IDE中生成错误的代码。 The error is incompatible types: Boolean can't be converted to int 错误是不兼容的类型:布尔值不能转换为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. 我正在尝试做的是获取此代码以读取CSV文本文件,替换并将新的csv数据写入新文件,但保留文件中的原始新行。 And, I want to ensure that the method used is platform independent thus why I am using BufferedWriter. 并且,我想确保所使用的方法独立于平台,因此为什么我要使用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){
    }
  }
}

What do you think that true argument is doing in the BufferedWriter constructor? 您认为true参数在BufferedWriter构造函数中做什么? 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)))

BufferedWriter has only two constructors: BufferedWriter只有两个构造函数:

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. 我建议您使用第一个构造函数。

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(); bw.newLine();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM