0

I am stuck in a situation where I am getting a file in byte[] which have comma separated values in quotes, I want to save it as CSV file and then read this file.

Sample data I am getting through byte[]:

"hi","how,are","you","what is","this"

"hi","how are","you","what is","this"

"hi","how,are","you","what, is","this"

Following is my code for saving it in CSV format.

byte[] bytes = myByteStream.getFile();
OutputStream out22 = new FileOutputStream("path/to/file/dummy.csv");
out22.write(bytes);
out22.close();

And the following code to read this CSV file.

  CSVReader reader = new CSVReader(new FileReader("path/to/file/dummy.csv"), ',');                
  String[] nextLine;

  while ((nextLine = reader.readNext()) != null)
  {
    System.out.println(nextLine[1]);
  }

The problem I am facing is with the values that have comma in quotes, and strange thing is when I open that csv file and then "Save As" with csv and run the above code to read it, then CSVReader reads the files properly.

So I think that problem is in saving the file as csv. It is not properly saving the file in CSV format.

Any help, on how to save it in proper CSV format ?

pro_newbie
  • 336
  • 2
  • 6
  • 19
  • 1
    This will help i think: http://stackoverflow.com/questions/10451842/how-to-escape-comma-and-double-quote-at-same-time-for-csv-file – kurt_vonnegut May 30 '16 at 11:55
  • For giggles make a copy of the csv file before you open it up and do a save as. Then diff the two files and see what is different. – Scott Conway May 31 '16 at 19:40

1 Answers1

0

Can you try below code. I executed it and its works fine.

package stackoverflow;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MainJava{
     /** 
     * Read bytes from a File into a byte[].
     * 
     * @param file The File to read.
     * @return A byte[] containing the contents of the File.
     * @throws IOException Thrown if the File is too long to read or couldn't be
     * read fully.
     */
    public static byte[] readBytesFromFile(File file) throws IOException {
      InputStream is = new FileInputStream(file);
      
      // Get the size of the file
      long length = file.length();
  
      // You cannot create an array using a long type.
      // It needs to be an int type.
      // Before converting to an int type, check
      // to ensure that file is not larger than Integer.MAX_VALUE.
      if (length > Integer.MAX_VALUE) {
        throw new IOException("Could not completely read file " + file.getName() + " as it is too long (" + length + " bytes, max supported " + Integer.MAX_VALUE + ")");
      }
  
      // Create the byte array to hold the data
      byte[] bytes = new byte[(int)length];
  
      // Read in the bytes
      int offset = 0;
      int numRead = 0;
      while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
          offset += numRead;
      }
  
      // Ensure all the bytes have been read in
      if (offset < bytes.length) {
          throw new IOException("Could not completely read file " + file.getName());
      }
  
      // Close the input stream and return bytes
      is.close();
      return bytes;
  }
    
    /**
     * Writes the specified byte[] to the specified File path.
     * 
     * @param theFile File Object representing the path to write to.
     * @param bytes The byte[] of data to write to the File.
     * @throws IOException Thrown if there is problem creating or writing the 
     * File.
     */
    public static void writeBytesToFile(File theFile, byte[] bytes) throws IOException {
      BufferedOutputStream bos = null;
      
    try {
      FileOutputStream fos = new FileOutputStream(theFile);
      bos = new BufferedOutputStream(fos); 
      bos.write(bytes);
    }finally {
      if(bos != null) {
        try  {
          //flush and close the BufferedOutputStream
          bos.flush();
          bos.close();
        } catch(Exception e){}
      }
    }
    }
}