[英]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?
如何从偏移量为M的文件中读取N个字节? And then I need to convert it to String using
new String(myByteArray, "UTF-8");
然后,我需要使用
new String(myByteArray, "UTF-8");
将其转换为String 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. UPD 1:我看到的答案不恰当。 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.
我的文件以字节形式保留字符串,例如:当我在文件中放入字符串“ str”时,它实际上在文件中打印出类似[B @ 6e0b ...]的内容。 Thus I need to get from this byte-code my string "str" again.
因此,我需要再次从该字节码中获取字符串“ str”。
UPD 2: As it's found out the problem appears when I use toString(): UPD 2:据发现,当我使用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? 可能有几种不同的方法可以执行此操作?
As of Java 7, reading the whole of a file really easy - just use Files.readAllBytes(path)
. 从Java 7开始,读取整个文件非常容易-只需使用
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. 如果您需要更手动地执行此操作,则应使用
FileInputStream
到目前为止,您的代码分配了一个数组,但未从文件中读取任何内容。
To read just a portion of a file, you should look at using RandomAccessFile
, which allows you to seek to wherever you want. 要仅读取文件的一部分 ,应该使用
RandomAccessFile
查看,它使您可以查找所需的任何位置。 Be aware that the read(byte[])
method does not guarantee to read all the requested data in one go, however. 请注意,
read(byte[])
方法不能保证一次性读取所有请求的数据。 You should loop until either you've read everything you need, or use readFully
instead. 您应该循环播放,直到您已经阅读了所需的所有内容,或者改为使用
readFully
。 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..
. 编辑:您的更新谈论看到诸如
[B@6e0b..
That suggests you're calling toString()
on a byte[]
at some point. 这表明您有时会在
byte[]
上调用toString()
。 Don't do that. 不要那样做 Instead, you should use
new String(data, StandardCharsets.UTF_8)
or something similar - picking the appropriate encoding, of course. 相反,您应该使用
new String(data, StandardCharsets.UTF_8)
或类似的名称-当然选择合适的编码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.