「java转成byte」java转成exe图片显示不了

博主:adminadmin 2023-01-26 00:21:07 245

本篇文章给大家谈谈java转成byte,以及java转成exe图片显示不了对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

如何把“java”中一个文件转化为“byte”数组?

import java.io.File;\x0d\x0a\x0d\x0aimport java.io.FileInputStream;\x0d\x0a\x0d\x0aimport java.io.IOException;\x0d\x0a\x0d\x0aimport java.io.InputStream;\x0d\x0a\x0d\x0apublic class Test {\x0d\x0a\x0d\x0a public static void main(String[] args) {\x0d\x0a\x0d\x0a // TODO Auto-generated method stub\x0d\x0a\x0d\x0a try{\x0d\x0a\x0d\x0a getBytesFromFile(new File("C:\\aaa.txt"));\x0d\x0a\x0d\x0a }catch(IOException e){\x0d\x0a\x0d\x0a System.out.println("IOException");\x0d\x0a\x0d\x0a }\x0d\x0a\x0d\x0a }\x0d\x0a\x0d\x0a // 返回一个byte数组\x0d\x0a\x0d\x0a public static byte[] getBytesFromFile(File file) throws IOException {\x0d\x0a\x0d\x0a InputStream is = new FileInputStream(file);\x0d\x0a\x0d\x0a// 获取文件大小\x0d\x0a\x0d\x0a long length = file.length();\x0d\x0a\x0d\x0aif (length Integer.MAX_VALUE) {\x0d\x0a\x0d\x0a // 文件太大,无法读取\x0d\x0a\x0d\x0a throw new IOException("File is to large "+file.getName());\x0d\x0a\x0d\x0a }\x0d\x0a\x0d\x0a// 创建一个数据来保存文件数据\x0d\x0a\x0d\x0a byte[] bytes = new byte[(int)length];\x0d\x0a\x0d\x0a// 读取数据到byte数组中\x0d\x0a\x0d\x0a int offset = 0;\x0d\x0a\x0d\x0a int numRead = 0;\x0d\x0a\x0d\x0a while (offset = 0) {\x0d\x0a\x0d\x0a offset += numRead;\x0d\x0a\x0d\x0a }\x0d\x0a\x0d\x0a// 确保所有数据均被读取\x0d\x0a\x0d\x0a if (offset

回答于 2022-11-16

java 中各种数据类型转换byte[]的方法

ObjectOutputStream oos = null;//对象输出流

ByteArrayOutputStream baos = null;//byte数组输出流

ByteArrayInputStream bais = null;//对象输入流

try {

//序列化

baos = new ByteArrayOutputStream();

oos = new ObjectOutputStream(baos);//将数组流传入对象流

oos.writeObject(new Integer(1));//用对象流读取对象。

byte[] bytes = baos.toByteArray();//用数组流将传入的对象转化为byte数组

//反序列化

bais = new ByteArrayInputStream(bytes);

ObjectInputStream ois = new ObjectInputStream(bais);

Integer i = (Integer)ois.readObject();

System.out.println(i);

} catch (Exception e) {

}以上是把integer类型转化成byte[]数组类型。注:基本类型要转化为byte[]数组的话,需要用该基本类型的引用类。比如int的引用类是integer,就行了所有的类型,包括class都可以用这种序列化方式来转成byte[],

java中String类型的如何转为byte[]

一、String转byte数组简单版:

1、String str = "abcd";

2、byte[] bs = str.getBytes();

二、复杂版

// pros - no need to handle UnsupportedEncodingException // pros - bytes in specified

encoding scheme byte[] utf8 = "abcdefgh".getBytes(StandardCharsets.UTF_8);

System.out.println("length of byte array in UTF-8 : " + utf8.length);

System.out.println("contents of byte array in UTF-8: " + Arrays.toString(utf8));

Output : length of byte array in UTF-8 : 8 contents of byte array in UTF-8: [97, 98, 99, 100, 101, 102, 103, 104]1

扩展资料:

反过来,将Byte数组转化为String的方法

using System;

using System.Text;

public static string FromASCIIByteArray(byte[] characters)

{

ASCIIEncoding encoding = new ASCIIEncoding( );

string constructedString = encoding.GetString(characters);

return (constructedString);

}

·

java转成byte的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java转成exe图片显示不了、java转成byte的信息别忘了在本站进行查找喔。