Read bytes from file Java

I'm trying to parse my file which keeps all data in binary form. How to read N bytes from file with offset M? And then I need to convert it to String using new String(myByteArray, "UTF-8");. Thanks!

Here's some code:

File file = new File("my_file.txt");
byte [] myByteArray = new byte [file.lenght];

UPD 1: The answers I see are not appropriative. My file keeps strings in byte form, for example: when I put string "str" in my file it actually prints smth like [B@6e0b... in my file. Thus I need to get from this byte-code my string "str" again.

UPD 2: As it's found out the problem appears when I use toString():

    PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(System.getProperty("db.file")), true), "UTF-8")));
    Iterator it = storage.entrySet().iterator();//storage is a map<String, String>
    while (it.hasNext()){
        Map.Entry pairs = (Map.Entry)it.next();
        String K = new String(pairs.getKey().toString());
        String V = new String(pairs.getValue().toString);
        writer.println(K.length() + " " + K.getBytes() + " " + V.length() + " " + V.getBytes());//this is just the format I need to have in file
        it.remove();
    }   

May be there're some different ways to perform that?

Jon Skeet
people
quotationmark

As of Java 7, reading the whole of a file really easy - just use Files.readAllBytes(path). For example:

Path path = Paths.get("my_file.txt");
byte[] data = Files.readAllBytes(path);

If you need to do this more manually, you should use a FileInputStream - your code so far allocates an array, but doesn't read anything from the file.

To read just a portion of a file, you should look at using RandomAccessFile, which allows you to seek to wherever you want. Be aware that the read(byte[]) method does not guarantee to read all the requested data in one go, however. You should loop until either you've read everything you need, or use readFully instead. For example:

public static byte[] readPortion(File file, int offset, int length)
    throws IOException {
  byte[] data = new byte[length];
  try (RandomAccessFile raf = new RandomAccessFile(file)) {
    raf.seek(offset);
    raf.readFully(data);
  }
  return data;
}

EDIT: Your update talks about seeing text such as [B@6e0b... That suggests you're calling toString() on a byte[] at some point. Don't do that. Instead, you should use new String(data, StandardCharsets.UTF_8) or something similar - picking the appropriate encoding, of course.

people

See more on this question at Stackoverflow