io 流简述:

i->inputStream(输入流) o->outputStream  (输出流)

IO流简单来说就是Input和Output流,IO流主要是用来处理设备之间的数据传输,Java对于数据的操作都是通过流实现,而java用于操作流的对象都在IO包中。

java 中的流是一个数据流,即可以在流中读取数据,也可以在流中写入数据。

source ——>inputStream/Reader——>program

program——>outputStream/writer——>target

inputStream/Reader 是与数据源相关,outputStream/writer与接收数据的目标相关。

IO流常用的基类:

* InputStream , OutputStream

字符流的抽象基类:

* Reader , Writer

由上面四个类派生的子类名称都是以其父类名作为子类的后缀:

如:FileReader和FileInputStream    FileWriter和FileOutputStream

File类是IO包中唯一代表磁盘文件本身的对象。通过File来创建,删除,重命名文件。File类对象的主要作用就是用来获取文本本身的一些信息。如文本的所在的目录,文件的长度,读写权限等等。(有的需要记忆,比如isFile(),isDirectory(),exits();有的了解即可。使用的时候查看API)

字符流简介:

* 字符流中的对象融合了系统默认的编码表,一般都是GBK编码,字符流只用来处理文本数据,而字节流用来处理媒体数据。数据最常见的表现形式是文件,字符流用于操作文件的子类,一般是FileWriter和FileReader.

读写:

* 字符流的每次操作后后需要flush()刷新,用完需要关闭close()流,使用对象抛出IO异常

实例:

public static void main(String[] args) {

        String filePath = "s://io.txt";
File file = new File(filePath);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileWriter write = null;
try {
write = new FileWriter(file);
write.write("阿萨德");//写入数据
write.flush(); // 刷新
} catch (IOException e) {
e.printStackTrace();
}finally{
if(write!=null){
try {
write.close(); //关闭流
} catch (IOException e) {
e.printStackTrace();
}
}
} }

  

public static void main(String[] args) {

        String filePath = "s://io.txt";
File file = new File(filePath);
FileReader read = null;
try {
if (!file.exists()) {
throw new Exception("文件不存在");
}
read = new FileReader(file);
char[] buf = new char[100];
int i;
while ((i = read.read(buf)) != -1) {
read.read(buf, 0, i);
}
System.out.println(new String(buf));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
} }
}

缓冲区:

 字符流的缓冲区:BufferedReader和BufferedWreiter

缓冲区是为了提高流读写效率而出现的

读取流缓冲区提供了一个一次读一行的方法readline,方便对文本数据的获取。
readline()只返回回车符前面的字符,不返回回车符。如果是复制的话,必须加入newLine(),
写入回车符newLine()是java提供的多平台换行符写入方法。

 使用缓冲区读取操作:

String filePath = "s://io.txt";
File file = new File(filePath);
FileReader read = null;
// 写入 FileWrite write = null;
try {
if (!file.exists()) {
throw new Exception("文件不存在");
}
read = new FileReader(file);
BufferedReader br = new BufferedReader(read);
String readLine = br.readLine();
System.out.println(readLine);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (read != null) {
try {
read.close();
} catch (IOException e) {
e.printStackTrace();
}
} }

 操作写入:

String filePath = "s://io.txt";
File file = new File(filePath);
FileWriter write = null;
try {
if (!file.exists()) {
throw new Exception("文件不存在");
}
write = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(write);
bw.write("万恶硬功夫业务俄国防");
bw.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (write != null) {
try {
write.close();
} catch (IOException e) {
e.printStackTrace();
}
} }

字节流:

1、字节流和字符流的基本操作是相同的,但是要想操作媒体流就需要用到字节流。

2、字节流因为操作的是字节,所以可以用来操作媒体文件。(媒体文件也是以字节存储的)

3、读写字节流:InputStream   输入流(读)和OutputStream  输出流(写)

4、字节流操作可以不用刷新流操作。

5、InputStream特有方法:  int available();//返回文件中的字节个数

 简单实例 图片复制:

        String filePath = "s://Desert.jpg";
String filePath2 = "s://test.jpg";
File file = new File(filePath);
File file2 = new File(filePath2);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file);
if (!file2.exists()) {
file2.createNewFile();
}
byte[] buf = new byte[1024];
fos = new FileOutputStream(file2);
int i;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}

 

