Java中的字节数组
Java字节数组仅用于存储字节数据类型值。字节数组中元素的默认值为0。
通过以下Java字节数组示例,您可以学习
- 如何声明Java字节数组?
- 如何为Java字节数组分配值?
- 如何从Java字节数组获取值?
- 如何在Java中将String转换为字节数组?
- 如何在Java中将字节数组转换为字符串?
什么是字节?
一组二进制数字或位(通常是八个)作为一个单元进行操作。一个字节,视为内存大小的单位。
字节是数字信息的单位,通常由八位组成。从历史上看,字节是计算机中用于编码文本的单个字符的位数,因此,它是许多计算机体系结构中最小的可寻址存储单元。
字节是一个数据测量单位,它包含八个位或一系列八个零和一。一个字节可用于表示2个8或256个不同的值。
字节在Java中是什么意思?
字节是Java中原始数据类型之一。这意味着Java字节的大小与计算机内存中的字节相同:它是8位,并且可以保存-128到127的值。字节数据类型以Java编程语言打包提供,无需特殊操作即可使其工作。
字节数组是什么意思?
字节为8位(二进制数据)。
字节数组是字节数组。
您可以使用字节数组来存储二进制数据的集合,例如文件的内容。不利之处是必须将整个文件内容加载到内存中。
Java中一个字节多少位?
该字节在Java中等于8位。
byte是Java中的关键字吗?
字节是Java中的关键字,它指定8位带符号整数基元类型。
标准Java整数数据类型以字节为单位:
- 字节1字节-128至127
- 短2字节-32768至32767
- int 4字节-2147483648至2147483647
- 长8字节-9223372036854775808至9223372036854775807
如何在Java中声明字节数组?
数组用[]声明(方括号)。如果将[](方括号)放在任何类型的任何变量之后,则仅该变量属于数组类型,而该声明中的其余变量不是属于该类型普通变量的数组变量。
如果将[](方括号)放在任何数据类型之后,则该声明中的所有变量都是数组变量。数组中的所有元素都可以通过index访问。数组元素索引从0到n-1个数字开始,即,如果数组有5个元素,则开始索引为0,结束索引为4。
// Java bytearray
//声明字节数组
byte [] byteArray;
如何在Java中初始化(init)字节数组(byte [])?
分配字节数组时,会将其初始化(init)为0。Java中的所有数组都初始化为type的默认值。这意味着将整数数组初始化为0,将布尔数组初始化为false,将引用类型数组初始化为null。
如何初始化字节数组示例
// Java bytearray
//如何在Java
byte []中初始化字节数组或byte [] byteArray = new byte [10];
Java中字节数组的初始值或默认值是多少?
字节数组的初始值为0。因为Java中字节数据类型的默认值为0。
Java中字节数组的长度是多少?
长度是Java中数组对象的属性,它返回给定数组中元素的数量。
Java中字节数组的最大长度是多少?
Java中字节数组的最大长度为2147483647。您最多可以存储2147483647个元素。
Java中数组的长度是多少?
在Java中,所有数组仅由int索引和声明。那就是数组的大小必须由一个int值指定,而不是long或short。所有数组索引从0开始到2147483646结束。您最多可以存储2147483647个元素。如果尝试将长(大)元素存储在数组中,则会遇到性能问题。如果您克服了性能问题,则应使用Java集合框架或简单地使用Vector。
句法 :
< data type > < variable >[];
< data type >[] < variable >;
|
Java字节数组示例
/* Java byte Array Example
Save with file name ByteArray.java */
public
class
ByteArray
{
public
static
void
main(String args[])
{
//JAVA BYTE ARRAY DECLARATION
byte
b[];
//MEMORY ALLOCATION FOR JAVA BYTE ARRAY
b =
new
byte
[
4
];
//ASSIGNING ELEMENTS TO JAVA BYTE ARRAY
b[
0
] =
20
;
b[
1
] =
10
;
b[
2
] =
30
;
b[
3
] =
5
;
//BYTE ARRAY OUTPUT
System.out.println(
"Java byte Array Example"
);
for
(
int
i=
0
;i<b.length;i++)
{
System.out.println(
"Element at Index : "
+ i +
" "
+ b[i]);
}
}
}
|
在Java字节数组示例中,您可以学习如何在声明时将值分配给Java字节数组。
声明时如何为Java字节数组分配值
/* How to assign values to java byte array at the time of declaration Example
Save with file name ByteArray2.java */
public
class
ByteArray2
{
public
static
void
main(String args[])
{
//JAVA BYTE ARRAY DECLARATION AND ASSIGNMENT
byte
b[] = {
20
,
10
,
30
,
5
};
//JAVA BYTE ARRAY OUTPUT
System.out.println(
"Java byte Array Example"
);
for
(
int
i=
0
;i<b.length;i++)
{
System.out.println(
"Element at Index : "
+ i +
" "
+ b[i]);
}
}
}
|
在Java字节数组示例之后,您可以学习如何使用其他Java字节数组变量声明Java字节数组。
如何使用其他Java字节数组变量声明Java字节数组
/* How to declare Java byte array with other Java byte array variables Example
Save with file name ByteArray3.java */
public
class
ByteArray3
{
public
static
void
main(String args[])
{
//JAVA BYTE ARRAY DECLARATION
byte
b[], a;
//b IS AN ARRAY a IS NOT AN ARRAY
//MEMORY ALLOCATION FOR JAVA BYTE ARRAY
b =
new
byte
[
4
];
//ASSIGNING ELEMENTS TO JAVA BYTE ARRAY
b[
0
] =
20
;
b[
1
] =
10
;
b[
2
] =
30
;
b[
3
] =
5
;
a =
100
;
//JAVA BYTE ARRAY OUTPUT
System.out.println(
"Java byte Array Example"
);
System.out.println(
"a value is : "
+a);
for
(
int
i=
0
;i<b.length;i++)
{
System.out.println(
"Element at Index : "
+ i +
" "
+ b[i]);
}
}
}
|
在Java字节数组示例之后,您可以学习如何将Java字节数组分配给其他Java字节数组。
如何将Java字节数组分配给其他Java字节数组
/* How to assign Java byte array to other Java byte array Example
Save with file name ByteArray4.java */
public
class
ByteArray4
{
public
static
void
main(String args[])
{
//JAVA BYTE ARRAY DECLARATION
byte
[] a, b;
//a AND b ARE ARRAY VARIABLES
//MEMORY ALLOCATION FOR JAVA BYTE ARRAY
b =
new
byte
[
4
];
//ASSIGNING ELEMENTS TO JAVA BYTE ARRAY
b[
0
] =
20
;
b[
1
] =
10
;
b[
2
] =
30
;
b[
3
] =
5
;
//ASSIGNING b ARRAY TO a ARRAY VARIABLE
a = b;
//JAVA BYTE ARRAY OUTPUT
System.out.println(
"Java byte Array Example"
);
System.out.println(
"b array values"
);
for
(
int
i=
0
;i<b.length;i++)
{
System.out.println(
"Element at Index : "
+ i +
" "
+ b[i]);
}
System.out.println(
"a array values"
);
for
(
int
i=
0
;i<b.length;i++)
{
System.out.println(
"Element at Index : "
+ i +
" "
+ a[i]);
}
}
}
|
Java将字符串转换为字节[]
我们可以通过两种方式将String转换为byte []。
- 通过使用String.getBytes()方法
- 通过在Java 8中使用Base64类
如何在Java中将String转换为字节数组?
若要从String转换为字节数组,请使用String.getBytes()方法。请注意,此方法使用平台的默认字符集。
我们可以使用String类的getBytes()方法使用平台的默认charset将字符串编码为字节序列。此方法已重载,我们也可以将Charset作为参数传递。
以下程序显示了如何在java中将String转换为字节数组。
如何在Java中将String转换为字节数组?
/* How to convert String to byte array in Java Example
Save with file name StringToByteArray.java */
import
java.util.Arrays;
// Convert String to byte array in Java
public
class
StringToByteArray
{
public
static
void
main(String[] args)
{
String str =
" convert String to byte Array in Java "
;
byte
[] bytearray = str.getBytes();
System.out.println(Arrays.toString(bytearray));
}
}
|
如何在Java 8中将String转换为字节数组?
要在Java 8中从String转换为字节数组,请使用Base64.getDecoder()。decode()方法。Base64.getDecoder()。decode()方法将字符串转换为字节数组。Base64类自Java 1.8开始,因此此代码在Java 1.8之前不起作用。
以下程序显示了如何在Java 8中将String转换为字节数组。
如何在Java 8中将String转换为字节数组?
/* How to convert String to byte array in Java 8 Example
Save with file name StringToByteArrayJava8.java */
import
java.util.Arrays;
import
java.util.Base64;
// Convert String to byte array in Java 8
public
class
StringToByteArrayJava8
{
public
static
void
main(String[] args)
{
String str =
" convert String to byte Array in Java "
;
byte
[] bytearray = Base64.getDecoder().decode(str);
System.out.println(Arrays.toString(bytearray));
}
}
|
如何在Java中将字节数组转换为字符串?
我们可以通过两种方式将字节数组转换为String。
- 没有字符编码。
- 具有字符编码。
- 通过在Java 8中使用Base64类
如何在不使用字符编码的Java中将字节数组转换为字符串?
我们甚至可以不指定字符编码就将字节数组转换为String。只需将字节数组传递给String构造函数即可。
如何在不使用字符编码的Java中将字节数组转换为字符串?
/* How to convert a byte Array to String in Java
without character encoding Example
Save with file name ByteArrayToString.java */
import
java.io.IOException;
import
java.util.Arrays;
// Convert a byte array to String in Java
public
class
ByteArrayToString
{
public
static
void
main(String[] args)
throws
IOException
{
byte
[] bytes =
" convert a byte Array to String in Java without character encoding "
.getBytes();
System.out.println(Arrays.toString(bytes));
// Create a string from the byte array
// without specifying character encoding
String string =
new
String(bytes);
System.out.println(string);
}
}
|
如何在Java中使用字符编码将字节数组转换为字符串?
我们可以使用UTF-8字符编码将字节数组转换为String。
如何在Java中使用字符编码将字节数组转换为字符串?
/* How to convert a byte Array to String in Java
with character encoding Example
Save with file name ByteArrayToString2.java */
import
java.io.IOException;
import
java.nio.charset.StandardCharsets;
// Convert a byte array to String in Java
class
ByteArrayToString2
{
public
static
void
main(String[] args)
throws
IOException
{
byte
[] bytes =
" convert a byte Array to String in Java with UTF-8 character encoding "
.getBytes(StandardCharsets.UTF_8);
// Create a string from the byte array with "UTF-8" encoding
String str =
new
String(bytes, StandardCharsets.UTF_8);
System.out.println(str);
}
}
|
如何在Java 8中将字节数组转换为String?
要将Java 8中的字节数组转换为String,请使用Base64.getEncoder()。encodeToString()方法。Base64.getEncoder()。encodeToString()方法将字节数组转换为String。Base64类自Java 1.8开始,因此此代码在Java 1.8之前不起作用。
以下程序显示了如何在Java 8中将字节数组转换为String。
如何在Java 8中将字节数组转换为String?
/* How to convert byte array to String in Java 8 Example
Save with file name ByteArrayToStringJava8.java */
import
java.util.Arrays;
import
java.util.Base64;
// Convert byte array String in Java 8
public
class
ByteArrayToStringJava8
{
public
static
void
main(String[] args)
{
byte
[] bytearray =
" convert byte Array to String in Java 8 "
.getBytes();
String str = Base64.getEncoder().encodeToString(bytearray);
System.out.println(str);
}
}
|
Java中的ByteBuffer是什么?
字节缓冲区可以是direct或non-direct。给定直接字节缓冲区,Java虚拟机将尽最大努力直接在其上执行本机I / O操作。也就是说,它将尝试避免在每次调用底层操作系统的本机I / O操作之前(或之后)将缓冲区的内容复制到中间缓冲区(或从中间缓冲区复制)。
java.nio.ByteBuffer类的allocate()方法用于分配新的字节缓冲区。
新缓冲区的位置将为零,其极限将是其容量,其标记将是未定义的,并且其每个元素都将初始化为零。它将有一个支持数组,并且其数组偏移量将为零。
如何将Java整数转换为字节数组?
我们将讨论将int转换为字节数组的各种方法。在java int中,数据类型占用4个字节(32位),范围是-2147483648至2147483647。
您可以使用Java NIO的ByteBuffer将Java整数转换为字节数组,这非常简单。下面的示例演示如何将Java整数转换为字节Array。
如何将Java整数转换为字节数组?
/* How to convert Java integer to byte Array Example
Save with file name IntToByteArray.java */
import
java.nio.ByteBuffer;
// Convert integer to byte array
public
class
IntToByteArray
{
public
static
void
main(String[] args)
{
byte
[] bytes = ByteBuffer.allocate(
4
).putInt(
1695609641
).array();
for
(
byte
b : bytes)
{
System.out.format(
"0x%x "
, b);
}
}
}
|
如何将Java字节数组转换为long?
Java提供了ByteBuffer类来做同样的事情。要转换任何字节数组,首先需要使用ByteBuffer的静态方法allocate分配8个字节,然后使用put方法放置byteArray并通过调用getLong()方法翻转字节缓冲区,我们可以获取该字节数组的long值。
您可以使用Java NIO的ByteBuffer将Java字节数组转换为long,这非常简单。
如何将Java字节数组转换为long?
/* How to convert Java byte Array to long Example
Save with file name ByteArrayToLong.java */
import
java.nio.ByteBuffer;
// Convert byte array to long
public
class
ByteArrayToLong
{
public
static
void
main(String[] args)
{
byte
[] bytes = {
0
,
6
,
36
, -
84
,
113
,
125
, -
118
, -
47
};
System.out.println(ByteBuffer.wrap(bytes).getLong());
System.out.println(convertByteArrayToLong(bytes));
}
public
static
long
convertByteArrayToLong(
byte
[] longBytes)
{
ByteBuffer byteBuffer = ByteBuffer.allocate(Long.BYTES);
byteBuffer.put(longBytes);
byteBuffer.flip();
return
byteBuffer.getLong();
}
}
|
如何在Java中将对象转换为字节数组?
- 通过实现Serializable接口使所需的对象可序列化。
- 创建一个ByteArrayOutputStream对象。
- 通过传递在上一步中创建的ByteArrayOutputStream对象来创建ObjectOutputStream对象。
- 使用ObjectOutputStream类的writeObject()方法将对象的内容写入输出流。
- 使用flush()方法将内容刷新到流中。
- 最后,使用toByteArray()方法将ByteArrayOutputStream的内容转换为字节数组。
下面的示例演示如何将对象转换为字节数组。准备要发送的字节数组:
如何在Java中将对象转换为字节数组?
/* How to convert an object to byte array Example
Save with file name ObjectToByteArray.java */
import
java.io.ByteArrayOutputStream;
import
java.io.ObjectOutputStream;
import
java.io.Serializable;
import
java.io.IOException;
// Convert an object to byte array
public
class
ObjectToByteArray
{
public
static
void
main(String[] args)
{
Sample yourObject =
null
;
ByteArrayOutputStream bos =
null
;
ObjectOutputStream out =
null
;
try
{
yourObject =
new
Sample();
bos =
new
ByteArrayOutputStream();
out =
new
ObjectOutputStream(bos);
out.writeObject(yourObject);
out.flush();
byte
[] yourBytes = bos.toByteArray();
}
catch
(Exception e)
{
// handle exception here.
}
finally
{
try
{
bos.close();
}
catch
(IOException ex)
{
// ignore close exception
}
}
}
}
class
Sample
implements
Serializable
{
public
void
display()
{
System.out.println(
"This is a sample class"
);
}
}
|
如何使用Java将byte [](数组)转换为文件?
“ hello” .getBytes(); 使用平台的默认字符集将给定的String编码为字节序列,该字符集将结果存储到新的byte数组中。
new File(“ c:\\ demo.txt”)通过将给定的路径名字符串转换为抽象路径名来创建一个新的File实例。
new FileOutputStream(file)创建一个文件输出流,以写入指定File对象表示的文件。
os.write(bytes)将指定字节数组中的字节数写入输出stream。
下面的示例演示如何将byte [](数组)转换为File。
如何使用Java将byte [](数组)转换为文件?
/* How to convert byte array to File Example
Save with file name ByteArrayToFile.java */
import
java.io.BufferedReader;
import
java.io.File;
import
java.io.FileOutputStream;
import
java.io.FileReader;
import
java.io.OutputStream;
// Convert byte array to File
public
class
ByteArrayToFile
{
public
static
void
main(String[] args)
{
byte
[] bytes =
"hello"
.getBytes();
File file =
new
File(
"c:\\demo.txt"
);
try
{
OutputStream os =
new
FileOutputStream(file);
os.write(bytes);
System.out.println(
"Write bytes to file."
);
printContent(file);
os.close();
}
catch
(Exception e)
{
e.printStackTrace();
}
}
public
static
void
printContent(File file)
throws
Exception
{
System.out.println(
"Print File Content"
);
BufferedReader br =
new
BufferedReader(
new
FileReader(file));
String line =
null
;
while
((line = br.readLine()) !=
null
)
{
System.out.println(line);
}
br.close();
}
}
|