用引号

时间:2016-05-30 11:47:41

标签: java bytearray opencsv

我遇到的情况是我在 byte [] 中获取了一个逗号分隔值的引号,我希望将其保存为 CSV 文件,然后阅读这个文件。

我通过byte []获取的示例数据:

  

“hi”,“how,are”,“you”,“what is”,“this”

     

“hi”,“how are”,“you”,“what is”,“this”

     

“hi”,“how,are”,“you”,“what,is”,“this”

以下是我以CSV格式保存的代码。

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

以下代码阅读此CSV文件。

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

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

我面临的问题是引号中带逗号的值,奇怪的是当我打开csv文件然后用csv“另存为”并运行上面的代码来读取它时,然后CSVReader读取文件正常。

所以我认为问题在于将文件保存为csv。它没有以CSV格式正确保存文件。

有关如何以适当的CSV格式保存的任何帮助吗?

1 个答案:

答案 0 :(得分:0)

你能试试下面的代码吗? 我执行了它,它的工作正常。



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){}
      }
    }
    }
}
&#13;
&#13;
&#13;