10

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();
            }
        }
swetha kini
  • 564
  • 1
  • 6
  • 23
  • 1
    What do you mean by "it is not working"? What exactly do you see? Being specific when asking questions will give you the answers you need. – Dheeraj Vepakomma Sep 03 '12 at 05:06
  • 1
    For example if "first,second\nthird" is added into csv, only "first" and "second" is visible..we cannot view "third" in csv file.I hope this is the problem with android editors.suppose if I transfer my .csv into windows PC, even "third" is visible in new row. – swetha kini Sep 03 '12 at 05:35

5 Answers5

8

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.

neevek
  • 11,760
  • 8
  • 55
  • 73
  • I am trying to open with excel..its not working as u said...please guide me through good editors which can show up "\n" – swetha kini Sep 03 '12 at 05:16
  • Have you tried opening the file from Excel by `File -> Import -> CSV File`. You should not directly open the file with Excel, which may not be able to detect that it is a CSV file. For editors, `notepad++`, or commercial ones like `EditPlus` or `UltraEdit` will treat `\n` as a line separator. – neevek Sep 03 '12 at 05:34
  • Finally it worked for me with csvWriter.print("\r\n").@Neevek Thank you – swetha kini Sep 13 '12 at 10:04
2

CSV files are not as easy to create as it may seem. I ran into a lot of issues when trying to write a quick CSV parser/writer.

Just use OpenCSV and solve all your problems instantly.

Using OpenCSV you can do something like this:

CSVWriter writer = new CSVWriter(new FileWriter("yourfile.csv"), '\t');
// feed in your array (or convert your data to an array)
String[] entries = "first#second#third".split("#");
writer.writeNext(entries);
you786
  • 3,659
  • 5
  • 48
  • 74
2
  1. try flush your stream before you close it csvWriter.flush()
  2. instead using print, try println
user902383
  • 8,420
  • 8
  • 43
  • 63
1

Try the following code (using StringBuffer) - a thread-safe, mutable sequence of characters:

    public void writeData(String data, String strFilePath) {

    PrintWriter csvWriter;
    try {
        /*1. declare stringBuffer*/
        StringBuffer oneLineStringBuffer = new StringBuffer();

        File file = new File(strFilePath);
        if (!file.exists()) {
            file = new File(strFilePath);

        }
        csvWriter = new PrintWriter(new FileWriter(file, true));

        /*2. append to stringBuffer*/   
        oneLineStringBuffer.append(data + "," + "hello");
        oneLineStringBuffer.append("\n");
        oneLineStringBuffer.append("world");

        /*3. print to csvWriter*/
        csvWriter.print(oneLineStringBuffer);

        csvWriter.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
prayagupa
  • 30,204
  • 14
  • 155
  • 192
1

You just have to change csvWriter.print("world"); to csvWriter.println("world");. The next print going to be in next new line.

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.println(data+","+"hello");
                    csvWriter.print("world");


                    csvWriter.close();


                }
                catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
jamalM
  • 57
  • 1
  • 9