第九篇 IO流技术(九)
打印流
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;// 设置默认长度为-1while ((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;// 设置默认长度为-1while ((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();} }
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 RandTest02 {public static void main(String[] args) throws IOException {// 分多少块File src = new File("4.JPG");// 总长度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);}}/*** 指定第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("4.JPG"), "r");RandomAccessFile raf2 = new RandomAccessFile(new File("dest/"+i+"4.JPG"), "rw");// 随机读取 raf.seek(beginPos);// 读取// 3、操作 (分段读取)byte[] flush = new byte[1024]; // 缓冲容器int len = -1; // 接收长度while ((len = raf.read(flush)) != -1) {if (actualSize > len) { // 获取本次读取的所有内容raf2.write(flush, 0, len);actualSize -= len;} else {raf2.write(flush, 0, actualSize);break;}}raf2.close();raf.close();} }
package com.zzp.demo;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.RandomAccessFile; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.List; import java.util.Vector;/*** * 面向对象思想封装 分割* * @author java**/ public class SplitFile {//源头private File src;//目的地(文件)private String destDir;//所有分割后的文件存储路径private List<String> destPaths;//每块的大小private int blockSize;//块数:多少块private int size;public SplitFile(String srcPath,String destDir){this(srcPath,destDir,1024);}public SplitFile(String srcPath,String destDir,int blockSize){this.destDir = destDir;this.src = new File(srcPath);this.blockSize =blockSize;this.destPaths =new ArrayList<String>();//初始化 init();}//初始化public void init(){//总长度long len = this.src.length(); //块数: 多少块this.size =(int) Math.ceil(len*1.0/blockSize);//路径for(int i=0;i<size;i++) {this.destPaths.add(this.destDir +"/"+i+"-"+this.src.getName());}}/*** 分割* 1、计算每一块的起始位置及大小* 2、分割* @throws IOException */public void split() throws IOException {//总长度long len = src.length(); //起始位置和实际大小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; //剩余量 }splitDetail(i,beginPos,actualSize);}} /*** 指定第i块的起始位置 和实际长度* @param i* @param beginPos* @param actualSize* @throws IOException*/private void splitDetail(int i,int beginPos,int actualSize ) throws IOException {RandomAccessFile raf =new RandomAccessFile(this.src,"r");RandomAccessFile raf2 =new RandomAccessFile(this.destPaths.get(i),"rw");//随机读取 raf.seek(beginPos);//读取//3、操作 (分段读取)byte[] flush = new byte[1024]; //缓冲容器int len = -1; //接收长度while((len=raf.read(flush))!=-1) { if(actualSize>len) { //获取本次读取的所有内容raf2.write(flush, 0, len);actualSize -=len;}else { raf2.write(flush, 0, actualSize);break;}} raf2.close();raf.close();}/*** 指定第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("4.JPG"), "r");RandomAccessFile raf2 = new RandomAccessFile(new File("dest/"+i+"4.JPG"), "rw");// 随机读取 raf.seek(beginPos);// 读取// 3、操作 (分段读取)byte[] flush = new byte[1024]; // 缓冲容器int len = -1; // 接收长度while ((len = raf.read(flush)) != -1) {if (actualSize > len) { // 获取本次读取的所有内容raf2.write(flush, 0, len);actualSize -= len;} else {raf2.write(flush, 0, actualSize);break;}}raf2.close();raf.close();}/*** 文件的合并* @throws IOException */public void merge(String destPath) throws IOException {//输出流OutputStream os =new BufferedOutputStream( new FileOutputStream(destPath,true)); Vector<InputStream> vi=new Vector<InputStream>();SequenceInputStream sis =null;//输入流for(int i=0;i<destPaths.size();i++) {vi.add(new BufferedInputStream(new FileInputStream(destPaths.get(i)))); }sis =new SequenceInputStream(vi.elements());//拷贝//3、操作 (分段读取)byte[] flush = new byte[1024]; //缓冲容器int len = -1; //接收长度while((len=sis.read(flush))!=-1) {os.write(flush,0,len); //分段写出 } os.flush(); sis.close();os.close();}public static void main(String[] args) throws IOException {SplitFile sf = new SplitFile("4.JPG","dest");sf.split();sf.merge("5.JPG");} }
转载于:https://www.cnblogs.com/zhangzhipeng001/p/9582228.html
第九篇 IO流技术(九)相关推荐
- JAVA基础 IO流技术学习笔记
目录 一.IO 流技术介绍 1.1 什么是IO? 1.2 流的概念 1.3 数据源 1.3.1 什么是数据源? 1.3.2数据源的分类 二.第一个简单的IO流程序 三.IO流经典写法(适用于任何 ...
- IO流技术【Properties类介绍、文件切割与合并】
IO流技术[Properties类介绍.文件切割与合并] 1.Properties类介绍 1.1.Properties的基本功能 Properties特点: 1.Hashtable的子类,map集合中 ...
- java第九章IO流与文件操作
使用输出流将String保存到文件(byte数组)byte[] bytes = "Java数据交流管道--IO流".getBytes(); 使用Filewriter 文章目录 9- ...
- 图片流写出 并带数据_第九章 IO流
1. 输入流和输出流联系和区别,节点流和处理流联系和区别 首先,你要明白什么是"流".直观地讲,流就像管道一样,在程序和文件之间,输入输出的方向是针对程序而言,向程序中读入东西,就 ...
- Java之IO流技术详解
何为IO? 首先,我们看看百度给出的解释. I/O输入/输出(Input/Output),分为IO设备和IO接口两个部分. i是写入,Input的首字母.o是输出,Output的首字母. IO 也称为 ...
- python篇 io流
1.io流概念 "io"是input stream和ouput stream的缩写:即输入输出流 主要进行点的是计算机输入和输出的操作. 一般来说,是内存与磁盘之间的输入输出(狭义 ...
- 14 Java面试之 IO 流技术
一.IO 是什么意思? data source 是什么意思? 答: IO:Input输入 Output输出; data source:数据源 二.字节流和字符流有什么区别?输入流和输出流有什么区别? ...
- io流技术java_技术文章-java中的IO流
1.File类 Java中对文件有操作时,可以实例化一个File对象,将文件路径利用这样的形式赋给File对象. File f = new File(filePath); File类的基本操作包括: ...
- java基础48 IO流技术(序列流)
本文知识点目录: 1.SequenceInputStream序列流的步骤 2.实例 3.附录(音乐的切割与合并) 1.SequenceInputStream序列流的步骤 1.找到目标文 ...
- java使用io流技术替换LOL英雄的皮肤
需求:没钱买LOL英雄皮肤,又要装逼!又不封号! 实现: Skin using = wantSkins.get(temp);Delete a = new Delete(lolpath + " ...
最新文章
- 有趣的MS Live Labs
- BOOST_VMD_GET_TYPE宏相关的测试程序
- 无法确定域的标识_标识标牌设计的基本要求:虽然是基本要求,你未必也都知道哦...
- “CSDN开发助手”:【必备插件 · 安装与使用教程】
- extjs Grid (二)
- linux服务器重启为啥重新新增端口,Linux服务器上新增开放端口号
- 软件详细设计说明书_软件测试的基本理论 笔记
- IMWeb Conf2018 Native跨端融合总结
- 飞机大战(微信小游戏)
- linux校园网设计方案,linux在校园网的应用方案.doc
- 两种异步时钟同步化区别分析
- 如何使用虚拟机下载和安装Centos
- Kinect-v2 Examples with MS-SDK Doc(Chinese documents)
- SqlServer 2008出现远程过程调用失败,错误代码[Ox800706be]
- vlan间路由的实现(思科模拟器)
- 一场技术人的年终盛典:9个老兵对2016年总结与思考
- 整数规划学习笔记(一)
- 《悟透JavaScript》之 甘露模型二
- Unrecognised tag: snapshotPolicy (position: START_TAG seen
- middel在c语言中的作用,Middle和medium的区别