打印流
PrintWriter与PrintStream
可以直接操作输入流和文件
序列流
SequenceInputStream
对多个流进行合并
操作对象
ObjectInputStream与ObjectOutputStream
被操作的对象需要实现Serializable(标记接口)
字节打印流
PrintStream
构造函数可以接收的参数类型;
1、File对象,File;
2、字符串路径:String;
3、字节输出流:OutputStream
字符打印流:
PrintWriter:
构造函数可以接收的参数类型;
1、File对象:File;
2、字符串路径:String;
3、字节输出流:OutputStream;
4、字符输出流:Writer;
package com.javaxl.io.other; import java.io.*; /** * @author 小李飞刀 * @site www.javaxl.com * @company * @create 2019-06-15 8:31 */ public class PrintStreamDemo { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); PrintWriter pw = new PrintWriter(new FileWriter("C:\\PrintWriterDemo.txt"),true); String line = null; while ((line = br.readLine())!=null){ if("over".equals(line)){ break; } pw.println(line.toUpperCase()); // pw.flush(); true代表自动刷新 } br.close(); pw.close(); } }
应用:多章小说合并成一本小说
package com.javaxl.io.other; import java.io.*; import java.util.Vector; /** * @author 小李飞刀 * @site www.javaxl.com * @company * @create 2019-06-15 8:53 */ public class SequenceDemo { public static void main(String[] args) throws IOException { Vector<FileInputStream> v = new Vector<>(); v.add(new FileInputStream("C:\\charset.txt")); v.add(new FileInputStream("C:\\fos.txt")); v.add(new FileInputStream("C:\\PrintWriterDemo.txt")); SequenceInputStream sis = new SequenceInputStream(v.elements()); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\sequence.txt")); byte[] bbuf = new byte[1024]; int len = 0; while ((len = sis.read(bbuf))!=-1){ bos.write(bbuf,0,len); } bos.close(); sis.close(); } }
Vector的遍历在IO流中的应用
当服务器限制上传文件的大小,可以将其切割上传
将切割的文件合并
package com.javaxl.io.other; import java.io.*; import java.util.ArrayList; import java.util.Enumeration; import java.util.Iterator; import java.util.List; /** * @author 小李飞刀 * @site www.javaxl.com * @company * @create 2019-06-15 9:29 */ public class SplitDemo { public static void main(String[] args) throws IOException { // split(); merge(); } public static void split() throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\6.jpg")); FileOutputStream out = null; byte[] bbuf = new byte[1024*10]; int len = 0; int count = 1; while ((len = bis.read(bbuf))!=-1){ out = new FileOutputStream("C:\\partFiles\\"+(count++)+".part"); out.write(bbuf,0,len); out.close(); } bis.close(); } public static void merge() throws IOException { List<FileInputStream> al = new ArrayList<>(); File dir = new File("C:\\partFiles"); for (File file : dir.listFiles()) { al.add(new FileInputStream(file)); } Iterator<FileInputStream> it = al.iterator(); Enumeration<FileInputStream> en = new Enumeration<FileInputStream>() { @Override public boolean hasMoreElements() { return it.hasNext(); } @Override public FileInputStream nextElement() { return it.next(); } }; SequenceInputStream sis = new SequenceInputStream(en); FileOutputStream fos = new FileOutputStream("C:\\partFiles\\together.jpg"); byte[] bbuf = new byte[1024]; int len = 0; while ((len = sis.read(bbuf))!=-1){ fos.write(bbuf,0,len); } fos.close(); } }
因为使用vector对象性能较低,所以采用ArrayList来存放需要合并的碎片文件,但是创建序列流需要用到枚举接口,所以用匿名内部类来完成枚举的创建,而匿名内部类所使用的变量需要被final所修饰。
备案号:湘ICP备19000029号
Copyright © 2018-2019 javaxl晓码阁 版权所有