一、流的分类:
* 1.操作数据单位:字节流、字符流
* 2.数据的流向:输入流、输出流
* 3.流的角色:节点流、处理流
*
二、流的体系结构
* 抽象基类               节点流(或文件流)                                            缓冲流(处理流的一种)
* InputStream          FileInputStream (read(byte[] buffer))                   BufferedInputStream (read(byte[] buffer))
* OutputStream      FileOutputStream (write(byte[] buffer,0,len)          BufferedOutputStream (write(byte[] buffer,0,len) / flush()
* Reader                 FileReader (read(char[] cbuf))                              BufferedReader (read(char[] cbuf) / readLine())
* Writer                    FileWriter (write(char[] cbuf,0,len)                        BufferedWriter (write(char[] cbuf,0,len) / flush()

三.案例

import com.sun.xml.internal.ws.policy.privateutil.PolicyUtils;
import org.junit.Test; import java.awt.geom.CubicCurve2D;
import java.io.*;
import java.nio.file.FileAlreadyExistsException;
import java.security.PublicKey; public class Filetest { @Test // 读文件
public void test1() {
FileReader fr = null;
try{
//1. 实例化File类的对象,指明要操作的文件
File file = new File("test");
//2. 提供具体的流
fr = new FileReader(file);
//3.数据的读入
// read() 返回读入的一个字符,如果达到文件的尾部,返回-1
int data = fr.read();
while (data !=-1){
System.out.print((char)data);
data = fr.read();
}
}catch (IOException e){
e.printStackTrace();
}finally {
//4. 关闭文件
if (fr !=null){
try {
fr.close();
}catch (IOException e) {
e.printStackTrace();
}
}
} }
// 对read() 操作升级,使用read的重载方法
@Test // 读文件
public void test2(){
FileReader fr = null;
try {
//1. 实例化File 类的对象,指明要操作的文件
File file = new File("test2");
//2. 提供具体的文件流
fr = new FileReader(file);
//3. 读入操作
// read(char [] cbuf) : 返回每次读入cbuf 数组中的字符的个数,如果达到文件末尾 返回-1
char[] cbuf = new char[5];
int len;
while((len=fr.read(cbuf)) !=-1){
String str = new String(cbuf,0,len);
System.out.print(str);
}
}catch (IOException e){
e.printStackTrace();
}finally {
// 关闭资源
if(fr !=null){
try{
fr.close();
}catch (IOException e){
e.printStackTrace();
}
} } }
@Test //读写文件
public void test3(){
FileReader fr = null;
FileWriter fw= null;
// 读写文件
//创建文件类对象
try {
File srcFile = new File("test");
File destFile = new File("test3");
//
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
// 读取原文件写入到另一个文件
char [] cbuf = new char [10];
int len;
while ((len = fr.read(cbuf)) !=-1){
fw.write(cbuf,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
// 关闭文件
if (fr !=null){
try {
fr.close();
}catch (IOException e){
e.printStackTrace();
}
}
if (fw !=null){
try {
fw.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
@Test//字节流方式读取文件
public void test4(){
FileInputStream fis = null;
try {
// 1. 造文件
File file = new File("1.png");
//2. 造流
fis = new FileInputStream(file);
//3.
byte[] bt = new byte[5];
int len;
while ((len=fis.read(bt)) !=-1){
//
String str = new String(bt,0,len);
System.out.println(str);
}
}catch (IOException e){
e.printStackTrace();
}finally {
// 关闭
if(fis !=null){
try {
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
@Test // 复制图片
public void test5(){
//
FileInputStream fis =null;
FileOutputStream fos =null;
try{
// 造文件
File srcFile = new File("1.png");
File descFile = new File("2.png");
//造流
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(descFile);
//
byte [] bt = new byte[1024];
int len;
while ((len=fis.read(bt))!=-1){
fos.write(bt,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
// 关闭操作
if (fis !=null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
if (fos !=null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
} }
public void copyFile(String srcPath,String descPath){ FileInputStream fis =null;
FileOutputStream fos =null;
try{
// 造文件
File srcFile = new File(srcPath);
File descFile = new File(descPath);
//造流
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(descFile);
//
byte [] bt = new byte[1024];
int len;
while ((len=fis.read(bt))!=-1){
fos.write(bt,0,len);
}
}catch (IOException e){
e.printStackTrace();
}finally {
// 关闭操作
if (fis !=null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
if (fos !=null){
try{
fis.close();
}catch (IOException e){
e.printStackTrace();
}
}
} }
}

6. java IO 流的更多相关文章

  1. Java:IO流与文件基础

    Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...

  2. java IO流详解

    流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  3. Java IO流学习总结

    Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...

  4. 揭开Java IO流中的flush()的神秘面纱

    大家在使用Java IO流中OutputStream.PrintWriter --时,会经常用到它的flush()方法. 与在网络硬件中缓存一样,流还可以在软件中得到缓存,即直接在Java代码中缓存. ...

  5. java io流 对文件夹的操作

    java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...

  6. Java IO流题库

    一.    填空题 Java IO流可以分为   节点流   和处理流两大类,其中前者处于IO操作的第一线,所有操作必须通过他们进行. 输入流的唯一目的是提供通往数据的通道,程序可以通过这个通道读取数 ...

  7. Java IO流总结

    Java IO流分类以及主要使用方式如下: IO流 |--字节流 |--字节输入流 InputStream: int read();//一次读取一个字节 int read(byte[] bys);// ...

  8. java io流 运行错误时,保存异常到文件里面

    java io流 运行错误时,保存异常到文件里面 下面这个实例,运行后,输入数字,为正确,如果输入字符串,则报错,保存错误信息 //运行错误时,保存异常到文件里面 //下面这个实例,运行后,输入数字, ...

  9. java io流 创建文件、写入数据、设置输出位置

    java io流 创建文件 写入数据 改变system.out.print的输出位置 //创建文件 //写入数据 //改变system.out.print的输出位置 import java.io.*; ...

  10. java io流 数据流传输

    java io流 数据流传输 把这段当公式用就可以了 //数据流传输 import java.io.*; public class Index{ public static void main(Str ...

随机推荐

  1. 【LeetCode】883. Projection Area of 3D Shapes 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...

  2. 【LeetCode】473. Matchsticks to Square 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 回溯法 日期 题目地址:https://leetco ...

  3. [源码解析] PyTorch 分布式之弹性训练(4)---Rendezvous 架构和逻辑

    [源码解析] PyTorch 分布式之弹性训练(4)---Rendezvous 架构和逻辑 目录 [源码解析] PyTorch 分布式之弹性训练(4)---Rendezvous 架构和逻辑 0x00 ...

  4. TensorFlow.NET机器学习入门【6】采用神经网络处理Fashion-MNIST

    "如果一个算法在MNIST上不work,那么它就根本没法用:而如果它在MNIST上work,它在其他数据上也可能不work". -- 马克吐温 上一篇文章我们实现了一个MNIST手 ...

  5. 【机器学习】Pandas库练习-获取yahoo金融苹果公司的股票数据

    # 获取yahoo金融苹果公司的股票数据. # 1.分析拉取的数据,找到收盘数据列的列名. # 2.绘制收盘价格柱状图. # 3.分析拉取的数据涨跌率,股价移动平均和波动率. # 4. 找出开盘价和收 ...

  6. Categorical Reparameterization with Gumbel-Softmax

    目录 概 主要内容 Gumbel distribution Jang E., Gu S. and Poole B. Categorical reparameterization with gumbel ...

  7. <数据结构>图的构建与基本遍历方法

    目录 建立一个图 邻接矩阵 邻接表 深度优先遍历(DFS) 具体步骤: 第一部分:给定结点u,遍历u所在的连通块的所有结点 第二部分:对图G所有结点进行第一部分的操作,即遍历了图的所有连通分量 伪代码 ...

  8. CentOS7.6下安装Redis5.0.7

    此次安装是在CentOS7下安装Redis5.0.7 一.首先准备Redis安装包 这里下载的是 redis-5.0.7.tar.gz 安装包,并将其直接放在了 root ⽬录下 压缩包下载地址:ht ...

  9. Elasticsearch集群安装Version6.2.2

    Elasticsearch集群安装, 基于Elasticsearch6.2.2版本, 在Linux上安装Elasticsearch集群. 1.安装规划 IP HostName Service Mast ...

  10. Swoole 中使用 PDO 连接池、Redis 连接池、Mysqli 连接池

    连接池使用说明 所有连接池的实现均基于 ConnectionPool 原始连接池: 连接池的底层原理是基于 Channel 的自动调度: 开发者需要自己保证归还的连接是可重用的: 若连接不可重用,需要 ...