「java写二进制文件」java编译二进制文件

博主:adminadmin 2022-12-03 11:27:06 64

本篇文章给大家谈谈java写二进制文件,以及java编译二进制文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java里怎样把文件转换成二进制?

转换文件成为二进制数据并保存的Java代码:

取出数据并还原文件到本地的java代码:

[java] view plain copy//读取数据库二进制文件

public void readerJpg() throws SQLException

{

connection=connectionManager.getconn();//自己连接自己的数据库

String sqlString="select images from save_image where id=4";//从数据库中读出要还原文件的二进制码,这里我读的是自己的数据库id为4的文件

File file=new File("E:\\1.jpg");//本地生成的文件

if(!file.exists())

{

try {

file.createNewFile();

} catch (Exception e) {

e.printStackTrace();

}

}

try {

byte[] Buffer = new byte[4096*5];

statement=connection.prepareStatement(sqlString);

resultSet = statement.executeQuery();

if(resultSet.next())

{

FileOutputStream outputStream = new FileOutputStream(file);

InputStream iStream = resultSet.getBinaryStream("images");//去字段用getBinaryStream()

int size=0;

while((size=iStream.read(Buffer))!=-1)

{

System.out.println(size);

outputStream.write(Buffer,0,size);

}

}

} catch (Exception e) {

e.printStackTrace();

}

}

Java中如何生成二进制文件?比如我需要写入0x123456 但是如果使用bufferedwriter的write() 只能写入int...

java里对文件流分为字节流和字符流。

1,FileOutputStream:按字节写入文件

2.1,导入包

import java.io.*;

2.2,声明一个OutputStream引用

OutputStream out =null;

2.3,构造一个OutputStream对象,并在其中写入内容

try {

out = new FileOutputStream("b.txt");

String str ="java终于完了";

byte[] b = str.getBytes();

try {

out.write(b, 0, b.length);

}

catch (IOException ex1) {

ex1.printStackTrace();

}

2.4,关闭对象

finally{

try {

out.close();

}

catch (IOException ex2) {

ex2.printStackTrace();

}

}

望采纳!

怎样用Java读写二进制文件

import java.util.*;

import java.io.*;

class SmallFile {

static final int HEADLEN = 24; //头总长度

byte[] fileName = new byte[16]; //列表文件名1: 长度128 想把它读到char[]里 它的编码方式不是Unicode。在不确定编码方式的时候,最好直接用byte[]来存放

int offset; //列表文件地址1: 长度32 想把它读到int里

int length = -1; //列表文件长度1: 长度32 想把它读到int里

byte[] content;

public SmallFile() {

}

public SmallFile(byte[] fn, byte[] content) {

Arrays.fill(fileName, (byte) 0);

if (fn != null) {

if (fn.length = 16) {

System.arraycopy(fn, 0, fileName, 0, fn.length);

}

else {

System.arraycopy(fn, 0, fileName, 0, 16);

}

}

this.content = content;

if (content != null) {

this.length = content.length;

}

else {

this.length = -1;

}

}

}

public class ReadBinary {

static final int HEADLEN = 8; //头总长度

private String filename;

private byte[] filehead = new byte[4]; //文件头: 长度32 想把它读到char[]里 它的编码方式不是Unicode

private int filecount = -1; //列表长度: 长度32 想把它读到int里 假设他是3 就会有3个列表文件名

private ListSmallFile files = null;

public void setFilehead(byte[] fh) {

if (fh == null)

return;

Arrays.fill(filehead, (byte) 0);

if (fh.length = 4) {

System.arraycopy(fh, 0, filehead, 0, fh.length);

}

else {

System.arraycopy(fh, 0, filehead, 0, 4);

}

}

public ReadBinary(String filename) {

try {

readFromFile(filename);

}

catch (Exception ex) {

System.out.println(ex.getMessage());

System.out.println("在载入数据文件时失败,因此视同为新建一个数据文件!");

this.filename = filename;

Arrays.fill(filehead, (byte) 0);

filecount = 0;

files = new ArrayListSmallFile ();

}

}

public void readFromFile(String filename) throws Exception {

BufferedInputStream bin = new BufferedInputStream(new FileInputStream(

filename));

this.filename = filename;

DataInputStream in = new DataInputStream(bin);

in.read(filehead); //文件头: 长度32 想把它读到char[]里 它的编码方式不是Unicode

filecount = in.readInt(); //列表长度: 长度32 想把它读到int里 假设他是3 就会有3个列表文件名

if (files == null) {

files = new ArrayListSmallFile ();

}

else {

files.clear();

}

for (int i = 0; i filecount; i++) {

SmallFile file = new SmallFile();

in.read(file.fileName);

file.offset = in.readInt(); //列表文件地址1: 长度32 想把它读到int里

file.length = in.readInt(); //列表文件长度1: 长度32 想把它读到int里

files.add(file);

}

}

public void writeToFile() throws Exception {

String temp = filename + ".tmp"; //临时文件

boolean exists = false;

RandomAccessFile raf = null;

try {

raf = new RandomAccessFile(filename, "r"); //文件存在则从文件读入

exists = true;

}

catch (Exception ex) {

System.out.println("文件不存在,因此启用内存写入模式");

}

if (filecount != files.size()) {

throw new Exception("怪事,居然不相同??");

}

DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new

FileOutputStream(temp)));

//1、写总文件头

out.write(filehead);

out.writeInt(filecount);

//2、写列表头

int sumlength = 0;

for (int i = 0; i files.size(); i++) {

SmallFile file = files.get(i);

out.write(file.fileName);

if (file.length 0) {

throw new Exception("怪事,文件长度怎么可能小于0?");

}

else {

out.writeInt(ReadBinary.HEADLEN + SmallFile.HEADLEN * filecount +

sumlength);

sumlength += file.length;

out.writeInt(file.length);

}

}

//3、写文件内容

for (int i = 0; i files.size(); i++) {

SmallFile file = files.get(i);

if (file.content != null (file.length == file.content.length)) {

out.write(file.content);

}

else if (exists) {

raf.seek(file.offset);

byte[] b = new byte[file.length];

raf.read(b);

System.out.println("b:" + new String(b));

out.write(b);

}

else {

throw new Exception("怪事,又不能从内存读,又不能从文件读。这活没法干了!");

}

}

out.close();

if (raf != null) {

raf.close();

raf = null;

}

System.gc();

//把原先的文件删除

File f = new File(filename);

f.delete();

//再把临时文件改名到正式文件

File f2 = new File(temp);

f2.renameTo(f);

}

