1.Java I/O框架采用装饰器模式,可按照读写方式分为字节流和字符流,二者根据数据源的不同都封装了不同的类(ByteArray、String、File、Pipe等),FilterInputStream、FilterOutputStream是用来提供装饰器类接口以控制特定输入流和输出流的两个类。字节流和字符流之间可以通过InputStreamReader和OutPutStreamWriter相互转换。
2.常用案例
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; /* * 以字符形式缓冲输入一个文件 */ public class BufferedInputFile { static int count = 0; //缓冲读取一个指定文件 public static String read(String fileName){ StringBuffer sb = new StringBuffer(); try { BufferedReader in = new BufferedReader(new FileReader(new File(fileName))); String s; while((s = in.readLine()) != null){ count = count + 1; sb.append(s + "\n"); } in.close(); } catch (Exception e) { e.printStackTrace(); } return sb.toString(); } public static void main(String[] args) { System.out.print(read("E:/J2EE/新建文件夹/1.txt")); System.out.println("" + count); } }
import java.io.IOException; import java.io.StringReader; public class MemoryInput { public static void main(String[] args) { StringReader in = new StringReader(BufferedInputFile.read("E:/J2EE/新建文件夹/2.txt")); int c; try { while((c = in.read() ) != -1){ System.out.println((char)c); } in.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.EOFException; public class FormattedMemoryInput { public static void main(String[] args) { DataInputStream in = new DataInputStream(new ByteArrayInputStream(BufferedInputFile.read("E:/J2EE/新建文件夹/2.txt").getBytes())); try{ while(true){ System.out.println((char)in.readByte()); } }catch(EOFException e){ System.out.println("流结束"); e.printStackTrace(); }catch(Exception e){ e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.FileWriter; import java.io.IOException; import java.io.StringReader; /* *输出内容到文件中 */ public class BasicFileOutput { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new StringReader( BufferedInputFile.read("E:/J2EE/新建文件夹/2.txt"))); FileWriter out = new FileWriter("E:/J2EE/新建文件夹/2_copy.txt"); String s; while((s = in.readLine()) != null){ out.write(s + "\r\n"); //在每行结尾添加一个回车换行 } in.close(); out.close(); } }
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /* *使用DataInputStream写入数据 *使用DataOutputStream恢复数据 */ public class StoreAndRecoverData { public static void main(String[] args) throws IOException { DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("E:/J2EE/新建文件夹/3.txt"))); out.writeChars("cc"); //将字符串按字符顺序写入 out.writeUTF("以UTF形式写入"); //使用utf-8编码写入 out.writeDouble(3.141659); out.writeBoolean(true); out.writeInt(1212); out.close(); DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("E:/J2EE/新建文件夹/3.txt"))); System.out.println(in.readChar()); in.skipBytes(2); System.out.println(in.readUTF()); System.out.println(in.readBoolean()); System.out.println(in.readInt()); } }
3.压缩
java.util.zip包中封装了读写压缩格式数据的类,他们大都从java.io包中的相关类继承而来
CheckSum:表示数据校验和的接口,这个包提供了两种校验类型:Adler32、CRC32
CheckedOutputStream(OutputStream out, Checksum cksum) 可以创建一个使用制定校验和的OutputStream
CheckedInputStream(InputStream in, Checksum cksum)
可以创建一个使用制定校验和的InputStream
压缩类:ZipOutputStream : 将数据压缩成zip格式 GZipOutputStream 将数据压缩成gzip格式
解压缩类:ZipInputStram:用于解压缩Zip格式的数据 GZIPInputStram:解压缩GZIP文件格式的数据
例1:使用gzip进行简单压缩
/* *使用GZIP进行简单压缩 */ public class GZIPcompress { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("E:/J2EE/新建文件夹/2.txt")); BufferedOutputStream out = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("E:/J2EE/新建文件夹/2.gz"))); int c; while((c = in.read()) != -1) out.write(c); in.close(); out.close(); //GZIPInputStream是字节流,要想使用readLine必须通过InputStreamReader转换 BufferedReader gin = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("E:/J2EE/新建文件夹/2.gz")))); String s; while((s = gin.readLine() ) != null){ System.out.println(s); } } }
例2:
/* *使用Zip进行多文件压缩 */ public class ZipCompress { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("E:/J2EE/新建文件夹/1_2_3.zip"); CheckedOutputStream cos = new CheckedOutputStream(fos, new Adler32()); ZipOutputStream zos = new ZipOutputStream(cos); BufferedOutputStream out = new BufferedOutputStream(zos); zos.setComment("1.txt 2.txt 3.txt的压缩文件"); String [] fileNames = {"E:/J2EE/新建文件夹/1.txt", "E:/J2EE/新建文件夹/2.txt", "E:/J2EE/新建文件夹/3.txt"}; for(String name : fileNames){ BufferedReader in = new BufferedReader(new FileReader(name)); zos.putNextEntry(new ZipEntry(name)); int c; while((c = in.read()) != -1){ out.write(c); } in.close(); out.flush(); //每次循环都应当清空缓冲区(写一次文件),否则会导致所有内容都写到最后一个文件内 } out.close(); } }
时间: 2024-10-13 20:36:54