序列流
序列流合并文件
package com.cloud.day5; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.Vector; /* SequenceInputStream(序列流) */ public class Demo1 { public static void main(String[] args) throws IOException { merge1(); } //把3个文件合并成一个文件 public static void merge3() throws IOException{ File file1 = new File("F:\\a.txt"); File file2 = new File("F:\\b.txt"); File file3 = new File("F:\\c.txt"); File file4 = new File("F:\\d.txt"); //建立对应的输入输出流对象 FileOutputStream fileOutputStream = new FileOutputStream(file4); FileInputStream fileInputStream1 = new FileInputStream(file1); FileInputStream fileInputStream2 = new FileInputStream(file2); FileInputStream fileInputStream3 = new FileInputStream(file3); //创建序列流对象 Vector<FileInputStream> vector = new Vector<FileInputStream>(); vector.add(fileInputStream1); vector.add(fileInputStream2); vector.add(fileInputStream3); Enumeration<FileInputStream> e = vector.elements(); SequenceInputStream sequenceInputStream = new SequenceInputStream(e); //读取文件数据 byte[] buf = new byte[1024]; int length = 0; while((length = sequenceInputStream.read(buf))!=-1){ fileOutputStream.write(buf, 0, length); } sequenceInputStream.close(); fileOutputStream.close(); } //SequenceInputStream合并文件夹 public static void meege2() throws IOException{ File inFile1 = new File("F:\\a.txt"); File inFile2 = new File("F:\\b.txt"); File outFile = new File("F:\\c.txt"); FileOutputStream fileOutputStream = new FileOutputStream(outFile); FileInputStream fileInputStream1 = new FileInputStream(inFile1); FileInputStream fileInputStream2 = new FileInputStream(inFile2); //建立序列流对象 SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1, fileInputStream2); byte[] buf = new byte[1024]; int len=0; while((len = inputStream.read(buf))!=-1){ fileOutputStream.write(buf, 0, len); } inputStream.close(); fileOutputStream.close(); } //合并a.txt和b.txt内容 public static void merge1() throws IOException{ File inFile1 = new File("F:\\a.txt"); File inFile2 = new File("F:\\b.txt"); File outFile = new File("F:\\c.txt"); FileOutputStream fileOutputStream = new FileOutputStream(outFile); FileInputStream fileInputStream1 = new FileInputStream(inFile1); FileInputStream fileInputStream2 = new FileInputStream(inFile2); ArrayList<FileInputStream> list = new ArrayList<FileInputStream>(); list.add(fileInputStream1); list.add(fileInputStream2); byte[] buf = new byte[1024]; int length = 0; for(int i=0;i<list.size();i++){ FileInputStream fileInputStream = list.get(i); while((length = fileInputStream.read(buf))!=-1){ fileOutputStream.write(buf, 0, length); } fileInputStream.close(); } fileOutputStream.close(); } } |
序列流切割文件
package com.cloud.day5; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.SequenceInputStream; import java.util.Enumeration; import java.util.Vector; /* 切割一首MP3再合并 */ public class Demo2 { public static void main(String[] args) throws IOException { File dir = new File("F:\\music"); Vector<FileInputStream> vector = new Vector<FileInputStream>(); File[] files = dir.listFiles(); for(File file:files){ if(file.getName().endsWith(".mp3")){ vector.add(new FileInputStream(file)); } } Enumeration<FileInputStream> e = vector.elements(); SequenceInputStream inputStream = new SequenceInputStream(e); FileOutputStream fileOutputStream = new FileOutputStream("F:\\sum.mp3"); byte[] buf = new byte[1024]; int length = 0; while((length = inputStream.read(buf))!=-1){ fileOutputStream.write(buf,0,length); } fileOutputStream.close(); inputStream.close(); } //切割MP3 public static void cutFile() throws IOException{ File file = new File("F:\\1.mp3"); File dir = new File("F:\\music"); FileInputStream fileInputStream = new FileInputStream(file); byte[] buf = new byte[1024*1024]; int length = 0; for(int i=0;(length = fileInputStream.read(buf))!=-1;i++){ FileOutputStream fileOutputStream = new FileOutputStream(new File(dir,"part"+i+".mp3")); fileOutputStream.write(buf, 0, length); fileOutputStream.close(); } fileInputStream.close(); } } |
打印流
打印流过程
package com.cloud.day1; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; /* 打印流(printStream) 打印流可以打印任意类型的数据,而且打印数据之前都会先把数据转换成字符串再进行打印。 */ class Animal { String name; String color; public Animal(String name, String color) { this.name = name; this.color = color; } @Override public String toString() { return "Animal [name=" + name + ", color=" + color + "]"; } } public class Demo1 { public static void main(String[] args) throws IOException { File file = new File("E:\\a.txt"); //创建一个打印流 /* PrintStream printStream = new PrintStream(file); printStream.println(123); printStream.println("spring"); Animal animal = new Animal("Cat","black"); printStream.println(animal); //默认标准的输出流就是向控制台输出 System.out.println(printStream); */ //收集日常的日志信息 File logFile = new File("E:\\execption.log"); PrintStream printStream = new PrintStream(new FileOutputStream(logFile,true)); try { int c = 4/0; System.out.println("c="+c); int[] arr = null; System.out.println(arr.length); } catch (Exception e) { e.printStackTrace(); } } } |
流的编码解码问题
基本案例
package com.cloud.day1; import java.util.Arrays; /* 编码和解码问题 编码:把看得懂的字符变成看懂码值,这个过程叫做编码 解码:把码值查找对应的字符,我们把这个过程称作为解码 注意:编码与解码一般我们都使用统一的码表。否则非常容易出乱码 */ public class Demo2 { public static void main(String[] args) throws Exception { String str = "中国"; //平台默认的编码表GBK byte []buf = str.getBytes("utf-8"); System.out.println("数组的元素:"+Arrays.toString(buf)); //默认使用GBK码表解码 str = new String(buf,"utf-8"); System.out.println("解码后的字符串:"+str); String str1 = "大家好"; //使用GBK进行编码 byte []buf1 = str1.getBytes(); System.out.println("字节数组:"+Arrays.toString(buf1)); str1 = new String(buf1 , "iso8859-1"); //出现乱码后还原 byte []buf2 = str1.getBytes("iso8859-1"); str1 = new String(buf2 , "gbk"); System.out.println(str1); } } |
转换流
转换流使用
package com.cloud.day1; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStreamWriter; /* 转换流: 输入字节流的转换流:InputStreamReader 是字节流通向字符流的桥 InputStreamReader 输出字节流的转换流: OutputStreamWriter 可以把输出字节流转换成输出字符流。 转换流的作用: 1. 如果目前所获取到的是一个字节流需要转换字符流使用,这时候就可以使用转换流。 字节流----> 字符流 2. 使用转换流可以指定编码表进行读写文件。 */ public class Demo3 { public static void main(String[] args) throws IOException { //readTest(); //writeTest(); readTest2(); } //使用输入字节流的转换流指定码表进行读取文件数据 public static void readTest2() throws IOException{ File file = new File("D:\\a.txt"); FileInputStream fileInputStream = new FileInputStream(file); //这里的编码如果使用UTF-8会发生乱码 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream,"gbk"); char []buf = new char[1024]; int length = 0; while((length = inputStreamReader.read(buf))!=-1){ System.out.println(new String(buf , 0 , length)); } } public static void writeTest() throws IOException{ File file = new File("D:\\a.txt"); FileOutputStream fileOutputStream = new FileOutputStream(file); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream); outputStreamWriter.write("Hello,春天"); outputStreamWriter.close(); } /** * 控制台输入:asa * 内容:asa * @throws IOException */ public static void readTest() throws IOException{ //获取标准的输入流 InputStream in = System.in; //字节流转换成字符流 InputStreamReader inputStreamReader = new InputStreamReader(in); //使用字符流的缓冲类 BufferedReader bufferedReader = new BufferedReader(inputStreamReader); String line = null; while((line = bufferedReader.readLine())!=null){ System.out.println("内容:"+line); } } } |