8 Replies Latest reply: Aug 4, 2010 5:50 PM by EJP RSS

    read from RandomAccessFile

    843790
      Hello,

      I have a situation that needs to be done.. I can not do it my self. Today I first tried RandomAccessFile and it is difficult for me to understand, so sorry for stupid questions.

      I have created a method:
          public void readRAF(String inputFile, String word){
              try {
                  RandomAccessFile raf = new RandomAccessFile(inputFile, "rw");
                  raf.seek(0);
      
                  while (raf.getFilePointer() < raf.length()) {
                      String line = raf.readUTF();
                      if (line.contains(" " + word + " ")){
                          raf.seek(line.indexOf(word) - 20);
                          System.out.println("?????????") //??????????
                      }
                  }
              } catch (Exception ex) {
                  Logger.getLogger(XMLToRAFConverter.class.getName()).log(Level.SEVERE, null, ex);
              }
          }
      It gives me error on this line:
      String line = raf.readUTF();
       java.io.EOFException
              at java.io.RandomAccessFile.readFully(RandomAccessFile.java:383)
              at java.io.DataInputStream.readUTF(DataInputStream.java:592)
              at java.io.RandomAccessFile.readUTF(RandomAccessFile.java:887)
              at com.web.search.crawler.app.XMLToRAFConverter.readRAF(XMLToRAFConverter.java:60)
              at com.web.search.crawler.app.XMLToRAFConverter.main(XMLToRAFConverter.java:75)
      How can I print the result of that seek raf.seek(line.indexOf(word) - 20); ?

      Please help....
        • 1. Re: read from RandomAccessFile
          843790
          Unless your file was written with writeUTF(), it is highly unlikely that readUTF() will work. I suggest you read the javadoc for what this method does. i.e. it will NOT work on a plain text file.
          • 2. Re: read from RandomAccessFile
            843790
            Fo e.g I have file with ten lines in it.

            I read those lines to ArraList.

            Then in RandomAccessFile I write those lines like this:
                public void writeRAF(){
                    try {
                        RandomAccessFile raf = new RandomAccessFile("/home/paulius/Desktop/Fi", "rw");
                        for (String line : listOfLines){
                            raf.write(line.getBytes());
                        }
                        raf.close();
                    } catch (Exception ex) {
                        Logger.getLogger(XMLToRAFConverter.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            am I right?
            • 3. Re: read from RandomAccessFile
              843790
              I suggest you read my reply again. I also suggest you read what readUTF() and writeUTF() actually does.
              • 4. Re: read from RandomAccessFile
                843790
                I did. I don't understand...
                So, please if you know how to solve my problem, tell me...
                • 5. Re: read from RandomAccessFile
                  843790
                  The simplest way to read text using readUTF() is to write the text using writeUTF().
                  • 6. Re: read from RandomAccessFile
                    843790
                    Its not clear to me why you are using a RandomAccessFile. This is a fairly advanced class and I would suggest you understand the basics before you try using it. Instead I suggest you use PrintStream and BufferedReader. There are plenty of examples on the web.

                    These are designed for writing/reading lines of text which appears to be your requirement.
                    • 7. Re: read from RandomAccessFile
                      843790
                      I need the fast reading,

                      Because I'm reading 3gb text files every time user send request, so I need word to be indexed.
                      • 8. Re: read from RandomAccessFile
                        EJP
                        But, as you've already been told several times, if the file wasn't written with writeUTF() you can't read it with readUTF(). Period.
                        I need the fast reading
                        But what makes you think RandomAccessFile gives you the fastest reading? It doesn't. You're not even doing random I/O, you're doing sequential I/O. For that you should use BufferedReader.readLine(). But: ...
                        Because I'm reading 3gb text files every time user send request
                        Then there is something seriously wrong with your design. This is absolute madness. You should completely redesign this to use a database.
                        so I need word to be indexed.
                        All that means in your code is String.indexOf(): in other words you just need a String. There's nothing about any of this that requires a RandomAccessFile.

                        Use a database.