Android 如何将byte数据存入list
时间: 2024-05-07 13:15:13 浏览: 17
你可以使用以下代码将byte数据存入list:
```java
List<Byte> byteList = new ArrayList<>();
byte[] bytes = {1, 2, 3, 4};
for (byte b : bytes) {
byteList.add(b);
}
```
你也可以使用Java 8的Stream API来实现:
```java
byte[] bytes = {1, 2, 3, 4};
List<Byte> byteList = Arrays.stream(bytes)
.boxed()
.collect(Collectors.toList());
```
相关问题
Android 如何将byte数据存入;ist
ream中?
可以使用以下方法将byte数据存入InputStream中:
1. 创建一个ByteArrayInputStream对象,将byte数组作为参数传递给它。
byte[] bytes = {1, 2, 3, 4, 5};
InputStream inputStream = new ByteArrayInputStream(bytes);
2. 使用FileInputStream对象将byte数组存入文件中。
FileOutputStream outputStream = new FileOutputStream("filename");
outputStream.write(bytes);
outputStream.close();
3. 使用BufferedInputStream对象读取byte数组中的数据。
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
int data = bufferedInputStream.read();
while (data != -1) {
// 处理读取到的数据
data = bufferedInputStream.read();
}
bufferedInputStream.close();
Android如何将Byte数组数据存入缓存
Android 中可以使用 SharedPreferences 将 Byte 数组数据存入缓存。可以通过以下步骤实现:
1. 使用 SharedPreferences 实例化一个 SharedPreference 对象。
2. 使用 Editor 实例化一个 SharedPreferences.Editor 对象。
3. 使用 putByteArray() 方法将 Byte 数组数据存入 Editor 对象中。
4. 使用 commit() 方法将 Editor 对象提交,完成缓存数据。
以下是示例代码:
```
SharedPreferences sharedPreferences = getSharedPreferences("myPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putByteArray("myKey", myByteArray);
editor.commit();
```
相关推荐
![.zip](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)