简体   繁体   English

如何让这个缓冲的编写器一次写一行? 爪哇

[英]How can I get this buffered writer to write one line at a time? JAVA

I am creating a little program that will amend driver letters saved in a program file. 我正在创建一个小程序,它将修改保存在程序文件中的驱动程序字母。 The method works in the sense that it locates the file correctly and amends the drive letter correctly. 从某种意义上说,该方法可以正确定位文件并正确修改驱动器号。

However when I open the file after it has been changed all the Directory listings are all on one line but they need to be 1 directory per line. 但是,当我在更改文件后打开文件时,所有目录列表都位于一行上,但是每行必须为1个目录。 I've tried saving each individual line to an Array List then printing them out like that but that didn't seem to work for me so was wondering if anyone could help? 我试过将每一行保存到一个数组列表中,然后像这样打印出来,但是这似乎对我不起作用,所以想知道是否有人可以帮忙?

Much appreciated. 非常感激。

PS Been messing around trying to make it work and also now ran into another issue where it is now printing them all out into one line but with spaces in between eg: PS一直在四处寻找使其工作的麻烦,现在又遇到了另一个问题,即现在将它们全部打印到一行中,但它们之间有空格,例如:

S:\\ DATA\\ SAGE\\ S:\\ DATA \\ SAGE \\

public class copy
{

private String newDriveLetter;
private String line;
private Path[]sageFolders;
private FileReader fileReader;
private BufferedReader bufferedReader;
private FileWriter fileWriter;
private BufferedWriter bufferedWriter;
private List<String> lines = new ArrayList<String>();



    public void scanFiles() throws IOException{

       try
       {
        System.out.println("Sage 2015 is Installed on this machine");
        File companyFile = new File(sageFolders[8] + "\\COMPANY");
        fileReader = new FileReader(companyFile);
        bufferedReader = new BufferedReader(fileReader); 

        while((line = bufferedReader.readLine()) != null)
        {
           if(line.contains("F"))
           {
               line = line.replace("F:\\","S:\\");
               lines.add(line);

           }

        }
        //Close the Readers
        fileReader.close();
        bufferedReader.close();

        fileWriter = new FileWriter(companyFile);
        bufferedWriter = new BufferedWriter(fileWriter);

        for(String s : lines)
        {
            bufferedWriter.write(s);
        }

        bufferedWriter.flush();
        bufferedWriter.close();

       }
       catch(FileNotFoundException e)
       {
          System.out.println("File not Found: Moving onto next Version"); 
       }

}

尝试使用PrintWriter- new PrintWriter(bufferedWriter).println(s)

The problem is that readLine() reads a whole line, and then tosses away the line ending. 问题是readLine()读取整行,然后扔掉行尾。 From the documentation: 从文档中:

A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached 一个字符串,其中包含行的内容,不包含任何行终止符;如果已到达流的末尾,则为null

So you have to add it back again, possibly using the preferred line ending for your platform: 因此,您必须再次添加回去,可能使用平台的首选行结尾:

bufferedWriter.write(String.format("%s%n", s);

here %s is the String and %n is the platform dependent line ending. 这里%sString%n是平台相关的行结尾。

Alternatively, as Ransom Briggs indicates, you may use newline() . 另外,正如Ransom Briggs指出的那样,您可以使用newline() newLine() is easier to read and will perform slightly better: newLine()更易于阅读,并且性能会稍好一些:

bufferedWriter.write(s);
bufferedWriter.newLine();

I've left the String.format method in as it is a more general approach of adding newlines to strings. 我保留了String.format方法,因为它是一种向字符串添加换行符的更通用的方法。

Add this after bufferedWriter.write(s);: 在bufferedWriter.write(s);之后添加:

bufferedWriter.write(System.getProperty("line.separator", "\n"));

This will add the system specific line separator, or "\\n" if the system property is not set. 这将添加系统特定的行分隔符,如果未设置系统属性,则添加“ \\ n”。

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

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