2016-05-30 157 views
0

我被困在那里,我在的byte []具有逗号引号分隔值获取文件的情况,我想将其保存为CSV文件,然后读取该文件。读逗号分隔值

采样数据我收到过的byte []:

“喜”, “怎么了,是”, “你”, “是什么”, “这个”

“喜”, “你怎么样”, “你”, “是什么”, “这个”

“喜”, “怎么了,是”, “你”, “什么是”, “这个”

以下是我用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

这将有助于我认为:http://stackoverflow.com/questions/10451842/how-to-escape-comma-and-double-quote-at-same-time- for-csv-file –

+0

对于咯咯而言,制作一份csv文件的副本,然后将其打开并进行另存为。然后区分这两个文件,看看有什么不同。 –

回答

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){} 
 
     } 
 
    } 
 
    } 
 
}