一、流的分类:
* 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. 晴天小猪历险记之Hill(Dijkstra优先队列优化)

    描述 这一天,他来到了一座深山的山脚下,因为只有这座深山中的一位隐者才知道这种药草的所在.但是上山的路错综复杂,由于小小猪的病情,晴天小猪想找一条需时最少的路到达山顶,但现在它一头雾水,所以向你求助. ...

  2. 替代RTD2166|CS5212直接Pin to pin兼容替代RTD2166|替代RTD2166方案

    RTD2166功能概述 RTD2166是一款DisplayPort端口到VGA转换器,成本较高,Capstone于2019年推出CS5212,直接Pin to pin兼容替代RTD2166,可用原RT ...

  3. spring练习,使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出。

    相关 知识 >>> 相关 练习 >>> 实现要求: 使用Eclipse搭建的Spring开发环境,使用set注入方式为Bean对象注入属性值并打印输出.要求如下: ...

  4. Java的发展简史

    Java是由Sun Microsystems公司(简称Sun公司)于1995 年 5 月推出的 Java程序设计语言和Java平台的总称.Java语言是一种可以撰写跨平台应用软件的面向对象的程序设计语 ...

  5. 物联网大赛 - Android学习笔记(三)Android 事件处理

    学习目标: 了解事件处理概念 监听事件处理模型 事件与事件监听接口 实现事件监听方式 回调事件处理模型 常见的事件回调方法 Handler类功能与用法 Handler更新程序界面 一.监听概念 再用户 ...

  6. 关于C#的decimal浮点类型转化成字符串时末尾存在多个0

    首先,对于浮点类型,double和float存在精度丢失问题,这一点在之前的一篇博文中有提到(C# double类型精度丢失问题),于是,一般时候推荐大家使用decmal,特别是涉及到一些金融计算时, ...

  7. websocket在线测试工具

    为了测试websocket, 根据网上的一些工具修改了一些, 因此得到了这个工具 源码 源码: <!DOCTYPE html> <html lang="en"&g ...

  8. VoIP语音处理流程和知识点梳理

    做音频软件开发10+年,包括语音通信.语音识别.音乐播放等,大部分时间在做语音通信.做语音通信中又大部分时间在做VoIP语音处理.语音通信是全双工的,既要把自己的语音发送出去让对方听到,又要接收对方的 ...

  9. mysql自动安装脚本

    #!/bin/bashif [ -d /software ] ;then cd /softwareelse mkdir /software && cd /softwarefi #is ...

  10. spring cloud --- Feign --- 心得

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 什么是Feign? 为了简化我们的开发,Spring Cloud Fei ...