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. 『计算机视觉』Mask-RCNN_推断网络其一:总览

    在我们学习的这个项目中,模型主要分为两种状态,即进行推断用的inference模式和进行训练用的training模式.所谓推断模式就是已经训练好的的模型,我们传入一张图片,网络将其分析结果计算出来的模 ...

  2. mysql排序的中文首字母排序和自定义排序

    select * FROM organ_new where city_code = 'SZ0755' and organ_type = 'H' and state = '1' ORDER BY FIE ...

  3. Parse error: syntax error, unexpected end of file in * 的解决办法

    这个原因很简单,就是你的php语法错误. 在你的php代码种出现了<?  ?>  标准的是<?php ?>

  4. js向一个数组中插入元素的几个方法-性能比较

    向一个数组中插入元素是平时很常见的一件事情.你可以使用push在数组尾部插入元素,可以用unshift在数组头部插入元素,也可以用splice在数组中间插入元素. 但是这些已知的方法,并不意味着没有更 ...

  5. ActiveMQ producer不断发送消息,会导致broker内存耗尽吗?

    http://activemq.apache.org/my-producer-blocks.html 回答了这个问题: ActiveMQ 5.x 支持Message Cursors,它默认把消息从内存 ...

  6. hosts.allow和hosts.deny支持哪些服务

    一.背景简介 在linux上多用iptables来限制ssh和telnet,编缉hosts.allow和hosts.deny感觉比较麻烦比较少用. aix没有iptables且和linux有诸多不同, ...

  7. Eclipse导入Oracle/MySQL数库驱动包教程

    在操作数据库时除了import相关的SQL类外,还得在项目中导入数据库的驱动才能连接和操作数据库. 而数据库驱动jar包在默认Java的lib里是没有的,要自己到官网下载导入:本教程以Oracle为例 ...

  8. Matlab远程调试 转

        Matlab的调试总体分为,直接调试和间接调试.1.直接调试:(1)去掉句末的分号:(2)单独调试一个函数:将第一行的函数声明注释掉,并定义输入量,以脚本方式执行 M 文件:(3)适当地方添加 ...

  9. 架构之路:nginx与IIS服务器搭建集群实现负载均衡(三)

    参考网址:https://blog.csdn.net/zhanghan18333611647/article/details/50811980 [前言] 在<架构之路:nginx与IIS服务器搭 ...

  10. Unity中UGUI之Canvas属性解读版本二

    Canvas Render Modes(渲染模式) 1.在screen空间中渲染2.在world空间中渲染 Screen Space-Overlay 在这个渲染模式中,UI元素将在场景的上面.如果场景 ...