
So I'm doing a project for school where I need to read in a binary data file and use it to make stats, like strength and wisdom, for characters. It's set up so the first 8 bits make up one stat.
I was wondering what the actual syntax to do this is. Is it like reading text files, like this.
File file = new File("CharacterStats.dat");
Scanner inputScanner = new Scanner(file);
inputScanner.next();
解决方案
If you're using JDK 7+ the easiest way would be:
Path path = Paths.get("CharacterStats.dat");
byte[] fileContents = Files.readAllBytes(path);
And then do with that array whatever you want.
Since a byte includes 8 bits you can access the first 8 bits by fileContents[0] and then probably control the flow of your program using bitwise operations.
本文介绍如何使用Java在JDK7及以上版本中读取二进制数据文件,并利用位操作处理每8位组成的数据,适用于游戏角色统计等应用场景。
1817

被折叠的 条评论
为什么被折叠?



