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字节流)的更多相关文章

  1. java学习笔记之IO编程—字节流和字符流

    1. 流的基本概念 在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操 ...

  2. Java学习笔记之 IO包 字节流

    IO包最重要的五个类和一个接口 File/OutputStream/InputStream(字节流)/Writer/Reader(字符流) 一个接口:Serializable   File类: 字节流 ...

  3. 【原】Java学习笔记033 - IO

    package cn.temptation; public class Sample01 { public static void main(String[] args) { // 需求:继承关系中爷 ...

  4. Java学习笔记之——IO

    一. IO IO读写 流分类: 按照方向:输入流(读),输出流(写) 按照数据单位:字节流(传输时以字节为单位),字符流(传输时以字符为单位) 按照功能:节点流,过滤流 四个抽象类: InputStr ...

  5. java学习笔记之IO编程—打印流和BufferedReader

    1.打印流(PrintWriter) 想要通过程序实现内容输出,其核心一定是要依靠OutputStream类,但是OutputStream类有一个最大缺点,就是这个类中的输出操作功能有限,所有的数据一 ...

  6. java学习笔记之IO编程—内存流、管道流、随机流

    1.内存操作流 之前学习的IO操作输入和输出都是从文件中来的,当然,也可以将输入和输出的位置设置在内存上,这就需要用到内存操作流,java提供两类内存操作流 字节内存操作流:ByteArrayOutp ...

  7. Java学习笔记37(字节流)

    输出:程序到文件 输入:文件到程序 字节输出流:OutputStream类 作用:在java程序中写文件 这个类是抽象类,必须使用它的子类 方法: 写入: package demo; import j ...

  8. Java学习笔记-10.io流

    1.输入流,只能从中读取数据,而不能向其写出数据.输出流,只能想起写入字节数据,而不能从中读取. 2.InputStream的类型有: ByteArrayInputStream 包含一个内存缓冲区,字 ...

  9. java学习笔记之IO编程—对象序列化

    对象序列化就是将内存中保存的对象以二进制数据流的形式进行处理,可以实现对象的保存或网络传输. 并不是所有的对象都可以被序列化,如果要序列化的对象,那么对象所在的类一定要实现java.io.Serial ...

随机推荐

  1. mac中 hosts地址

    /etc/hosts 拉出hosts文件,修改之后再拉进去

  2. 突破本地离线存储5M限制的JS库localforage简介

    http://www.zhangxinxu.com/wordpress/2018/06/js-localforage-localstorage-indexdb/

  3. Leetcode 109

    //这种也是空间复杂度为O(K)的解法,就是边界有点难写class Solution { public: vector<int> getRow(int rowIndex) { vector ...

  4. [LightOJ 1265] Island of Survival

    Island of Survival You are in a reality show, and the show is way too real that they threw into an i ...

  5. 一、I/O操作(流的概念)

    一.流(Stream) 所谓流(Stream),就是一系列的数据. 当不同的介质之间有数据交互的时候,java就会使用流来实现. 数据源可以使文件,还可以是数据库,网络,甚至是其他的程序 不如读取文件 ...

  6. JQuary中的FullPage属性的用法

    $(document).ready(function(){ //常用方法    //$.fn.fullpage.moveSectionUp()   //向上滚动一页 //$.fn.fullpage.m ...

  7. ActiveMQ producer 提交事务时突然宕机,会发生什么

    producer 在提交事务时,发生宕机,commit 的命令没有发送到 broker,这时会发生什么? ActiveMQ 开启事务发送消息的步骤: session.getTransactionCon ...

  8. JAVA工程师面试常见问题集锦

    集锦一: 一.面试题基础总结 1. JVM结构原理.GC工作机制详解 答:具体参照:JVM结构.GC工作机制详解     ,说到GC,记住两点:1.GC是负责回收所有无任何引用对象的内存空间. 注意: ...

  9. vue 中使用 Toast弹框

    import { ToastPlugin,ConfirmPlugin,AlertPlugin} from 'vux' Vue.use(ToastPlugin) Vue.use(ConfirmPlugi ...

  10. vue 项目中的坑 在项目中遇到 持续更新ing

    1.vue2.0 不支持 v-html 后绑定的内容使用过滤,可是有时候过滤必须使用-----------解决:通过methods中定义方法 然后 v-html='myMethods(string)' ...