io 的一些简单说明及使用的更多相关文章

  1. 服务器端IO模型的简单介绍及实现

    https://mp.weixin.qq.com/s?src=3&timestamp=1541726441&ver=1&signature=xPSye3v7miF7aVeLHb ...

  2. 利用socket.io+nodejs打造简单聊天室

    代码地址如下:http://www.demodashi.com/demo/11579.html 界面展示: 首先展示demo的结果界面,只是简单消息的发送和接收,包括发送文字和发送图片. ws说明: ...

  3. 服务器端IO模型的简单介绍及实现 阻塞 / 非阻塞 VS 同步 / 异步 内核实现的拷贝效率

    小结: 1.在多线程的基础上,可以考虑使用"线程池"或"连接池","线程池"旨在减少创建和销毁线程的频率,其维持一定合理数量的线程,并让空闲 ...

  4. IO流的简单实现

    IO流的几种实现方式 学习目标: 例题: 字节输出流 字节输入流 字符输入流 字符输出流 学习目标: 熟练掌握IO流的基本实现方式 例题: 字节输出流 代码如下: public class Outpu ...

  5. Socket.IO聊天室~简单实用

    小编心语:大家过完圣诞准备迎元旦吧~小编在这里预祝大家元旦快乐!!这一次要分享的东西小编也不是很懂啊,总之小编把它拿出来是觉地比较稀奇,而且程序也没有那么难,是一个比较简单的程序,大家可以多多试试~ ...

  6. 几种服务器端IO模型的简单介绍及实现

    一些概念: 同步和异步 同步和异步是针对应用程序和内核的交互而言的,同步指的是用户进程触发I/O操作并等待或者轮询的去查看I/O操作是否就绪,而异步是指用户进程触发I/O操作以后便开始做自己的事情,而 ...

  7. java IO文件操作简单基础入门例子,IO流其实没那么难

    IO是JAVASE中非常重要的一块,是面向对象的完美体现,深入学习IO,你将可以领略到很多面向对象的思想.今天整理了一份适合初学者学习的简单例子,让大家可以更深刻的理解IO流的具体操作. 1.文件拷贝 ...

  8. Java开发笔记(九十一)IO流处理简单的数据压缩

    前面介绍的文件I/O,不管是写入文本还是写入对象,文件中的数据基本是原来的模样,用记事本之类的文本编辑软件都能浏览个大概.这么存储数据,要说方便确实方便,只是不够经济划算,原因有二:其一,写入的数据可 ...

  9. java的IO,AIO简单对比

    以下内容转载lzzzl Channel 通道 Buffer 缓冲区 Selector 选择器 其中Channel对应以前的流,Buffer不是什么新东西,Selector是因为nio可以使用异步的非堵 ...

随机推荐

  1. java.lang.ClassNotFoundException: org.apache.log4j.Logger 异常

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'd ...

  2. BP neural network optimized by PSO algorithm on Ammunition storage reliability prediction 阅读笔记

    1.BP neural network optimized by PSO algorithm on Ammunition storage reliability prediction 文献简介文献来源 ...

  3. 生成器的认识及其思考:VAE, GAN, Flow-based Invertible Model

    生成器对应于认知器的逆过程. 这一切的起源都是当初一个极具启发性的思想:Sleep-wake algorithm——人睡眠时整理记忆做梦,是一个生成的过程,即通过最终的识别结果企图恢复接收到的刺激,当 ...

  4. Maskrcnn遇到的坑

    第一个要讲maskrcnn 中keras 升到2.1 然后 在线程问题上要把workers设置成1,是否使用线程设置成false 然后调用模型的时候要把模型和加载文件放到一个目录下

  5. 网络编程-day2

    1 网络通信协议 Tcp udp的区别 重点(*****) TCP(Transmission Control Protocol)可靠的.面向连接的协议(eg:打电话).传输效率低全双工通信(发送缓存& ...

  6. Hibernate的增删改查(事务)

    Hibernate的事务: 1. 设置事务隔离级别都是用  自己进行存储的 二进制      十进制 read uncommitted   读未提交        0001         1 rea ...

  7. python中一些传参事情

    #一个参数的传参 def hello(a):    print(a+'王彦军你好')hello('hello')''' #2个参数的 def ab(a,b):    print(a+'你好')    ...

  8. node-express-1

    安装: express v4.0以后的安装: npm install express-generator -g 建立项目 express -t ejs blog 安装依赖 cd blog && ...

  9. vue中父子组件的通信

    1.父组件向子组件传递数据 父组件传递:data = parent.data 子组件接收props: {data:{}} 2.子组件向父组件传递数据(https://vuefe.cn/v2/guide ...

  10. js 两数的最大公约数

    function gcd(a,b){ if (b == 0){ return a; } var r = parseInt(a % b) ; return gcd(b, r);}gcd(12,5);