毕向东_Java基础视频教程第20天_IO流(15~17)
第20天-15-IO流(打印输出流)
打印输出流:PrintWriter与PrintStream
两者的区别:Since JDK 1.4 it's possible to specify the character encoding for a PrintStream. Thus, the differences between PrintStream and PrintWriter are only about auto flushing behavior and that a PrintStream cannot wrap a Writer.
package bxd; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter; /*
打印输出流: 该流提供了打印方法, 可以将各种类型的数据都原样打印 PrintStream构造函数可以接受的参数类型:
1. File对象 File
2. 字符串路径 String
3. 字节输出流 OutputStream PrintWriter构造函数可以接受的参数类型(由于可接受的参数类型更多, 因而更常用一些):
1. File对象 File
2. 字符串路径 String
3. 字节输出流 OutputStream
4. 字符输出流 Writer
*/
public class PrintStreamDemo {
public static void main(String[] args) throws IOException { BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out, true); String line = null;
while ((line = bufr.readLine()) != null) {
if ("over".equals(line)) break;
out.println(line.toUpperCase());
}
out.close();
bufr.close();
}
}
第20天-16-IO流(合并流-SequenceInputStream)
package bxd; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector; public class SequenceDemo { public static void main(String[] args) throws IOException { Vector<FileInputStream> v = new Vector<FileInputStream>();
v.add(new FileInputStream("info.txt"));
v.add(new FileInputStream("list.txt"));
Enumeration<FileInputStream> en = v.elements(); SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("uni.txt"); byte[] buf = new byte[1024];
int len = 0;
while ((len = sis.read(buf)) != -1) {
fos.write(buf, 0, len);
} fos.close();
sis.close();
}
}
第20天-17-IO流(切割文件)
package bxd; 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.Iterator; public class SplitFile {
public static void splitFile() throws IOException {
FileInputStream fis = new FileInputStream("ts.png");
FileOutputStream fos = null; byte[] bytes = new byte[600 * 1024];
int len = 0;
int count = 1;
while ((len = fis.read(bytes)) != -1) {
fos = new FileOutputStream((count++) + ".part");
fos.write(bytes, 0, len);
fos.close();
}
fis.close();
} public static void merge() throws IOException { ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for (int x = 1; x <= 4; x++) al.add(new FileInputStream(x + ".part")); final 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("rets.png"); byte[] bytes = new byte[1024];
int len = 0;
while ((len = sis.read(bytes)) != -1) fos.write(bytes, 0, len); fos.close();
sis.close();
} public static void main(String[] args) throws IOException {
merge();
}
}
毕向东_Java基础视频教程第20天_IO流(15~17)的更多相关文章
- 毕向东_Java基础视频教程第20天_IO流(7~10)
第20天-07-IO流(递归) package bxd; import java.io.File; public class FileDemo3 { // 非递归打印 public static vo ...
- 毕向东_Java基础视频教程第20天_IO流(11~14)
第20天-11-IO流(Properties简述) .properties是一种主要在Java相关技术中用来存储应用程序的可配置参数的文件的文件扩展名.它们也可以存储用于国际化和本地化的字符串,这种文 ...
- 毕向东_Java基础视频教程第20天_IO流(5~6)
第20天-05-IO流(文件列表一) static File[] listRoots() List the available filesystem roots. String[] list() Re ...
- 毕向东_Java基础视频教程第20天_IO流(1~4)
第20天-01-IO流(File概述) File类: 用来将文件或者文件夹封装成对象, 方便进行操作. File对象可以作为参数, 传递给流对象的构造函数. 流对象不能操作文件夹; 流对象不能操作文件 ...
- 毕向东_Java基础视频教程第19天_IO流(20~22)
第19天-20-IO流(改变标准输入输出设备) static void setIn(InputStream in) Reassigns the "standard" input s ...
- 毕向东_Java基础视频教程第19天_IO流(01~05)
第19天-01-IO流(BufferedWriter) 字符流的缓冲区 缓冲区的出现提高了对数据的读写效率. 对应类缓冲区要结合流才可以使用. BufferedWriter BufferedReade ...
- 毕向东_Java基础视频教程第19天_IO流(06~10)
第19天-06-IO流(装饰设计模式) 装饰设计模式: 当想要对已有的对象进行功能增强时, 可以定义类,将已有对象传入,基于已有的功能,并提供加强功能.那么这个自定义的类称为装饰类. 装饰类通常会通过 ...
- 毕向东_Java基础视频教程第19天_IO流(11~14)
第19天-11-IO流(字节流File读写操作) import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...
- 毕向东_Java基础视频教程第21天_IO流(1)
第21天-01-IO流(对象的序列化) ObjectInputStream与ObjectOutputStream 被操作的对象需要实现Serializable接口(标记接口) 非必须, 但强烈建议所有 ...
随机推荐
- Android中通过xml改变背景及文字颜色
原创文章,转载请注明出处,谢谢! 本篇主要介绍Android开发中,通过XML资源文件来设置控件在不同状态下的背景及文字颜色.关于xml改变背景及文字颜色的原理,大家可以去看一下郭霖大神的源码分析文章 ...
- Mac 10.12常用软件清单
链接: https://pan.baidu.com/s/1slds1OD 密码: 7m5t 配套教程:http://www.cnblogs.com/EasonJim/tag/mac/ 如果失效了,联系 ...
- 非常不错的app和网站
置顶: word.pdf之间相互转换的网站: https://www.addpdf.cn 很棒啊 1. Global Potplayer 这个软件简直了,播放各种视频, 无论是本地的,还是在线的,都非 ...
- Python离线安装依赖包
1.制作requirement.txt pip freeze > requirement.txt 2.下载离线Pytho安装包 pip download -r requirement.txt - ...
- ssm项目启动,加载数据库连接池时卡住
今天早上到公司启动项目的时候,加载数据库连接池时卡住,昨晚还好着呢,然后排查原因,最后发现是因为有一个mapper的xml配置文件中 <mapper namespace="com.mi ...
- linux系统挂载U盘,中文文件名乱码解决方案
本人(壮壮熊)所用系统:ubuntu 12.4 今天在使用mount指令挂在硬盘时,出现令人头疼的中文文件名乱码. 问题: 使用mount /dev/sdb1 /media指令挂在第二颗硬盘的第一个分 ...
- gulp教程之gulp中文API
1.gulp.src(globs[, options]) 1.1.说明:src方法是指定需要处理的源文件的路径,gulp借鉴了Unix操作系统的管道(pipe)思想,前一级的输出,直接变成后一级的输入 ...
- cmd sc命令进行服务操作
sc 命令可以注册.删除和查询系统服务 sc可供选择的参数有很多,这里不详细描述.只介绍简单的最基本的sc使用方式. 1. sc create 创建windows服务 eg: sc \\myserve ...
- 【转】如何在ASP.NET 2.0中定制Expression Builders
expressions是asp.net 2.0中的新特色,它可以使你在asp.net的页面里很方便的使用自定义的属性. 在ASPX页里只要使用$符号就可以访问到,你定制的属性了. 例如我们看个例子: ...
- 欢迎来到GIS思考者的博客www.gisthinker.com
我是一名GIS爱好者,这是我的个人博客,欢迎点击: GIS思考者:www.gisthinker.com