This discussion is archived
5 Replies Latest reply: May 14, 2012 6:17 AM by EJP RSS

Java Random access write using File Channel.

881651 Newbie
Currently Being Moderated
I read from a file , write to a random access file. Writing it piece by piece, While writing if I gave position as sequential I'm getting same file as out put (correct), but If I gave random position then file is not as I expected , that is not writing to exact position.

Pls find attached sample program and check with a 3000 Byte file. excuse with some hard coding and not straight logic, which is needed in my real situation.
       private void readFiles()throws FileNotFoundException,IOException {
               
                String sourceFilename = "D:/abc.txt";
                String detFileName = "D:/out/edf.txt";
                
                FileInputStream fileInStream = new FileInputStream(sourceFilename);
                DataInputStream dInStream = new DataInputStream(fileInStream);
                RandomAccessFile writer = new RandomAccessFile(detFileName, "rw");
                FileChannel fco = writer.getChannel();
                         
                if(fileInStream.available()%8 !=0){
                     System.out.println("Please use a 3000KB file for testing");
                     return;
                }
                
                long nSamples = fileInStream.available()/8;   // Total File length reading 8 bytes
                      System.out.println("Available length "+fileInStream.available()+"nsamples"+nSamples); 
                      
                      
                     long startTime = System.currentTimeMillis();
                    
                     //DataOutputStream dos = new DataOutputStream(writer);
                    
                    int divNum = 1;
                    
                    for (int i = 3;i<9;i++){
                         if(nSamples%i==0){
                              divNum = i;
                              break;
                         }
                    }
                    System.out.println("divNum "+divNum); 
                    /// Please look the following ..., here I gave order as 0,1,2 If I change the order to 2,1,0 file corrupts, any non sequential order fails. every time file writes sequentially.
                   
                    {
                         int order =0;
                         dataReadWriteLogic(dInStream , fco,(order*nSamples/divNum) ,  (((order+1)*nSamples)/divNum));
                         order =1;
                          dataReadWriteLogic(dInStream , fco,(order*nSamples/divNum) ,  (((order+1)*nSamples)/divNum));
                          order=2;
                          dataReadWriteLogic(dInStream , fco,(order*nSamples/divNum) ,  (((order+1)*nSamples)/divNum));
                    }
                    writer.close();
                          dInStream.close();
                          fileInStream.close();
                          System.out.println("Finishing time diff: is "+(System.currentTimeMillis() - startTime)/1000+":Seconds"); 
                      
          }
          
          private void dataReadWriteLogic(DataInputStream din, FileChannel fco,long startLoc, long endLoc) throws IOException{
                System.out.println( startLoc +":"+endLoc);
                ByteBuffer buffer = ByteBuffer.allocate(1024*1024*4); // 4MB
              
                long writePos =startLoc*8; 
             float dataNibble = 0;
             float validityBit=0;
             for (long sample = startLoc; sample < endLoc; sample++)
             {
                 
                  dataNibble = din.readFloat();
                  buffer.putFloat(dataNibble);
                  validityBit = din.readFloat();
                  buffer.putFloat(validityBit);
             }
             
             writeFile(buffer,fco,writePos);
            System.out.println( din.available());
          }
          
          private void writeFile(ByteBuffer buffer, FileChannel fco, long position ) throws IOException{
               
               buffer.flip();
               System.out.println("position"+position+" buffer length"+buffer.remaining());
            fco.position(position);
            fco.write(buffer);
          }
// Please help with suggestions.

Edited by: EJP on 10/05/2012 15:47: added {noformat}
{noformat} tags: please use them.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 2. Re: Java Random access write using File Channel.
    881651 Newbie
    Currently Being Moderated
    Solved the issue as I dentified Input file also want to read as random, so need to use Randomaccessfile for reading file too...,
  • 3. Re: Java Random access write using File Channel.
    EJP Guru
    Currently Being Moderated
    In other words, to be clear, everything you have posted both here and in the other place was incorrect. There was no 'file not as expected', no 'not writing to exact position', no problem doing a 'random access write using a FileChannel', nothing, nada, nil, nulla, niente, zero, zip. The problem was elsewhere, as suggested.

    And re your complaint in the other place about my crosspost notice in this place, (a) you put it in the wrong place, (b) in the nature of things you don't get to ask the questions and make the rules about how they are answered: people will have their own rules about that, and they may well include not having their time wasted by crossposting, especially if they read both places, as I do, and (c) having already wasted the time of upwards of a dozen people with an imaginary problem, including those who like me actually kindly and voluntarily compiled and ran your code to prove that, you are hardly in a position to complain about the measures we take to limit the ways in which our time can be wasted.
  • 4. Re: Java Random access write using File Channel.
    881651 Newbie
    Currently Being Moderated
    @EJP,

    I have given a test program, that is easy runnable , some one Identified issue just going through the code. helped me with the solution. some people will complain every time like formating is not good, spelling mistake there , why used readFloat etc..., It's not easy to convince those people until they use some common sense. Anyway I got answer, I'm thankful as used your to look my issue, but your cross questions are not suitable to me, that's all.
  • 5. Re: Java Random access write using File Channel.
    EJP Guru
    Currently Being Moderated
    It is your meaningless questions about non-existent problems that are not suitable, at least for this forum, or SO either. You don't appear to have taken in anything that was said in either place.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points