打印流
package com.zzp.demo; import java.awt.image.FilteredImageSource; import java.io.BufferedOutputStream; import java.io.FileDescriptor; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; /** * * PrintStream * @author java * */ public class PrintTest01 { public static void main(String[] args) throws FileNotFoundException { PrintStream ps = System.out; ps.print("生活不易"); ps.println("且行且珍惜"); //加上true之后,自动刷新 ps = new PrintStream(new BufferedOutputStream(new FileOutputStream("4.txt")),true); ps.print("生活不易"); ps.println("且行且珍惜"); ps.println("真是这么简单"); ps.close(); //重定向输出端 System.setOut(ps); System.out.println("重定向"); //重定向回控制台 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true)); System.out.println("重定向回控制台"); } }
PrintWriter
package com.zzp.demo; import java.awt.image.FilteredImageSource; import java.io.BufferedOutputStream; import java.io.FileDescriptor; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.io.PrintWriter; /** * * PrintWriter * @author java * */ public class PrintTest02 { public static void main(String[] args) throws FileNotFoundException { //加上true之后,自动刷新 PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream("4.txt")),true); pw.print("生活不易"); pw.println("且行且珍惜"); pw.println("真是这么简单"); pw.close(); } }
随机读取流 RandomAccessFile
package com.zzp.demo; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; /** * * 随机读取流 RandomAccessFile * * @author java * */ public class RandTest01 { public static void main(String[] args) throws IOException { //分多少块 File src = new File("src/com/zzp/demo/CopyTxt.java"); //总长度 long len = src.length(); //每块的大小 int blockSize = 1024; //多少块,不够整数的向上取整 int size =(int) Math.ceil(len*1.0/blockSize); System.out.println(size); //起始位置 int beginPos = 0; //实际大小 int actualSize = (int)(blockSize > len?len:blockSize); for(int i=0;i<size;i++){ beginPos = i*blockSize; if(i==size-1){//最后一块 actualSize = (int)len; }else{ actualSize = blockSize; len -= actualSize; } split(i,beginPos,actualSize); } } //指定起始位置,读取剩余的所有内容 public static void test01(){ try { RandomAccessFile raf = new RandomAccessFile(new File("src/com/zzp/demo/CopyTxt.java"), "r"); //随机读取的位置 raf.seek(2); // 3、操作(写入操作) byte[] flush = new byte[1024]; // 1k一读取 int len = -1;// 设置默认长度为-1 while ((len = raf.read(flush)) != -1) { System.out.println(new String(flush, 0, len)); } raf.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } //分块思想 public static void test02(){ try { RandomAccessFile raf = new RandomAccessFile(new File("src/com/zzp/demo/CopyTxt.java"), "r"); //起始位置 int beginPos = 2; //实际大小 int actualSize = 1026; //随机读取的位置 raf.seek(beginPos); // 3、操作(写入操作) byte[] flush = new byte[1024]; // 1k一读取 int len = -1;// 设置默认长度为-1 while ((len = raf.read(flush)) != -1) { if(actualSize > len){ System.out.println(new String(flush, 0, len)); actualSize -= len; }else{ System.out.println(new String(flush, 0, actualSize)); break; } } raf.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 指定第i块的起始位置 和实际长度 * @param i * @param beginPos * @param actualSize * @throws IOException */ public static void split(int i,int beginPos,int actualSize ) throws IOException { RandomAccessFile raf =new RandomAccessFile(new File("src/com/sxt/io/Copy.java"),"r"); //随机读取 raf.seek(beginPos); //读取 //3、操作 (分段读取) byte[] flush = new byte[1024]; //缓冲容器 int len = -1; //接收长度 while((len=raf.read(flush))!=-1) { if(actualSize>len) { //获取本次读取的所有内容 System.out.println(new String(flush,0,len)); actualSize -=len; }else { System.out.println(new String(flush,0,actualSize)); break; } } raf.close(); } }