6. java IO 流
一、流的分类:
* 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 流的更多相关文章
- Java:IO流与文件基础
Java:IO流与文件基础 说明: 本章内容将会持续更新,大家可以关注一下并给我提供建议,谢谢啦. 走进流 什么是流 流:从源到目的地的字节的有序序列. 在Java中,可以从其中读取一个字节序列的对象 ...
- java IO流详解
流的概念和作用 学习Java IO,不得不提到的就是JavaIO流. 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- Java IO流学习总结
Java流操作有关的类或接口: Java流类图结构: 流的概念和作用 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输 ...
- 揭开Java IO流中的flush()的神秘面纱
大家在使用Java IO流中OutputStream.PrintWriter --时,会经常用到它的flush()方法. 与在网络硬件中缓存一样,流还可以在软件中得到缓存,即直接在Java代码中缓存. ...
- java io流 对文件夹的操作
java io流 对文件夹的操作 检查文件夹是否存在 显示文件夹下面的文件 ....更多方法参考 http://www.cnblogs.com/phpyangbo/p/5965781.html ,与文 ...
- Java IO流题库
一. 填空题 Java IO流可以分为 节点流 和处理流两大类,其中前者处于IO操作的第一线,所有操作必须通过他们进行. 输入流的唯一目的是提供通往数据的通道,程序可以通过这个通道读取数 ...
- Java IO流总结
Java IO流分类以及主要使用方式如下: IO流 |--字节流 |--字节输入流 InputStream: int read();//一次读取一个字节 int read(byte[] bys);// ...
- java io流 运行错误时,保存异常到文件里面
java io流 运行错误时,保存异常到文件里面 下面这个实例,运行后,输入数字,为正确,如果输入字符串,则报错,保存错误信息 //运行错误时,保存异常到文件里面 //下面这个实例,运行后,输入数字, ...
- java io流 创建文件、写入数据、设置输出位置
java io流 创建文件 写入数据 改变system.out.print的输出位置 //创建文件 //写入数据 //改变system.out.print的输出位置 import java.io.*; ...
- java io流 数据流传输
java io流 数据流传输 把这段当公式用就可以了 //数据流传输 import java.io.*; public class Index{ public static void main(Str ...
随机推荐
- 【LeetCode】739. Daily Temperatures 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 解题方法 倒序遍历 栈 日期 题目地址:https://leetcode ...
- codeforce A. 2Char(水题,暴力)
今晚发了个蛇精病,然后CF了,第一题这好难啊,然而水题一个,暴力飘过. 链接http://codeforces.com/contest/593/problem/A: 题意比较难懂吗?傻逼百度都翻译不对 ...
- King's Order(hdu5642)
King's Order Accepts: 381 Submissions: 1361 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- [linux]ubuntu18.04 屏幕分辨率不适应问题
今天换了新显示器,发现更大的屏幕不适应原有的屏幕分辨率,看起来特别变扭. 在设置处查看最高分辨率仅为1024*748,没有与屏幕相适应的1920*1080(16:9). 解决方式: 1. 终端输入命令 ...
- capstoneCS5213|HDMI转VGA带DAV模拟音频输出转换器|CS5213方案
capstone CS5213是一款HDMI到VGA转换器结合了HDMI输入接口和模拟RGB DAC输出且带支持片上音频数模转换器.CS5213芯片设计简单,整体芯片尺寸精悍,外围电路集成优化度较高, ...
- 快看!❤️又一超实用浏览器插件!常用网站自动整合,JSON格式化,CSDN全站去广告!多种工具一键调用。开发者的福音!
其实这个插件才出来的时候博主也下载了使用过,并没有什么亮点,那时候甚至觉得有点多余,因为CSDN全站去广告啥的,早就安装了油猴脚本,广告?不存在的嘿嘿.. 就在前几天看见CSDN的活动在推荐这款插件, ...
- 【MySQL作业】MySQL函数——美和易思日期和时间函数应用习题
点击打开所使用到的数据库>>> 1.采用尽可能多的方式显示当前系统日期和时间. 下列 SQL 语句可以显示当前系统的日期和时间: curdate() 和 current_date() ...
- 涂鸦智能 dubbo-go 亿级流量的实践与探索
涂鸦智能 dubbo-go 亿级流量的实践与探索 dubbo 是一个基于 Java 开发的高性能的轻量级 RPC 框架,dubbo 提供了丰富的服务治理功能和优秀的扩展能力.而 dubbo-go 在 ...
- C/C++ Qt 运用JSON解析库 [基础篇]
JSON是一种简单的轻量级数据交换格式,Qt库为JSON的相关操作提供了完整的类支持,使用JSON解析文件之前需要先通过TextStream流将文件读入到字符串变量内,然后再通过QJsonDocume ...
- 初识python: 继承
继承:可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展. 通过继承创建的新类称为"子类"或"派生类". 被继承的类称为"基 ...