在selectUTF-8编码之后,excel文件不会生成

我的代码的这一部分成功创buildxls文件

FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls"); wb.write(fileOut); fileOut.close(); 

当代码的其他部分有这个声明(这是在上面的代码之前)

 in = new ByteArrayInputStream(theCell_00.getBytes("")); 

但是当我改变它

 in = new ByteArrayInputStream(theCell_00.getBytes("UTF-8")); 

这部分

 FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls"); wb.write(fileOut); fileOut.close(); 

不再生成任何xls文件。

我需要将编码更改为UTF-8,正如我在ByteArrayInputStream行中所做的那样,那么我应该怎么做,代码仍然会生成xls文件。

如果你需要,这两个部分是从这个function。

 public void getExcel() throws Exception { try { ByteArrayInputStream in = null; FileOutputStream out = null; HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("new sheet"); /* * KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey key = * kgen.generateKey(); byte[] encoded = key.getEncoded(); * * IOUtils.write(encoded, new FileOutputStream(new * File("C:\\Users\\abc\\Desktop\\key.txt"))); */ FileInputStream fin = new FileInputStream("C:\\key.txt"); DataInputStream din = new DataInputStream(fin); byte b[] = new byte[16]; din.read(b); InputStream excelResource = new FileInputStream(path); Workbook rwb = Workbook.getWorkbook(excelResource); int sheetCount = rwb.getNumberOfSheets(); Sheet rs = rwb.getSheet(0); int rows = rs.getRows(); int cols = rs.getColumns(); for (int i = 0; i < rows; i++) { for (int j = 0; j < Col.length; j++) { String theCell_00 = rs.getCell(j, i).getContents(); System.out.println("the Cell Content : " + theCell_00); in = new ByteArrayInputStream(theCell_00.getBytes("")); out = new FileOutputStream("c:\\Decrypted.txt"); try { // System.out.println(b); SecretKey key1 = new SecretKeySpec(b, "AES"); // Create encrypter/decrypter class AESDecrypter encrypter = new AESDecrypter(key1); encrypter.encrypt(new ByteArrayInputStream(theCell_00.getBytes()), new FileOutputStream("temp.txt")); // Decrypt // encrypter.encrypt(,new FileOutputStream("Encrypted.txt")); encrypter.decrypt(in, out); try { if (out != null) out.close(); } finally { if (in != null) in.close(); } // encrypter.decrypt(new // ByteArrayInputStream(theCell_00.getBytes(Latin_1)),new // FileOutputStream("c:\\Decrypted.txt")); String filename = "c:\\Decrypted.txt"; BufferedReader bufferedReader = null; try { // Construct the BufferedReader object bufferedReader = new BufferedReader(new FileReader(filename)); // System.out.println(bufferedReader.readLine()); String line = null; while ((line = bufferedReader.readLine()) != null) { // Process the data, here we just print it out /* * HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = * wb.createSheet("new sheet"); HSSFRow row = sheet.createRow(2); */ // System.out.println(i); HSSFRow row = sheet.createRow(i); int s_col = 0; row.createCell(s_col).setCellValue(line); // s_col++; // row.createCell(1).setCellValue(new Date()); FileOutputStream fileOut = new FileOutputStream("c:\\Decrypted.xls"); wb.write(fileOut); fileOut.close(); // System.out.println(line); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { // Close the BufferedReader try { if (bufferedReader != null) bufferedReader.close(); } catch (IOException ex) { ex.printStackTrace(); } } } catch (Exception e) { e.printStackTrace(); } } } rwb.close(); } catch (Exception ex) { ex.printStackTrace(); ex.getMessage(); } } 

AESDecrypter.decrypt调用期望的数据types是什么? 它是否需要一个FileOutputStream对象? 或者你可以通过一个Writer或其他OutputStream?

我通常做这样的事情来写UTF-8输出:

 Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("c:\\Decrypted.txt"), "UTF-8"));