public void addFile(SmallFile file) {

if (files != null) {

filecount++;

files.add(file);

}

}

public static void test1(){

ReadBinary rb = new ReadBinary("f:\\temp\\rb.dat");

rb.setFilehead("HEAD1234567890122222222222222222".getBytes());

SmallFile f = new SmallFile("第1个文件".getBytes(), "第1个文件的内容".getBytes());

rb.addFile(f);

try {

rb.writeToFile();

}

catch (Exception ex) {

ex.printStackTrace();

}

}

public static void test2(){

ReadBinary rb = new ReadBinary("f:\\temp\\rb.dat");

rb.setFilehead("HEA".getBytes());

SmallFile f = new SmallFile("第2个文件".getBytes(), "第2个文件的内容".getBytes());

rb.addFile(f);

try {

rb.writeToFile();

}

catch (Exception ex) {

ex.printStackTrace();

}

}

public static void main(String[] args) {

//test1();

test2();

}

}

跪求“java中二进制怎么表示”

java中二进制的表示:

1、Java中定义两个数,然后分别打印出它们的二进制表示(例如7和-7):

System.out.println("Java二进制7: "+Integer.toBinaryString(7));

System.out.println("Java二进制-7: "+Integer.toBinaryString(-7));

输出:

Java二进制7: 111

Java二进制-7: 11111111111111111111111111111001

7的二进制就是111

-7转化二进制的过程:

(1)把-7转化成7,二进制是 111

(2)Java中对于不满32位的int二进制自动补齐,所以变成了 (29个0)111

(3)然后取反  (29个1)000

(4)然后加1 (29个1)001

扩展资料:

c语言中二进制的转换:

用函数转换itoa(值,数组名,进制)

#includestdio.h

#includestdlib.h

void main()

{

char str[8];

int i=8;

itoa(i,str,2);

printf("%s",str);

}

参考资料:

java二进制——百度百科

关于java写二进制文件和java编译二进制文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-03,除非注明,否则均为首码项目网原创文章,转载请注明出处。