当前位置:网站首页>File class byte input and output stream
File class byte input and output stream
2022-07-27 16:58:00 【Not very [email protected]】
Catalog
1、 Byte output stream :FileOutputStream
2、 Byte input stream :FileIntputStream
3、 Byte output stream is used in conjunction with byte input stream :
2. Create a FileOutputStream object :
3、 The serialization result is :
1、 Byte output stream :FileOutputStream
@Test
public void test() throws Exception {
FileOutputStream fileOutputStream = new FileOutputStream("F:\\111\\1.txt");
String str="lwh";
byte[] bytes = str.getBytes();
fileOutputStream.write(bytes);
fileOutputStream.close();
}Running results :

2、 Byte input stream :FileIntputStream
@Test
public void test2() throws Exception{
FileInputStream os = new FileInputStream("F:\\111\\1.txt");
byte[] bytes = new byte[3];
int c=0;
while ((c=os.read(bytes))!=-1){
String str = new String(bytes,0,c);
System.out.println(str);
}Running results :

3、 Byte output stream is used in conjunction with byte input stream :
package demo420.demo02;
import org.junit.Test;
import java.io.*;
/**
* @ founder xiaoliu
* @ Creation time 2022/4/20
* @ describe
*/
public class Demo01 {
@Test
public void test() throws IOException {
FileInputStream fos = new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.jpg");
FileOutputStream os = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\2.jpg");
byte[] bytes = new byte[5];
int len;
while ((len=fos.read(bytes))!=-1) {
os.write(bytes,0,len);
}
fos.close();
os.close();
}
}
result :

4、 Cache stream :
BufferedInputStream and BufferedOutputStream These two classes are InputStream and OutputStream Subclasses of , As a subclass of decorator , Use them to prevent every read / Actual write operation when sending data , Represents the use of buffers .
package demo420.demo04;
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* @ founder xiaoliu
* @ Creation time 2022/4/21
* @ describe
*/
public class DemoBuffer {
@Test
public void test() throws Exception{
FileInputStream is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\111\\6.txt");
FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\111\\7.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
int c=0;
byte[] bytes = new byte[10];
while ((c=is.read(bytes))!=-1){
String str = new String(bytes,0,c);
bos.write(str.getBytes());// byte[] bytes = str.getBytes(); Convert to byte array
}
}
}
add bos.flush or is.close after , The data will be loaded into the file
bos.flush(); is.close();
Summary :
Only after the stream refresh operation or the stream close operation , Data will be loaded
5、 Object flow :
java.io.ObjectOutputStream and java.io.ObjectInputStream These two classes can facilitate the serialization of objects (Serialize) And deserialization (Deserialize).
1. Create an object
package demo420.demo03;
import java.io.Serializable;
/**
* @ founder xiaoliu
* @ Creation time 2022/4/20
* @ describe
*/
public class Hero implements Serializable {
private String name;
private int age;
private String level;
public Hero(String name, int age, String level) {
this.name = name;
this.age = age;
this.level = level;
}
@Override
public String toString() {
return "Hero{" +
"name='" + name + '\'' +
", age=" + age +
", level='" + level + '\'' +
'}';
}
}
2. Create a FileOutputStream object :
FileOutputStream is = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\demo.txt");
ObjectOutputStream os = new ObjectOutputStream(is);
Hero lwh = new Hero("lwh", 2, "3");
os.writeObject(lwh);
os.close();3、 The serialization result is :

4、 Implement deserialization
Class to implement Serializable Interface

FileInputStream is = new FileInputStream("C:\\Users\\Administrator\\Desktop\\demo.txt");
ObjectInputStream objectInputStream = new ObjectInputStream(is);
Object o = objectInputStream.readObject();
System.out.println(o);
is.close();result :

版权声明
本文为[Not very [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/208/202207271449228876.html
边栏推荐
- Ten thousand words analysis ribbon core components and operation principle
- Advanced pointer of C language
- 牛客题目——判断是不是完全二叉树、平衡二叉树
- Jerry's in ear detection function [chapter]
- MPC_ ORCA
- 从零开始Blazor Server(1)--项目搭建
- Rotate the whole model in ADAMS
- log4j.jar和slf4-log4下载链接
- 字符流读取文件
- CDQ divide and conquer and whole dichotomy learning notes
猜你喜欢

As changes the background theme and background picture

File类字节输入、输出流

(2) Dynamic convolution of dynamic convolution

C语言之操作符

mysql视图及存储过程

JD Zhang Zheng: practice and exploration of content understanding in advertising scenes

Quadratic programming based on osqp

Dynamic memory allocation in C language

C语言之指针初级

Opencv (III) -- image segmentation
随机推荐
JSON data format usage
The 21st - -- the 30th time
Servlet uses cookies to realize the last login time of users
LNMP environment - deploy WordPress
MQ Series 2: technology selection of Message Oriented Middleware
MPC_ ORCA
OpenCV(三)——图像分割
Configuration and application of gurobi in pycharm
Enumeration and union of C language
Insert pictures in word to maintain high DPI method
JSON data parsing
Data collection: skillfully using Bloom filter to extract data summary
分享一个网上搜不到的「Redis」实现「聊天回合制」的方案
Duplicate names in molecular class methods
MPC5744p时钟模块
牛客题目——判断是不是完全二叉树、平衡二叉树
实测:云RDS MySQL性能是自建的1.6倍
JDBC程序实现完整步骤
CODIS cluster deployment
Scala for loop (loop guard, loop step size, loop nesting, introducing variables, loop return value, loop interrupt breaks)