-1

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?

Maxim Gotovchits
  • 729
  • 3
  • 11
  • 22
  • possible duplicate of [File to byte\[\] in Java](http://stackoverflow.com/questions/858980/file-to-byte-in-java) – Alboz Oct 07 '14 at 20:56
  • Search "Java read binary file" and you should see plenty of examples. Also look into "Java file seek". – Florian F Oct 07 '14 at 20:57
  • `[B@6e0b...` is not String written as bytes, it is result of `toString` from byte array, which includes `[B` - byte array; `@` separator; `6e0b...` hexadecimal representation of hashcode of array. Can we see code you used to write string to file? – Pshemo Oct 07 '14 at 21:03
  • 1
    @Pshemo But how to get it as real bytes to solve my problem? – Maxim Gotovchits Oct 07 '14 at 21:05
  • You need to correctly write your sting to file first to be able to read it. Consider using something like `PrintWriter out = new PrintWriter("filename.txt"); out.println(yourString);` You will also need to `out.close()` this writer. – Pshemo Oct 07 '14 at 21:08
  • I edited some of your previous question to remove Java from title because it is not needed there. On Stack Overflow there tag system, marking your question with [java] tag is enough to tell us which language you are interested in. – Pshemo Oct 07 '14 at 21:42

1 Answers1

9

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • That doesn't cover the "N bytes at offset M" part. – Florian F Oct 07 '14 at 20:58
  • @FlorianF: Gah, so it doesn't - I missed that. – Jon Skeet Oct 07 '14 at 20:59
  • @MaximGotovchits: Your update doesn't show what it thinks it does - all it shows is that calling `toString()` on a `byte[]` doesn't do what you want it to. You've already *got* the code to convert a `byte[]` into a `String` using an appropriate encoding... – Jon Skeet Oct 07 '14 at 21:03
  • @JonSkeet Yes, I call toString() when I use `new String(pairs.getKey().toString());` But `pairs.getKey()` doesn't work without toString =( – Maxim Gotovchits Oct 07 '14 at 21:17
  • @MaximGotovchits: "doesn't work" doesn't tell us anything about what's wrong, but I've explained in my answer how to convert a byte array into a string using a particular encoding. If you want anything else, I suggest you ask a separate question after researching others. (It really is a different question from "how do I read a portion of a file into a `byte[]`.) – Jon Skeet Oct 07 '14 at 21:20
  • @JonSkeet Yes, I'm sorry for that. Looks like that the main problem was hidden a little bit. So I've updated the question. – Maxim Gotovchits Oct 07 '14 at 21:30
  • 2
    @MaximGotovchits: No, *don't* keep updating the question to drag it further and further away from the original question... especially when I've already provided you with the solution to the new problem anyway. (Don't call `toString()`, call `new String(bytes, charset)`.) If you *must* ask for more information, do so in a new question. – Jon Skeet Oct 07 '14 at 21:33