- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序将文件读取到字节数组中,然后尝试从该文件中提取 bmp 图像。问题是我遇到了越界错误。
{
public static void main( String[] args )
{
FileInputStream fileInputStream=null;
File file = new File("C:/thumbcache_32.db");
byte[] bFile = new byte[(int) file.length()];
System.out.println("Byte array size: " + file.length());
try {
//convert file into array of bytes
fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();
//convert array of bytes into file
FileOutputStream fileOuputStream =
new FileOutputStream("C:/Users/zak/Desktop/thumb final/Carved_image.bmp");
fileOuputStream.write(bFile,1573278,1577427);
fileOuputStream.close();
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
}
}
}
文件加载到的字节数组的大小是“3145728”
我正在尝试将字节“1573278”复制到“1577427”。正如您所看到的,这些字节在字节数组的范围内,所以我不确定为什么会收到此错误
程序运行时的输出
Byte array size: 3145728
java.lang.IndexOutOfBoundsException
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(Unknown Source)
at Byte_copy.main(Byte_copy.java:28)
最佳答案
FileOutputStream.write需要 3 个参数,最后 2 个参数是偏移量和长度。假设我们有一个大小为 10 的数组:
byte [] arr = new byte[10];
FileOutputStream out = ...
out.write(arr, 5, 5); // writes the last 5 bytes of the file, skipping the first 5
out.write(arr, 0, 10); // writes all the bytes of the array
out.write(arr, 5, 10); // ERROR! index out of bounds,
// your attempting to write 10 bytes starting at offset 5
现在在您的代码中使用 fileOuputStream.write(bFile,1573278,1577427);
和1573278+1577427=3150705
,如您所见,3150705 > 3145728。因此,您的索引超出范围,因为您的偏移量或限制太高。我不知道你为什么选择这两个数字背后的含义,但你可以这样做。
fileOuputStream.write(bFile, 1573278, bFile.length - 1573278);
关于java - FileOutputStream.writeBytes 越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26306066/
因为 writeByte() 的目的在两个类中是相同的。但是两者写的内容不同。 import java.io.*; class First { public static void m
遇到一个小问题,我无法理解。我有一个字符串,我想将其写入文件 .txt 中的字节(用于测试)。这是我的代码: // Check writeVarString, writing in checkVarS
我如何使用 WriteByte?下面的代码将不起作用,同时出现 CS1502 错误和“stream.WriteByte(totalSeasons);”处的 CS1502 错误。线。这令人沮丧,因为我非
我的程序将文件读取到字节数组中,然后尝试从该文件中提取 bmp 图像。问题是我遇到了越界错误。 { public static void main( String[] args ) {
我有一个小型 TCP 服务器程序和一个相应的客户端,它们通过 ServerSocket 和 Socket 类以及 DataInputStream/DataOutputStream 进行通信。我在向服务
我想使用 DataOutputStream#writeBytes ,但遇到了错误。来自 Java 文档的 writeBytes(String) 的描述: Writes out the string t
我正在尝试使用“RandomAccessFile”将字符串写入(和读取)文件,但是“fio.writeBytes(file1str)”在字符串末尾添加了一些垃圾: BufferedReader in
我正在从缓冲区读取字符串并将其写入服务器。我遇到的问题是,当我打开套接字并循环写入时,服务器永远不会接收到该字符串。当我使用它时: try { Socket sen
如何使用DataInputStream的方法读取DataOutputStream.writeBytes(String)写入的数据? 我真的不知道这个问题! 最佳答案 writeBytes(String
在函数实现 (https://golang.org/src/strings/builder.go) 上方的注释中,我们看到:“返回的错误始终为零。”。这同样适用于写入字符串构建器的所有其他变体。 如果
在函数实现 (https://golang.org/src/strings/builder.go) 上方的注释中,我们看到:“返回的错误始终为零。”。这同样适用于写入字符串构建器的所有其他变体。 如果
我正在使用 ObjectOutputStream 对象操作系统将字符串消息从客户端 Android 应用程序发送到 C++ 服务器。 我知道服务器必须如何接收我的消息:消息的每个字符都存储在字节数组
我正在用 Java 编程来与连接到 com 端口的设备通信(通过 USB 连接到 PC,但通过 RS232 到 USB 电缆连接)。我编写了一个与 jssc 对话的程序,该程序在 Windows 下正
需要在dart中实现以下 MemoryStream stream = new MemoryStream(288); stream.WriteByte((byte) 13); stream.WriteB
这个问题已经有答案了: writeBytes(str) vs write(str) in DataOutputStream (2 个回答) 已关闭 4 年前。 当使用DataOutputStream推
如果我想通过套接字发送一个整数,我应该使用哪个?有什么区别? Socket socket=new Socket(localhost, 9876); DataOutputStream writer=ne
在 DataOutputStream 中,writeBytes(str) 与 write(str) 有什么区别?是否有任何使用它们的提示/技巧?提前致谢。 最佳答案 DataOutputStream
我想为我的报告创建一个 HTML 文件。报告中的内容可以通过使用 BufferedWriter#write(String) 创建 File f = new File("source.htm"); Bu
byte[] message = ... Socket socket = ... DataOutputStream dOut = new DataOutputStream(socket.getOutp
我正在使用 this 开发一个简单的客户端服务器程序关联。我的服务器是 Mac 机器,客户端是 Windows 机器 (Win 10)。 客户端-服务器通信工作正常。当我检查使用 Wireshark
我是一名优秀的程序员,十分优秀!