一、流的分类:
* 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】820. 单词的压缩编码 Short Encoding of Words(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode-cn.com/problems/short- ...

  2. 【LeetCode】686. Repeated String Match 解题报告(Python & C++)

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

  3. 【剑指Offer】栈的压入、弹出队列 解题报告(Python)

    [剑指Offer]栈的压入.弹出队列 解题报告(Python) 标签(空格分隔): 剑指Offer 题目地址:https://www.nowcoder.com/ta/coding-interviews ...

  4. Fence(poj1821)

    Fence Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4705   Accepted: 1489 Description ...

  5. 实战!Spring Boot 整合 阿里开源中间件 Canal 实现数据增量同步!

    大家好,我是不才陈某~ 数据同步一直是一个令人头疼的问题.在业务量小,场景不多,数据量不大的情况下我们可能会选择在项目中直接写一些定时任务手动处理数据,例如从多个表将数据查出来,再汇总处理,再插入到相 ...

  6. Codeforces 888D: Almost Identity Permutations(错排公式,组合数)

    A permutation \(p\) of size \(n\) is an array such that every integer from \(1\) to \(n\) occurs exa ...

  7. IntelliJ IDEA打war包

    1.按ctrl+alt+shift+s键打开Project Structure,点击+号图标,选择"Artifacts->Web Application Archive" 2 ...

  8. CS5265低成本替代RTD2172|CS5265替代兼容RTD2172|替代RTD2172

    瑞昱RTD2172是TYPEC转HDMI4K60HZ音视频数据转换器芯片.CS5265可以替代兼容RTD2172,除了实现同等的转换功能外且整体方案成本和性价比方面比RTD2172要高,且外围器件较少 ...

  9. RSA非对称加密算法实现:Golang

    RSA是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA ...

  10. ActiveMQ基础教程(一):认识ActiveMQ

    ActiveMQ是Apache软件基金会所研发开源的消息中间件,为应用程序提供高效的.可扩展的.稳定的和安全的企业级消息通信. 现在的消息队列有不少,RabbitMQ.Kafka.RocketMQ,Z ...