如何编写新行的CSV文件

如何编写新行的CSV文件

本文介绍了Android的:如何编写新行的CSV文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想安排我在.CSV file.I数据想把我的数据在特定的行,所以我试图把\ N,但它不是working.Please帮我把数据传输到特定的行。谢谢你在前进。

 公共无效writeData(字符串数据,字符串strFilePath)
        {

            PrintWriter的csvWriter;
            尝试
            {

                档案文件=新的文件(strFilePath);
                如果(!file.exists()){
                    文件=新的文件(strFilePath);
                }
                csvWriter =的新PrintWriter(新的FileWriter(文件,真));


                csvWriter.print(数据+,+你好);
                csvWriter.append('\ N');
                csvWriter.print(世​​界);


                csvWriter.close();


            }
            赶上(例外五)
            {
                e.printStackTrace();
            }
        }
 

解决方案

您code看起来不错,而且我相信换行正确写入文件。我能想到的唯一原因是,你有没有把编辑器中打开该文件作为行分隔符,它把 \ r \ñ对作为一个行分隔符(如记事本在Windows上)。

所以,你可以写出来与换行write.print(\ r \ N),或者干脆打开与其他一些编辑器,如<$ C $文件C> VIM 。不,你用什么编辑器打开该文件的事情,换行字符是存在的。

I am trying to organize my data in .CSV file.I want to put my data in a particular row so i tried putting "\n" but it is not working.Please help me in putting data to a particular row.Thank you in advance..

public void writeData(String data,String strFilePath)
        {

            PrintWriter csvWriter;
            try
            {

                File file = new File(strFilePath);
                if(!file.exists()){
                    file = new File(strFilePath);
                }
                csvWriter = new  PrintWriter(new FileWriter(file,true));


                csvWriter.print(data+","+"hello");
                csvWriter.append('\n');
                csvWriter.print("world");


                csvWriter.close();


            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
解决方案

You code looks good, and I am sure the newline is written correctly to the file. The only reason that I can think of is that you opened the file with an editor that does not treat \n as a line separator, it treats \r\n pair as a line separator(like Notepad on Windows).

So you can either write the newline with write.print("\r\n"), or simply open the file with some other editors like vim. No matter what editor you use to open the file, the newline character is there.

这篇关于Android的:如何编写新行的CSV文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 18:15