statefulbeantocsv不会将完整字段写入csv,

3ks5zfa0  于 2021-06-27  发布在  Java
关注(0)|答案(0)|浏览(395)

我正在使用opencsv5.3,我正在编码 InputStreamOutputStream csv格式。在这个场景中, InputStream 包含24051条记录,同时写入24037条正确记录和24038条损坏/不完整记录。你能帮忙吗?
下面是我的代码:

private static void doCsvEncoding(Socket clientSocket, InputStream inputStream, OutputStream outputStream) {

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        try {
            byte[] byteChunk = new byte[131072];
            try {
                int readBytes = inputStream.read(byteChunk);
                int total = 0;
                while (readBytes != -1) {
                    total += readBytes;
                    byteArrayOutputStream.write(byteChunk, 0, readBytes);
                    readBytes = inputStream.read(byteChunk);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                clientSocket.shutdownInput();
            }

            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
            ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
            int cnt = 0;
            ByteArrayOutputStream mediatorOutputStream = new ByteArrayOutputStream();

            StatefulBeanToCsv<T> statefulBeanToCsv =
                    new StatefulBeanToCsvBuilder<T>(new OutputStreamWriter(mediatorOutputStream))
                            .withIgnoreField(T.class, T.class.getField("tag"))
                            .withIgnoreField(T.class, T.class.getDeclaredField("code")).build();

            try {
                while (true) {
                    T t = (T)objectInputStream.readObject();
                    statefulBeanToCsv.write(t);
                    cnt ++;
                }
            } catch (IOException | NullPointerException e) {
                e.printStackTrace();
            } catch (CsvRequiredFieldEmptyException e) {
                e.printStackTrace();
            } catch (CsvDataTypeMismatchException e) {
                e.printStackTrace();
            }
            outputStream.write(("" + cnt + " ").getBytes());
            System.out.println(cnt);
            mediatorOutputStream.close();
            ByteArrayInputStream mediatorInputStream = new ByteArrayInputStream(mediatorOutputStream.toByteArray());

            try {
                int readBytes = mediatorInputStream.read(byteChunk);
                while (readBytes != -1) {
                    outputStream.write(byteChunk, 0, readBytes);
                    readBytes = mediatorInputStream.read(byteChunk);
                }
                clientSocket.shutdownOutput();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch(IOException | ClassNotFoundException | NoSuchFieldException e) {
            e.printStackTrace();
        }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题