FileChannel不断编写代码问题

时间:2012-06-08 14:55:26

标签: java

我有以下代码,在 j for loop out.write()只能写入 j == 0 ,那么bytesThisWritten为0。

    boolean append = true;
    FileChannel out = new FileOutputStream(dirToSave + fileName, append).getChannel();
    ByteBuffer dataBuffer = ByteBuffer.allocate(ScanDataStructConstants.NUM_DETECTORS * 4);
                dataBuffer.order(ByteOrder.LITTLE_ENDIAN);
                FloatBuffer floatBuffer = dataBuffer.asFloatBuffer();
                long bytesThisWritten = 0;
                for(int i = 0; i < ScanDataStructConstants.NUM_VIEWS_PER_ROTATION; i++)
                {
                    if(!taskWorker.isCancelled())
                    {
                        taskWorker.setTaskProgress(i); 
                        //write auxHeader
                        AuxHeader auxHeader = auxHeaderArray.getOneAuxHeader(i);                    
                        byteWritten = byteWritten + out.write(auxHeader.getJavoAuxHeader().getCmdByteBuffer()); 
                        System.out.println("DataMgr write auxHeader i=" + i + ", byteWritten=" + byteWritten);

                        //write data                               
                        for(int j = 0; j < ScanDataStructConstants.NUM_ROWS; j++)
                        {                           
                            if(!taskWorker.isCancelled())
                            {
                                float[] oneRow = scanDataArray.getOneRowDataFromAView(i, j);                           
                                floatBuffer.rewind();
                                floatBuffer.put(oneRow);
                                bytesThisWritten = out.write(dataBuffer);
                                byteWritten = byteWritten + bytesThisWritten;
                                if(i == 0)
                                {
                                    System.out.println("DataMgr write data i,j=" + i + "," + j + 
                                            ", bytesThisWritten=" + bytesThisWritten + 
                                            ", byteWritten=" + byteWritten + 
                                            " oneRow.size=" + oneRow.length + 
                                            ", oneRow[0]" + oneRow[0]);
                                }
                            }
                            else
                            {
                                status = ReturnStatusEnum.CANCELED_FAILURE;
                                break;
                            }
                        }
                    }
                    else
                    {
                        status = ReturnStatusEnum.CANCELED_FAILURE;
                        break;
                    }
                }

对于打印输出我知道源数据不为空:

DataMgr write auxHeader i=0, byteWritten=4256
DataMgr write data i,j=0,0, bytesThisWritten=3584, byteWritten=7840 oneRow.size=896, oneRow[0]-0.012269974
DataMgr write data i,j=0,1, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.012150764
DataMgr write data i,j=0,2, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.0056562424
DataMgr write data i,j=0,3, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.006008148
DataMgr write data i,j=0,4, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.0064029694
DataMgr write data i,j=0,5, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.010842323
DataMgr write data i,j=0,6, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.01802063
DataMgr write data i,j=0,7, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.0023899078
DataMgr write data i,j=0,8, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.0068216324
DataMgr write data i,j=0,9, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.005973816
DataMgr write data i,j=0,10, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.012469292
DataMgr write data i,j=0,11, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.013271332
DataMgr write data i,j=0,12, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]6.542206E-4
DataMgr write data i,j=0,13, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.008749008
DataMgr write data i,j=0,14, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.012054443
DataMgr write data i,j=0,15, bytesThisWritten=0, byteWritten=7840 oneRow.size=896, oneRow[0]-0.008696556

i for-loop 中似乎有效但只在 j循环中它只能写一次。 FileChannel处于追加模式,通道未在 i循环内关闭。我的代码出了什么问题?

1 个答案:

答案 0 :(得分:0)

j循环更改为以下内容将解决此问题。似乎FloatBuffer有一些特殊属性。

for(int j = 0; j < ScanDataStructConstants.NUM_ROWS; j++)
                    {                           
                        if(!taskWorker.isCancelled())
                        {
                            float[] oneRow = scanDataArray.getOneRowDataFromAView(i, j);                              
                            dataBuffer.rewind();
                            FloatBuffer floatBuffer = dataBuffer.asFloatBuffer();
                            floatBuffer.put(oneRow);
                            bytesThisWritten = out.write(dataBuffer);
                            bytesWritten = bytesWritten + bytesThisWritten;
                            if(i == 0)
                            {
                                System.out.println("DataMgr write data i,j=" + i + "," + j + 
                                        ", bytesThisWritten=" + bytesThisWritten + 
                                        ", byteWritten=" + bytesWritten + 
                                        " oneRow.size=" + oneRow.length + 
                                        ", oneRow[0]" + oneRow[0]);
                            }                               
                        }
                        else
                        {
                            status = ReturnStatusEnum.CANCELED_FAILURE;
                            break;
                        }
                    }
                }