博客
关于我
File类字节输入、输出流
阅读量:796 次
发布时间:2023-03-28

本文共 3679 字,大约阅读时间需要 12 分钟。

Java 输入输出流基础教程

1. 字节输出流:FileOutputStream

文件输出流(FileOutputStream)是Java中用于将数据写入文件的核心类。它允许程序将字节数据输出到文件中,常见应用场景包括日志记录、配置文件生成等。

@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();
}

2. 字节输入流:FileInputStream

文件输入流(FileInputStream)则相反,它用于从文件中读取字节数据。程序可以通过它逐个读取字节,或者一次性读取多个字节。

@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);
}
}

3. 字节输出流与输入流的联合使用

有时,程序需要将数据从一个文件读取后立即写入另一个文件。这种情况下,可以通过同时使用FileInputStream和FileOutputStream来实现数据的传输。

package demo420.demo02;
import org.junit.Test;
import java.io.*;
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();
}
}

4. 缓存流

缓存流(BufferedInputStream和BufferedOutputStream)是一种通过在读取/写入数据时使用内存缓冲区来提高性能的优化技术。使用缓存流可以减少对实际设备的直接操作,提升数据处理效率。

package demo420.demo04;
import org.junit.Test;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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);
byte[] bytes = new byte[10];
while ((int c = is.read(bytes)) != -1) {
String str = new String(bytes, 0, c);
bos.write(str.getBytes());
}
bos.flush();
is.close();
fos.close();
}
}

5. 对象流

对象流(ObjectOutputStream和ObjectInputStream)是一种更高效的数据序列化工具。它能够将Java对象直接序列化为字节流,便于传输和恢复。

package demo420.demo03;
import java.io.Serializable;
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 + '\'' +
'}';
}
}
package demo420.demo03;
import java.io.*;
public class DemoSerialize {
@Test
public void test() throws Exception {
Hero lwh = new Hero("lwh", 2, "3");
FileOutputStream is = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\demo.txt");
ObjectOutputStream os = new ObjectOutputStream(is);
os.writeObject(lwh);
os.close();
}
}
package demo420.demo03;
import java.io.*;
public class DemoDeserialize {
@Test
public void test() throws Exception {
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();
}
}

转载地址:http://jbhfk.baihongyu.com/

你可能感兴趣的文章
Objective-C实现eval函数功能(附完整源码)
查看>>
Objective-C实现Exceeding words超词(差距是ascii码的距离) 算法(附完整源码)
查看>>
Objective-C实现extended euclidean algorithm扩展欧几里得算法(附完整源码)
查看>>
Objective-C实现Factorial digit sum阶乘数字和算法(附完整源码)
查看>>
Objective-C实现factorial iterative阶乘迭代算法(附完整源码)
查看>>
Objective-C实现factorial recursive阶乘递归算法(附完整源码)
查看>>
Objective-C实现FigurateNumber垛积数算法(附完整源码)
查看>>
Objective-C实现Gale-Shapley盖尔-沙普利算法(附完整源码)
查看>>
Objective-C实现hamiltonianCycle哈密尔顿图算法(附完整源码)
查看>>
Objective-C实现hamming numbers汉明数算法(附完整源码)
查看>>
Objective-C实现hanning 窗(附完整源码)
查看>>
Objective-C实现hanoiTower汉诺塔算法(附完整源码)
查看>>
Objective-C实现hardy ramanujana定理算法(附完整源码)
查看>>
Objective-C实现highest response ratio next高响应比优先调度算法(附完整源码)
查看>>
Objective-C实现hill climbing爬山法用来寻找函数的最大值算法(附完整源码)
查看>>
Objective-C实现hornerMethod霍纳法算法(附完整源码)
查看>>
Objective-C实现Http Post请求(附完整源码)
查看>>
Objective-C实现Http协议下载文件(附完整源码)
查看>>
Objective-C实现IIR 滤波器算法(附完整源码)
查看>>
Objective-C实现IIR数字滤波器(附完整源码)
查看>>