本文共 3679 字,大约阅读时间需要 12 分钟。
文件输出流(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();} 文件输入流(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); }} 有时,程序需要将数据从一个文件读取后立即写入另一个文件。这种情况下,可以通过同时使用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(); }} 缓存流(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(); }} 对象流(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/