Java IO输入输出流 FileWriter 字符流
字节缓冲流
//为什么要使用包装流,使用包装流是为了提高读写操作的性能。
public class Packing_flowDemo {
public static void main(String[] args) throws Exception {
File file = new File("file/packing_flow.txt");
//包装流的写法,缓冲区内存大小。1024*8=8192 (byte)
// BufferedOutputStream packing = new BufferedOutputStream(new FileOutputStream(file, true));
// packing.write("大家好!你好吗?how are your !".getBytes());
// packing.close();
//包装流的读写操作。
BufferedInputStream outPacking = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int len = -1;
while ((len = outPacking.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, len));
}
}
}
public static void main(String[] args) throws IOException {//为了代码看起来美观一些,直接抛出去
File file=new File("moves/许嵩 - 素颜 - 现场版.mp3");
File file1=new File("moves/许嵩 - 素颜.mp3");
//text(file, file1);
//text2(file, file1);
//text3(file, file1);
text4(file, file1);
}
private static void text(File file,File file1) throws IOException {
//节点流的方法,一个一个字节的读和写
long begin=System.currentTimeMillis();
FileInputStream in=new FileInputStream(file);
FileOutputStream out =new FileOutputStream(file1);
int len=-1;
while((len=in.read())!=-1){
out.write(len);
}
in.close();
out.close();
System.out.println(System.currentTimeMillis()-begin);//5547毫秒
} private static void text2(File file,File file1) throws IOException {
//缓冲流的写法,一个一个字节的读和写
long begin=System.currentTimeMillis();
BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file1));
int len=-1;
while(in.read()!=-1){
out.write(len);
}
in.close();
out.close();
System.out.println(System.currentTimeMillis()-begin);//63毫秒
} private static void text3(File file,File file1) throws IOException {
//节点流的写法,一次性读取1024个字节
long begin=System.currentTimeMillis();
FileInputStream in=new FileInputStream(file);
FileOutputStream out =new FileOutputStream(file1);
int len=-1;
byte[] buffer=new byte[1024];
while((len=in.read(buffer))!=-1){
out.write(buffer,0,len);
}
in.close();
out.close();
System.out.println(System.currentTimeMillis()-begin);//38毫秒
} private static void text4(File file,File file1) throws IOException {
//缓冲流的写法,一次性读取1024个字节
long begin=System.currentTimeMillis();
BufferedInputStream in=new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out =new BufferedOutputStream(new FileOutputStream(file1));
int len=-1;
byte[] buffer=new byte[1024];
while((len=in.read(buffer))!=-1){
out.write(buffer,0,len);
}
in.close();
out.close();
System.out.println(System.currentTimeMillis()-begin);//4毫秒
}
Java IO输入输出流 FileWriter 字符流的更多相关文章
- java.IO输入输出流:过滤流:buffer流和data流
java.io使用了适配器模式装饰模式等设计模式来解决字符流的套接和输入输出问题. 字节流只能一次处理一个字节,为了更方便的操作数据,便加入了套接流. 问题引入:缓冲流为什么比普通的文件字节流效率高? ...
- java IO输入输出流中的各种字节流,字符流类
字节流字节流主要是操作byte类型数据,也byte数组为准,主要操作类就是·字节输出流:OutputStream·字节输入流:InputStream字符流在程序中一个字符等于2个字节,那么java提供 ...
- Java精选笔记_IO流(字符输入输出流、字符文件输入输出流、字符流的缓冲区)
字符流 Reader是字符输入流的基类,用于从某个源设备读取字符 Writer是字符输出流,用于向某个目标设备写入字符 字符流操作文件 字符输入流FileReader,通过此流可以从关联的文件中读取一 ...
- java io 输入输出流
数据流分类: 流序列中的数据既可以是未经加工的原始二进制数据, 也可以是经一定编码处理后符合某种格式规定的特定数据. 因此Java中的流分为两种: 1) 字节流:数据流中最小的数据单元是字节 2) 字 ...
- Java IO(四)——字符流
一.字符流 字节流提供了处理任何类型输入/输出操作的功能(因为对于计算机而言,一切都是0和1,只需把数据以字节形式表示就够了),但它们不可以直接操作Unicode字符,因为一个Unicode字符占用2 ...
- java IO的字节流和字符流及其区别
1. 字节流和字符流的概念 1.1 字节流继承于InputStream OutputStream, 1.2 字符流继承于InputStreamReader OutputStre ...
- Java IO 输入输出流 详解 (一)***
首先看个图: 这是Javaio 比较基本的一些处理流,除此之外我们还会提到一些比较深入的基于io的处理类,比如console类,SteamTokenzier,Externalizable接口,Seri ...
- Java基础(二十七)Java IO(4)字符流(Character Stream)
字符流用于处理字符数据的读取和写入,它以字符为单位. 一.Reader类与Writer类 1.Reader类是所有字符输入流的父类,它定义了操作字符输入流的各种方法. 2.Writer类是所有字符输出 ...
- java IO之字节流和字符流-Reader和Writer以及实现文件复制拷贝
接上一篇的字节流,以下主要介绍字符流.字符流和字节流的差别以及文件复制拷贝.在程序中一个字符等于两个字节.而一个汉字占俩个字节(一般有限面试会问:一个char是否能存下一个汉字,答案当然是能了,一个c ...
随机推荐
- ReactJS开发环境搭建与相关工具介绍
现在Web开发的技术几年前相比可谓变化之大.各种各样的框架,各种各样的工具,让Web开发效率更高,开发出来的效果更好.同时带来的是开发环境的复杂度相比以前是成倍的增加.ReatJS框架是现在比较流行的 ...
- Ubuntu-14.04-QT开发环境搭建-(一)
Ubuntu 14.04 QT 开发环境搭建 一 . 软件:qt-creator-linux-x86-opensource-2.7.0.binqt-everywhere-opensource-src- ...
- spring-aop + memcached 的简单实现
一般情况下,java程序取一条数据是直接从数据库中去取,当数据库达到一定的连接数时,就会处于排队等待状态,某些在一定时间内不会发生变化的数据,完全没必要每次都从数据库中去取,使用spring-aop ...
- zabbix设置sendmail发送邮件
http://blog.csdn.net/xin_yu_xin/article/details/45115723
- WP8.1通过StreamSocket连接C++服务器
注:当服务端和手机模拟器运行在一台机器时,会有奇怪错误.将服务端放在其它机器上更改客户端连接地址,运行正常.或者直接用本机modern调试也可以. 实例化一个对象 StreamSocket clien ...
- python--open用法
open/文件操作f=open('/tmp/hello','w') #open(路径+文件名,读写模式) #读写模式:r只读,r+读写,w新建(会覆盖原有文件),a追加,b二进制文件.常用模式 如:' ...
- PHP 乘法口诀表
echo "乘法口诀表<br>"; for($i=1;$i<10;$i++) { for ($j = 1; $j <= $i; $j++) printf(& ...
- MFC 打开外部EXE文件的三种方法
目前知道三种方式:WinExec,ShellExecute ,CreateProcess,别人已经总结的很好了<vc中调用其他应用程序的方法(函数) winexec,shellexecute , ...
- 【279】◀▶ Python 运算符说明
参考:Python 运算符说明 目录: 一.算术运算符 二.比较(关系)运算符 三.赋值运算符 四.位运算符 五.逻辑运算符 六.成员运算符 七.身份运算符 八.运算符优先级 一.Python 算术运 ...
- scala 在vim中的语法高亮
https://github.com/derekwyatt/vim-scala 提供了一个选择 看install手册,发现两个命令都是必须的. mkdir -p ~/.vim/{ftdetect,in ...