Java学习笔记28(IO字节流)
IO定义:
写:Output操作:将内存上的数据持久化 到设备上,这个动作称为输出;
读:Input操作:把硬盘上的东西读取到内存,这个动作称为输入; 这两种操作称为IO流
IO流可以分为两类:字节流 字符流
字节流
输入和输出:参照物,都是java程序来参照
字节时输入流类:InputStream 字节输出流类:OutputStream
字节输入流类和字节输出流类都是抽象类,所以只能通过子类来创建实例化对象。示例:输出流
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; public class Demo2 {
public static void main(String[] args) throws IOException {
//通过子类FileOutputStream类建立输出流对象
OutputStream out=new FileOutputStream("d:\\a.txt");
//调用OutputStream的write(int b)方法,写入字节数据到磁盘上的文件
out.write();
byte[] b={'A','B','C'};
//写入字节数组write(byte[] b)
out.write(b);
//write(byte[] b,int off,int len)写入字节数据,从x下标开始,写入数据的个数
out.write(b,,);
//释放资源
out.close();
}
}
输入流:
package com.zs.Demo; import java.io.FileInputStream;
import java.io.IOException; public class Demo3 {
public static void main(String[] args) {
try {
fun1();
fun2();
fun3();
} catch (IOException e) {
e.printStackTrace();
}
} private static void fun3() throws IOException {
FileInputStream in=new FileInputStream("d:\\a.txt");
byte[] b=new byte[1024];
int len=0;
while((len=in.read(b,0,2))!=-1){
//字符数组转字符串
System.out.println(new String(b,0,len));
/* dA
BC
C*/
}
in.close();
} private static void fun2() throws IOException {
FileInputStream in=new FileInputStream("d:\\a.txt");
byte[] b=new byte[1024];
int len=0;
while((len=in.read(b))!=-1){
//字符数组转字符串
System.out.println(new String(b,0,len));//dABCC
}
in.close();
}
private static void fun1() throws IOException {
FileInputStream in=new FileInputStream("d:\\a.txt");
//read()从输入流中读取数据的下一个字节;当没有字节时返回-1;
int len=0;
while((len=in.read())!=-1){
System.out.println((char)len);//d A B C C
}
in.close();
}
}
续写:
public class Demo4 {
public static void main(String[] args) throws IOException {
File f=new File("d:\\a.txt");
FileOutputStream fou=new FileOutputStream(f,true);
fou.write("hello\r\n".getBytes());// \r\n回车换行,getBytes()将字符串转字节数组
fou.close();
}
}//如果文件存在,不会覆盖,会把内容追加到文件内容后面
异常处理:
package com.zs.Demo; import java.io.FileOutputStream;
import java.io.IOException; public class Demo5 {
public static void main(String[] args) {
FileOutputStream fou=null;
try {
fou=new FileOutputStream("d:\\a.txt");
fou.write("java".getBytes());
} catch (IOException e) {
System.out.println(e);
throw new RuntimeException("文件写入失败");
}finally {
try {
if (fou != null) {
fou.close();
}
} catch (IOException e) {
throw new RuntimeException("释放资源失败");
}
}
}
}
复制文件:
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class CopyFile {
public static void main(String[] args) {
FileInputStream fin=null;
FileOutputStream fou=null;
try {//在main方法中,尽量使用try语句,其他方法中用throw
fin=new FileInputStream("d:\\a.txt");
fou=new FileOutputStream("e:\\a.txt");
//首先读取d盘下的a文件内容到内存
int len=0;
while((len=fin.read())!=-1){
fou.write(len);
}
} catch (IOException e) {
throw new RuntimeException("文件复制失败");
}finally {
if (fou!=null){
try {
fou.close();//先打开的后关闭,后打开的先关闭,这里先关闭输出流,
} catch (IOException e) {
throw new RuntimeException("输出流资源释放失败");
}finally {
if (fin!=null){
try {
fin.close();
} catch (IOException e) {
throw new RuntimeException("输入流资源释放失败");
} }
}
}
}
}
}
上一个方法中,一次只复制一字节的文件,当文件太大时,复制速度太慢,如下:用上面的代码复制一个视频,一字节一字节复制
package com.zs.Demo2; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class CopyFile {
public static void main(String[] args) {
FileInputStream fin=null;
FileOutputStream fou=null;
try {//在main方法中,尽量使用try语句,其他方法中用throw
fin=new FileInputStream("d:\\1.mp4");
fou=new FileOutputStream("e:\\1.mp4");
//首先读取d盘下的a文件内容到内存
int len=0;
long l = System.currentTimeMillis();
while((len=fin.read())!=-1){
fou.write(len);
}
long l1 = System.currentTimeMillis();
System.out.println(l1-l);//158014 可以发现,复制了158秒,速度很慢
} catch (IOException e) {
throw new RuntimeException("文件复制失败");
}finally {
if (fou!=null){
try {
fou.close();//先打开的后关闭,后打开的先关闭,这里先关闭输出流,
} catch (IOException e) {
throw new RuntimeException("输出流资源释放失败");
}finally {
if (fin!=null){
try {
fin.close();
} catch (IOException e) {
throw new RuntimeException("输入流资源释放失败");
}
}
}
}
}
}
}
为了提高速度,采用字节数组,
package com.zs.Demo2; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class CopyFileByByte {
public static void main(String[] args) {
FileInputStream fin=null;
FileOutputStream fou=null; try {
fin=new FileInputStream("d:\\1.mp4");
fou=new FileOutputStream("e:\\1.mp4");
int len=0;
byte[] b=new byte[1024];//这里的意思是一次复制1kb,也可以把数组大小改变1MB(1024*1024)
long l = System.currentTimeMillis();
while((len=fin.read(b))!=-1){
fou.write(b);
}
long l1 = System.currentTimeMillis();
System.out.println(l1-l);//325 只要325毫秒就复制好了
} catch (IOException e) {
throw new RuntimeException("复制失败");
}finally {
if (fou!=null){
try {
fou.close();
} catch (IOException e) {
throw new RuntimeException("释放资源失败");
}finally {
if (fin!=null){
try {
fin.close();
} catch (IOException e) {
throw new RuntimeException("释放资源失败");
}
}
}
}
}
}
}
Java学习笔记28(IO字节流)的更多相关文章
- java学习笔记之IO编程—字节流和字符流
1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...
- Java学习笔记之 IO包 字节流
IO包最重要的五个类和一个接口 File/OutputStream/InputStream(字节流)/Writer/Reader(字符流) 一个接口:Serializable File类: 字节流 ...
- 【原】Java学习笔记033 - IO
package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:继承关系中爷 ...
- Java学习笔记之——IO
一. IO IO读写 流分类: 按照方向:输入流(读),输出流(写) 按照数据单位:字节流(传输时以字节为单位),字符流(传输时以字符为单位) 按照功能:节点流,过滤流 四个抽象类: InputStr ...
- java学习笔记之IO编程—打印流和BufferedReader
1.打印流(PrintWriter) 想要通过程序实现内容输出,其核心一定是要依靠OutputStream类,但是OutputStream类有一个最大缺点,就是这个类中的输出操作功能有限,所有的数据一 ...
- java学习笔记之IO编程—内存流、管道流、随机流
1.内存操作流 之前学习的IO操作输入和输出都是从文件中来的,当然,也可以将输入和输出的位置设置在内存上,这就需要用到内存操作流,java提供两类内存操作流 字节内存操作流:ByteArrayOutp ...
- Java学习笔记37(字节流)
输出:程序到文件 输入:文件到程序 字节输出流:OutputStream类 作用:在java程序中写文件 这个类是抽象类,必须使用它的子类 方法: 写入: package demo; import j ...
- Java学习笔记-10.io流
1.输入流,只能从中读取数据,而不能向其写出数据.输出流,只能想起写入字节数据,而不能从中读取. 2.InputStream的类型有: ByteArrayInputStream 包含一个内存缓冲区,字 ...
- java学习笔记之IO编程—对象序列化
对象序列化就是将内存中保存的对象以二进制数据流的形式进行处理,可以实现对象的保存或网络传输. 并不是所有的对象都可以被序列化,如果要序列化的对象,那么对象所在的类一定要实现java.io.Serial ...
随机推荐
- leetcode-algorithms-28 Implement strStr()
leetcode-algorithms-28 Implement strStr() mplement strStr(). Return the index of the first occurrenc ...
- windows下进程间通信与线程间通信
进程间通信: 1.文件映射(Memory-Mapped Files) 文件映射(Memory-Mapped Files)能使进程把文件内容当作进程地址区间一块内存那样来对待.因此,进程不必使用文件I/ ...
- PostgreSQL主备流复制机制
原文出处 http://mysql.taobao.org/monthly/2015/10/04/ PostgreSQL在9.0之后引入了主备流复制机制,通过流复制,备库不断的从主库同步相应的数据,并在 ...
- SpringBoot系列之jar包转war包
1.修改pom,将打包方式改为war包 2.dependencides中配置外部tomcat <!--因配置外部TOMCAT 而配置--> <dependency> <g ...
- MySql习题和答案
MySQL测试题 一.表关系请创建如下表,并创建相关约束 二.操作表 1.自行创建测试数据 2.查询“生物”课程比“物理”课程成绩高的所有学生的学号.ps:针对的是自己的生物成绩比物理成绩高,再把符合 ...
- IDEA上传一个项目到github
IDEA上传一个项目到github 只要3步 1. 2. 3. 4. 5.查看页面 上传成功... 详情: https://blog.csdn.net/qq_27093465/article/d ...
- InnoDB存储引擎的 B+ 树索引
B+ 树是为磁盘设计的 m 叉平衡查找树,在B+树中,所有的记录都是按照键值的大小,顺序存放在同一层的叶子节点上,各叶子节点组成双链表.叶节点是数据,非叶节点是索引. 首先,需要清楚:B+ 树索引并不 ...
- Win10系列:JavaScript综合实例4
实现主页面和分类页面的之后,最后来看一下菜肴页面的实现,这个页面用于详细介绍某项菜肴或主食,如名称.图片和具体做法等.在pages文件夹里面添加一个名为foodDetail的文件夹,并在foodDet ...
- centos6.5 安装PHP7.0支持nginx
1.安装PHP所需要的扩展 yum -y install libxml2 libxml2-devel openssl openssl-devel bzip2 bzip2-devel ...
- flask+apache+mod-wsgi部署遇到的坑
首先,看到这种方式部署,我也有疑问,为什么不用nginx,gunicorn.接手的项目,就先按照前人思路run起来. 线上使用ubuntu系统,apache2,而给我玩耍的测试机是centos6.5, ...