Java - 尝试写入文件 2 次或更多次时出现 BufferOverflowException
Java - BufferOverflowException when trying to write to a file 2 or more times
我正在尝试编写一个程序,该程序可以一次获取 1 位,然后在 16 位 "collected" 后将 2 个字节写入文件 "collected"。
基本代码如下:
public void addBit(int bit) throws IOException{
if(this.byteholder.length() < 16){
this.byteholder += "" + bit;
}
else{
write();
}
}
public void write() throws IOException{
if(this.byteholder.length() == 16){
System.out.println(this.byteholder);
int a = Integer.parseInt(byteholder, 2);
System.out.println(Integer.toBinaryString(a));
ByteBuffer bytes = ByteBuffer.allocate(2).putInt(a);
byte[] byteArray = bytes.array();
out.write(byteArray);
out.flush();
this.byteholder = "";
}
}
public static void main(String[] args) {
try {
File f = new File("test");
BitFileWriter out = new BitFileWriter(f);
for(int i=0; i<2; i++){
out.addBit(1);
out.addBit(0);
out.addBit(0);
out.addBit(1);
out.addBit(0);
out.addBit(1);
out.addBit(1);
out.addBit(0);
out.addBit(1);
out.addBit(0);
out.addBit(0);
out.addBit(1);
out.addBit(0);
out.addBit(1);
out.addBit(1);
out.addBit(0);
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
main方法只是用来测试上面的方法。 "out" 变量是一个 FileOutputStream,"byteholder" 是一个包含二进制代码的字符串,文件 "test" 是我在 Eclipse 项目目录中的随机空文件。
我遇到的问题是,如果我 运行 我的测试代码不止一次(循环 addbit 代码不止一次),我会得到一个 BufferOverflowException 并且我不知道为什么。我不确定如何正确使用 ByteBuffer,但我需要它能够一次向文件写入 2 个字节。有人可以帮忙吗?谢谢
还有红利问题!每当我成功 运行 我的测试代码时,我可以看到我的 "test" 文件中的文件大小没有改变(仍然是 0 字节)。为什么?。我 运行宁 Windows 10 顺便说一句。
编辑:
这是我的堆栈跟踪:
Exception in thread "main" java.nio.BufferOverflowException
at java.nio.Buffer.nextPutIndex(Unknown Source)
at java.nio.HeapByteBuffer.putInt(Unknown Source)
at CompPck.BitFileWriter.write(BitFileWriter.java:30)
at CompPck.BitFileWriter.addBit(BitFileWriter.java:21)
at CompPck.BitFileWriter.main(BitFileWriter.java:66)
ByteBuffer.allocate(2).putInt(a);
您正在为 4 个字节的 int 分配 2 个字节。你期望发生什么?如果要使用 16 位值,请使用 short
(和 putShort
)。
如果由于符号问题(1001011010010110
被认为超过了 short 的值),您在使用 short 时遇到问题,您可以将 a
保留为 int
,但用 putShort((short)a)
.
写值
至于你的文件保持空白,你可能没有在你的 close()
方法中正确关闭资源,或者你忘记将 你的 缓冲区写入文件。
关于为什么您的输出文件保持为空:
您第一次调用 write()
方法是在您尝试添加第 17 位(顺便说一句,松开它)的那一刻。
如果您只进行一次迭代(即只尝试写入 16 位),则永远不会调用您的写入方法。因此 BufferOverflowException
没有出现 - 但也没有任何内容写入文件。
我正在尝试编写一个程序,该程序可以一次获取 1 位,然后在 16 位 "collected" 后将 2 个字节写入文件 "collected"。
基本代码如下:
public void addBit(int bit) throws IOException{
if(this.byteholder.length() < 16){
this.byteholder += "" + bit;
}
else{
write();
}
}
public void write() throws IOException{
if(this.byteholder.length() == 16){
System.out.println(this.byteholder);
int a = Integer.parseInt(byteholder, 2);
System.out.println(Integer.toBinaryString(a));
ByteBuffer bytes = ByteBuffer.allocate(2).putInt(a);
byte[] byteArray = bytes.array();
out.write(byteArray);
out.flush();
this.byteholder = "";
}
}
public static void main(String[] args) {
try {
File f = new File("test");
BitFileWriter out = new BitFileWriter(f);
for(int i=0; i<2; i++){
out.addBit(1);
out.addBit(0);
out.addBit(0);
out.addBit(1);
out.addBit(0);
out.addBit(1);
out.addBit(1);
out.addBit(0);
out.addBit(1);
out.addBit(0);
out.addBit(0);
out.addBit(1);
out.addBit(0);
out.addBit(1);
out.addBit(1);
out.addBit(0);
}
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
main方法只是用来测试上面的方法。 "out" 变量是一个 FileOutputStream,"byteholder" 是一个包含二进制代码的字符串,文件 "test" 是我在 Eclipse 项目目录中的随机空文件。
我遇到的问题是,如果我 运行 我的测试代码不止一次(循环 addbit 代码不止一次),我会得到一个 BufferOverflowException 并且我不知道为什么。我不确定如何正确使用 ByteBuffer,但我需要它能够一次向文件写入 2 个字节。有人可以帮忙吗?谢谢
还有红利问题!每当我成功 运行 我的测试代码时,我可以看到我的 "test" 文件中的文件大小没有改变(仍然是 0 字节)。为什么?。我 运行宁 Windows 10 顺便说一句。
编辑: 这是我的堆栈跟踪:
Exception in thread "main" java.nio.BufferOverflowException
at java.nio.Buffer.nextPutIndex(Unknown Source)
at java.nio.HeapByteBuffer.putInt(Unknown Source)
at CompPck.BitFileWriter.write(BitFileWriter.java:30)
at CompPck.BitFileWriter.addBit(BitFileWriter.java:21)
at CompPck.BitFileWriter.main(BitFileWriter.java:66)
ByteBuffer.allocate(2).putInt(a);
您正在为 4 个字节的 int 分配 2 个字节。你期望发生什么?如果要使用 16 位值,请使用 short
(和 putShort
)。
如果由于符号问题(1001011010010110
被认为超过了 short 的值),您在使用 short 时遇到问题,您可以将 a
保留为 int
,但用 putShort((short)a)
.
至于你的文件保持空白,你可能没有在你的 close()
方法中正确关闭资源,或者你忘记将 你的 缓冲区写入文件。
关于为什么您的输出文件保持为空:
您第一次调用 write()
方法是在您尝试添加第 17 位(顺便说一句,松开它)的那一刻。
如果您只进行一次迭代(即只尝试写入 16 位),则永远不会调用您的写入方法。因此 BufferOverflowException
没有出现 - 但也没有任何内容写入文件。