【问题标题】:New line character not getting written to a file using StringBuffer换行符未使用 StringBuffer 写入文件
【发布时间】:2014-01-25 03:28:47
【问题描述】:

我正在尝试解析 XML 文件并使用元素值填充字符串缓冲区。但是,在读取每个元素后,我将在StringBuffer 中添加一个换行符,如下所示。 XML 文件是从 Windows 机器上复制的,并放在 Unix 服务器上的特定位置。

public void startElement(String uri, String localName,
        String qName, Attributes attributes) throws SAXException {
    System.out.println("Start Element :" + qName);

    if (qName.equalsIgnoreCase("Constituent") && isEquityIndexPositionFile) {
        constituentContent.append(constCurrValue);
        constituentContent.append(COLUMN_SEPARATOR);
        constituentContent.append(constCurrName);
        constituentContent.append(COLUMN_SEPARATOR);
        constituentContent.append("");// mul_factor
        constituentContent.append(COLUMN_SEPARATOR);
        constituentContent.append("");// weight
        constituentContent.append(COLUMN_SEPARATOR);
        constituentContent.append(businessDate);
        constituentContent.append(COLUMN_SEPARATOR);
        constituentContent.append(currDateString);
        constituentContent.append(System.getProperty("line.separator")); 

请假设以上所有值都来自 XML 文件。这里COLUMN_SEPARATOR|

现在我想将StringBuffer constituentContent 的内容写入如下文件:

public void endDocument() throws SAXException {   
try {   
    if(constituentContent.toString().length() > 0)   
    {   
        System.out.println(constituentContent.toString());   
        fos = new FileOutputStream(RAW_DATA_FILE_LOC + CONSTITUENT_FILE, true);   
        bos = new BufferedOutputStream(fos);   
        bos.write(constituentContent.toString().getBytes());   
        fos.flush();   
        bos.flush();   
        fos.close();   
        bos.close();   
        log.info("created the index constituent  .dat file");   
    }   
} catch (Exception e) {   
    log.info("exception while creating the index constituent .dat file");   
    e.printStackTrace();   
}   
}  

System.out.println 语句使用行分隔符在控制台上打印内容,但是当我在 TextPad 中打开文件时,所有值都在一行中。我已经尝试了行分隔符的所有选项\n,\r\n,但无济于事。问题出在 Unix 操作系统上。

我还创建了这个示例程序并在 Unix 上运行。

public class Demo { 
    public static void main(String[] args) throws IOException { 
        final String seperator = System.getProperty("file.separator"); 
        final String lineSeperator = System.getProperty("line.separator"); 
        System.out.println("File Separator is"+seperator ); 
        System.out.println("Line Separator is"+ " "+lineSeperator); 
        StringBuffer headerContent=new StringBuffer(); 
        headerContent.append("1026564"); 
        headerContent.append("|"); 
        headerContent.append("1005503"); 
        headerContent.append("|"); 
        headerContent.append("391.6000"); 
        headerContent.append("|"); 
        headerContent.append("INR"); 
        headerContent.append("|"); 
        headerContent.append("|"); 
        headerContent.append("|"); 
        headerContent.append("2013-12-03"); 
        headerContent.append("|"); 
        headerContent.append("|"); 
        headerContent.append("06 Jan 2014"); 
        headerContent.append(lineSeperator); 
        headerContent.append("1026564"); 
        headerContent.append("|"); 
        headerContent.append("1005503"); 
        headerContent.append("|"); 
        headerContent.append("391.6000"); 
        headerContent.append("|"); 
        headerContent.append("INR"); 
        headerContent.append("|"); 
        headerContent.append("|"); 
        headerContent.append("|"); 
        headerContent.append("2013-12-03"); 
        headerContent.append("|"); 
        headerContent.append("|"); 
        headerContent.append("06 Jan 2014"); 
        headerContent.append(lineSeperator); 

        System.out.println(headerContent.toString()); 
        File file=new File("/opt/compliance/Atish.dat"); 
        FileOutputStream out= new FileOutputStream(file,true); 
        BufferedOutputStream bos = new BufferedOutputStream(out); 
        bos.write(headerContent.toString().getBytes()); 
        bos.flush(); 
        out.close(); 
        bos.close(); 
    } 
}

在这里它可以工作,写入文件的数据不在一行中。行分隔符在这里工作。请建议。

【问题讨论】:

  • 欢迎堆栈溢出。请尽量让您的代码更小、更切题,并更好地描述您的问题。
  • @user3164538 不客气。
  • 一个建议 - 当使用由 BufferedOutputStream 包装的 FileOutputStream 时,只为 BufferedOutputStream 实例调用 flush() 和 close() 方法就足够了。它将刷新并关闭底层 FileOutputStream。
  • 这实际上令人难以置信,值得一票。这里一定有问题。
  • 我注意到在您的测试代码中,您在 FileOutputStream 构造函数中使用了 File 对象。但是,在您的实际代码中,您直接使用文件的字符串路径。它不应该有所作为,但是,会吗?

标签: java


【解决方案1】:

尝试使用:

constituentContent.append("\r\n"); 

代替:

constituentContent.append(System.getProperty("line.separator"));

【讨论】:

  • 他似乎已经尝试了所有这些......他所有的演讲都在代码中丢失了!
【解决方案2】:

如果我正确理解了您的问题,我会将BufferedOutputStream 替换为OutputputStreamWriter + BufferedWriter,专门设置编码。最后还有一个方便的newLine方法:

OutputStreamWriter ost = new OutputStreamWriter(
     new FileOutputStream("/opt/compliance/Atish.dat", true),
     Charset.forName("UTF-8").newEncoder() 
);
BufferedWriter bw = new BufferedWriter(ost);
bw.write(constituentContent.toString());

另外,由于您没有同时执行任何操作,我想将StringBuffer 替换为StringBuilder 是安全的。

【讨论】:

  • 另外看看this question 和它的answer,他在写入最终文件时用newLine 替换\n(这样,在应用程序内部,他只能使用\n).
  • 更正:因为你没有*同时做任何事情
  • 是的,一个词可以产生很大的不同嘿嘿嘿。已修复,谢谢。
  • 这是提问者面临的问题吗?我看着代码,我不知道它有什么问题。如果他的测试代码有效,那么他的主要代码也应该有效!
  • 他的代码使用默认编码写入系统文件。这是确保它是用 UTF-8 编写的。我不知道他系统上的换行符是否设置为\n\r\n,如果它是第一个选项很好,我的代码将像魅力一样工作,如果不是,他将不得不使用策略来替换我的第一条评论建议换行。关于为什么主要方法有效而另一个无效,也许在他的原始应用程序中,默认编码是以某种方式设置的。见:stackoverflow.com/questions/361975/…
猜你喜欢
  • 2010-11-11
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 2019-10-09
  • 2017-03-23
  • 2013-12-13
相关资源
最近更新 更多