本文共 7298 字,大约阅读时间需要 24 分钟。
Java IO流系统是Java编程的重要组成部分,涵盖了文件读写、数据传输等多个核心功能。作为开发者,掌握高级IO流的使用方法至关重要。本文将深入探讨Java中的缓冲流、转换流、序列化流以及打印流,帮助开发者更高效地处理IO操作。
缓冲流(BufferedInputStream/BufferedOutputStream)是一种高效流,它通过在内存中创建一个缓冲区,减少系统IO操作的次数,从而提高读写效率。缓冲流的主要分类如下:
缓冲流的工作原理是,在流对象创建时,会预先分配一个默认大小的缓冲区。读取或写入数据时,先将数据放入缓冲区,再批量处理,从而提升性能。
public BufferedInputStream(InputStream in)
: 创建一个新的缓冲输入流。public BufferedOutputStream(OutputStream out)
: 创建一个新的缓冲输出流。// 读取字节缓冲流BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bis.txt"));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));
通过测试发现,使用缓冲流处理大文件比普通流快得多。以下是两种流的效率对比:
普通流:
public class BufferedDemo { public static void main(String[] args) throws FileNotFoundException { long start = System.currentTimeMillis(); try (FileInputStream fis = new FileInputStream("jdk9.exe")) { FileOutputStream fos = new FileOutputStream("copy.exe"); int b; while ((b = fis.read()) != -1) { fos.write(b); } } catch (IOException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("普通流复制时间:" + (end - start) + "毫秒"); }}
缓冲流:
public class BufferedDemo { public static void main(String[] args) throws FileNotFoundException { long start = System.currentTimeMillis(); try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk9.exe"))) { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe")); int b; while ((b = bis.read()) != -1) { bos.write(b); } } catch (IOException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("缓冲流复制时间:" + (end - start) + "毫秒"); }}
缓冲流优化示例:
public class BufferedDemo { public static void main(String[] args) throws FileNotFoundException { long start = System.currentTimeMillis(); try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk9.exe"))) { BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe")); int len; byte[] bytes = new byte[8 * 1024]; while ((len = bis.read(bytes)) != -1) { bos.write(bytes, 0, len); } } catch (IOException e) { e.printStackTrace(); } long end = System.currentTimeMillis(); System.out.println("缓冲流使用数组复制时间:" + (end - start) + "毫秒"); }}
public BufferedReader(Reader in)
: 创建一个新的缓冲输入流。public BufferedWriter(Writer out)
: 创建一个新的缓冲输出流。// 读取字符缓冲流BufferedReader br = new BufferedReader(new FileReader("br.txt"));BufferedWriter bw = new BufferedWriter(new FileWriter("bw.txt"));
readLine()
: 读取一行文本。newLine()
: 写入换行符。读取一行文本:
public class BufferedReaderDemo { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new FileReader("in.txt")); String line = null; while ((line = br.readLine()) != null) { System.out.print(line); System.out.println("------"); } br.close(); }}
写入换行符:
public class BufferedWriterDemo { public static void main(String[] args) throws IOException { BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt")); bw.write("黑马"); bw.newLine(); bw.write("程序"); bw.newLine(); bw.write("员"); bw.newLine(); } bw.close();}
案例分析:
案例实现:
public class BufferedTest { public static void main(String[] args) throws IOException { HashMaplineMap = new HashMap<>(); BufferedReader br = new BufferedReader(new FileReader("in.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt")); String line = null; while ((line = br.readLine()) != null) { String[] split = line.split("\\."); lineMap.put(split[0], split[1]); } br.close(); for (int i = 1; i <= lineMap.size(); i++) { String key = String.valueOf(i); String value = lineMap.get(key); bw.write(key + "." + value); bw.newLine(); } bw.close(); }}
字符编码是将字符转换为二进制数的过程,字符集则包含了系统支持的所有字符。常见编码表包括ASCII、ISO-8859-1、GB2312、GBK和Unicode等。
InputStreamReader
是字节流与字符流之间的桥梁,用于指定编码读取文本文件。
代码示例:
// 读取GBK编码文件InputStreamReader isr = new InputStreamReader(new FileInputStream("E:\\File_GBK.txt"), "GBK");
读取示例:
public class ReaderDemo { public static void main(String[] args) throws IOException { String fileName = "E:\\File_GBK.txt"; InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName), "GBK"); int read; while ((read = isr.read()) != -1) { System.out.print((char) read); } isr.close(); }}
OutputStreamWriter
是字符流与字节流之间的桥梁,用于指定编码写出文本文件。
代码示例:
// 写入UTF-8编码文件OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("E:\\out.txt"), "UTF-8");osw.write("你好");osw.close();
转换流是字节流与字符流之间的桥梁,用于处理不同编码的文件。
Java 提供了强大的对象序列化机制,允许将Java对象保存为字节序列,实现持久化存储。
构造方法:
public ObjectOutputStream(OutputStream out)
代码示例:
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.txt"));out.writeObject(employee);
注意事项:
Serializable
接口。transient
修饰。构造方法:
public ObjectInputStream(InputStream in)
反序列化示例:
ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.txt"));Employee e = (Employee) in.readObject();in.close();
案例分析:
案例实现:
public class SerTest { public static void main(String[] args) throws Exception { Student student = new Student("老王", "laow"); Student student2 = new Student("老张", "laoz"); Student student3 = new Student("老李", "laol"); ArrayListlist = new ArrayList<>(); list.add(student); list.add(student2); list.add(student3); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("list.txt")); oos.writeObject(list); oos.close(); ObjectInputStream ois = new ObjectInputStream(new FileInputStream("list.txt")); ArrayList list = (ArrayList ) ois.readObject(); for (int i = 0; i < list.size(); i++) { Student s = list.get(i); System.out.println(s.getName() + "--" + s.getPwd()); } }}
打印流(PrintStream)提供了一个高效的方式来向标准输出或文件打印文本。
构造方法:
public PrintStream(String fileName)
代码示例:
PrintStream ps = new PrintStream("ps.txt");System.setOut(ps);System.out.println(97);
通过System.setOut()
方法,可以将打印流的目标文件更改为指定文件或其他流。
通过本章的学习,你应该能够熟练使用Java中的缓冲流、转换流、序列化流和打印流,提升日常开发中的IO操作效率。
转载地址:http://jelm.baihongyu.com/