我想一次从文件中读取 x
个字节(例如“testFile.raw”),并将这些字节输出到另一个文件。
例如:
read 20 bytes from testFile.raw -> dump into outputfile.jpg
...repeat until done...
这可能吗?
最佳答案
这是一个使用大小为 20 的字节数组的示例:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class FileInputOutputExample {
public static void main(String[] args) throws Exception {
try{
byte[] b = new byte[20];
InputStream is = new FileInputStream("in.txt");
OutputStream os = new FileOutputStream("out.txt");
int readBytes = 0;
while ((readBytes = is.read(b)) != -1) {
os.write(b, 0, readBytes);
}
is.close();
os.close();
}catch(IOException ioe){
System.out.println("Error "+ioe.getMessage());
}
}
}
关于java - java一次从文件中读取x个字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35251767/