Java IO总结之缓冲读入文件
package com.io; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.LinkedList; /** * 缓冲读入文件 * @author Administrator * */ public class BufferedInputFile { public static String read(String filename) throws IOException{ //reading input by lines: BufferedReader in = new BufferedReader(new FileReader(filename)); String s; StringBuilder sb= new StringBuilder(); while((s=in.readLine())!=null){ sb.append(s+"\n"); } in.close(); return sb.toString(); } //test1,打开一个文本文件,每次读取一行内容,将每行作为一个string读入,并将该string对象存入linklist中,逆序打印linklist public static void printLinkList(String fileName) throws IOException{ BufferedReader in = new BufferedReader(new FileReader("C:/Users/Administrator/Desktop/exec.txt")); String string; LinkedList<String> list = new LinkedList<String>(); while((string=in.readLine())!=null){ list.add(string); } /*for(int i=list.size();i>=0;i--){ System.out.println(list.get(i)); }*/ java.util.ListIterator<String> item = list.listIterator(list.size()); while(item.hasPrevious()){ System.out.println(item.previous()); } while(item.hasNext()){ System.out.println(item.next()); } /** * 结果: * 4-->4-->4:4:4:ghdgml11 3-->3-->3:3:3:flypiger 2-->2-->2:2:2:ghdgml11 1-->1-->1:1:1:flypiger */ } public static void main(String[] args) throws IOException { //System.out.println(read("C:/Users/Administrator/Desktop/exec.txt")); printLinkList("C:/Users/Administrator/Desktop/exec.txt"); } }
不要问为什么,代码诠释一切
package com.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.StringReader; /** * 基本的io * @author Administrator * */ public class BasicFileOutput { public static String file = "C:/Users/Administrator/Desktop/exec.txt"; public static String file1 = "C:/Users/Administrator/Desktop/exe.txt"; public static void main(String[] args) throws IOException { //使用BufferedReader缓冲读入流,使用StringReader包装一个BufferedInputFile.read方法读取的字符串文件流 System.out.println(BufferedInputFile.read(file)); BufferedReader in = new BufferedReader(new StringReader(BufferedInputFile.read(file))); //FileWriter可以写数据的对象,用BufferedWriter缓冲流包装提升性能,再用PrintWriter包装提升格式 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file1))); int lineCount = 1; String string; while((string=in.readLine())!=null){ out.println(lineCount++ +": "+string); } out.flush(); out.close(); in.close(); System.out.println(BufferedInputFile.read(file1)); } }
package com.io; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; /** * 读取二进制文件 * @author Administrator * */ public class BinaryFile { public static byte[] read(File file) throws IOException{ BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file)); byte[] data = new byte[inputStream.available()]; inputStream.read(data); inputStream.close(); return data; } public static byte[] read(String file) throws IOException{ return read(new File(file).getAbsoluteFile()); } }
package com.io; import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.IOException; /** * 格式化的内存输出 * @author Administrator * */ public class FormattedMemoryInput { public static void main(String[] args) throws IOException { DataInputStream in= new DataInputStream(new ByteArrayInputStream(BufferedInputFile.read("C:/Users/Administrator/Desktop/exec.txt").getBytes())); //available用来查询还有多少可供存取的字符 while(in.available()!=0){ System.out.println(in.readByte()); } } }
package com.io; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * 用GZIP进行压缩,有空总结常用流的包装 * @author Administrator * */ public class GZIPcompress { /**压缩/解压缩 常用流,可根据实例替换 * ZipOutputStream * ZipInputStream * GZipOutputStream * GZipInputStream * @param args * @throws IOException */ public static void main(String[] args) throws IOException { if(args.length==0){ System.out.println( "Usage: \nGZIPcompress file\n"+ "\tUsers GZIP compression to compress "+ "the file to test.gz" ); BufferedReader in = new BufferedReader(new FileReader("C:/Users/Administrator/Desktop/exec.txt")); //后缀名可以是.gz .gzip.zip BufferedOutputStream out= new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("C:/Users/Administrator/Desktop/test.gz"))); System.out.println("Writing file*********************************"); int c; while((c=in.read())!=-1){ out.write(c); } out.close(); in.close(); System.out.println("reading file*********************************"); BufferedReader in2 = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("C:/Users/Administrator/Desktop/test.gz")))); String string; while((string=in2.readLine())!=null){ System.out.println(string); } } } }
package com.io; import java.io.IOException; import java.io.StringReader; /** * 从内存中读取,没什么好说的 * @author Administrator * */ public class MemoryInput { public static void main(String[] args) throws IOException { StringReader in = new StringReader(BufferedInputFile.read("C:/Users/Administrator/Desktop/exec.txt")); //SringReader读取的int类型的,所以要转换成char打印 int c; while((c=in.read())!=-1){ System.out.println((char)c); } } }