-1

From another question in the link below I'm reading a data file I want to read a data file word not just byte with values above 255, how do I do this? and exact offset if possible

Read Data File address (Java)

There were replys to using RandomAcessFile but I cant get it working any help?

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class ReadTest {

    public static void main(String[] args)throws IOException  {   

    byte[]     magic = new byte[4];
    File file = new File("D://test.map");
    RandomAccessFile raf = new RandomAccessFile(file );
    raf.seek(0L);
    raf.readFully(magic);
    System.out.println(new String(magic));        
    }
}

error line 13 "RandomAccessFile raf = new RandomAccessFile(file );"

slavoo
  • 5,798
  • 64
  • 37
  • 39
  • What error? Why do you have whitespace after `file`? Is the magic number actually displayable as a `String`? – Kayaman Jun 30 '17 at 11:24
  • 1
    You are trying to call a consturctor that does not exist. There is no RandomAccessFile Constructor that just takes a File as an Argument. You need to pass a mode as String as well: https://docs.oracle.com/javase/8/docs/api/java/io/RandomAccessFile.html#RandomAccessFile-java.io.File-java.lang.String- – OH GOD SPIDERS Jun 30 '17 at 11:30
  • the code is from one of the links posted as an answer in the other thread https://stackoverflow.com/questions/26790224/how-do-i-fetch-specific-bytes-from-a-file-knowing-the-offset-and-length - the error is a constructor error ??? – Mharles Canson Jun 30 '17 at 11:33
  • do you really need Random Access? maybe a DataInputStream would suffice..and the question of given link is about **fetching a specific byte**, not about reading a word.. and you could use `readInt` instead of `readFully` if you want to read an int – user85421 Jun 30 '17 at 11:34
  • Thanks for your replys, Yes my question is about both offset and word "Read Data Byte not just Word at offset" anyone know if this is possible. – Mharles Canson Jun 30 '17 at 11:39

1 Answers1

0

You cannot read a word from file directly instead of doing this you can read all data of a file and store it in string buffer and then split it on the basis of space like this

String str="Hello my name is saurabh";
           String word[] = str.split(" ");
           for(int i=0;i<word.length;i++){
             System.out.println(word[i]);
       }