且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android的:如何编写新行的CSV文件?

更新时间:2023-12-04 14:28:32

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

所以,你可以写出来与换行write.print(\ r \ N),或者干脆打开与其他一些编辑器,如 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.