读取和写入同一文件-输出格式更改

Supun Amarasinghe:

我想读取文件,并根据某些条件将一些文本附加到同一文件中。这是我的代码。

 public static void writeOutputToFile(ArrayList<String> matchingCriteria) {

    //Provide the folder which contains the files
    final File folder = new File("folder_location");
    ArrayList<String> writeToFile = new ArrayList<String>();

    //For each file in the folder
    for (final File fileEntry : folder.listFiles()) {

        try (BufferedReader br = new BufferedReader(new FileReader(fileEntry))) {
            String readLine = "";

            while ((readLine = br.readLine()) != null) {
                writeToFile.add(readLine);
            }

            try (FileWriter fw = new FileWriter(fileEntry); BufferedWriter bw = new BufferedWriter(fw)) {
                for (String s : writeToFile) {
                    boolean prefixValidation = false;
                    //Check whether each line contains one of the matching criterias. If so, set the condition to true
                    for (String y : matchingCriteria) {
                        if (matchingCriteria.contains(y)) {
                            prefixValidation = true;
                            break;
                        }
                    }
                    //Check if the prefixes available in the string
                    if (prefixValidation) {
                        if (s.contains("name=\"") && !(s.contains("id=\""))) {
                            //Split the filtered string by ' name=" '
                            String s1[] = s.split("name=\"");

                            /*Some Code*/                         
                            //Set the final output string to be written to the file
                            String output = "Output_By_Some_Code";

                            //Write the output to the file.
                            fw.write(output);

 //If this action has been performed, avoid duplicate entries to the file by  continuing the for loop instead of going through to the final steps
                                continue;
                            }
                        }
                        fw.write(s);
                        bw.newLine();
                    }             
                    fw.flush();
                    bw.flush();
 //Clear the arraylist which contains the current file data to store new file data.
                        writeToFile.clear();
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
    }

此代码可以正常工作。问题是,输出与输入文件不完全相同。我将附加一些内容的输入文件中的多行写入输出文件中的同一行。

例如,我为这些元素添加了一个id属性,并添加了它,但是输出却写成一行。

<input type="submit" <%=WebConstants.HTML_BTN_SUBMIT%> value="Submit" />
<input type="hidden" name="Page" value="<%=sAction%>" />
<input type="hidden" name="FileId" value="" />

我的问题是,我做错什么了吗,以致格式混乱了?
如果是这样,是否需要做一些与输入文件完全相同的打印?

非常感谢您的帮助。提前致谢 :)

利诺(Lino):

要解决您的问题,只需在\n要编写的每一行中添加一个额外的行分隔符()。

所以这个:writeToFile.add(readLine);变成这个:writeToFile.add(readLine+"\n");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

更改Assetic的输出不会使其从同一文件夹中读取

更改JSON_Encode输出格式(括号和逗号)

将多个不同记录的数组以Avro格式写入同一文件

读取和写入同一文件fstream

流利的:同一文件,不同的过滤器和输出

使用smtplib发送文件时更改标准输出格式-python2.7

proc printto:日志和过程输出到同一文件不会刷新%put输出到日志文件

为什么在同一文件中'grep“ * .h”'和'grep -E“ * .h”'的不同输出

Bash Shell重定向标准输出和错误到同一文件中的事件顺序

在同一文件夹中的多个CSV文件中更改定界符,并将它们写入新文件夹

Solaris pstack如何读取输出/输出格式

将文件的目录输出重定向到同一文件

如何通过将格式更改为同一文件来插入文件名?

在一行中运行两个PowerShell命令时,为什么更改了输出格式?

在Java中读取和写入同一文件

如何使用spark(scala)读取和写入(更新)同一文件

如何异步写入和读取节点中的同一文件?

在Java中同时读取和写入同一文件

使用try-with-resources读取和写入同一文件

使用C ++同时读取和写入同一文件

从 PowerShell 中的 Invoke-RestMethod 同时读取和写入同一文件

在同一文件中处理不同的日志格式

在 R 中处理同一文件中的不同数字格式

无法以2种单独的格式调整同一文件的大小。干预图像-Laravel

将项目添加到来自同一文件的txt输出中

PHP -l对同一文件产生不同的输出

使用tee同时输出多个命令到同一文件是否安全?

将多个命令的输出保存到同一文件

如何从bash中的不同输出保存在同一文件的两列中