结点流:直接对File类进行操作的文件流

package stream;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException; import org.junit.jupiter.api.Test; /*
* 流的体系结构: 抽象基类 节点流(或文件流) 缓冲流(处理流的一种)
* 字节输入流 InputStream FileInputStream BufferedInputStream
* 字节输出流 OutputStream FileOutputStream BufferedOutputStream
* 字符输入流 Reader FileReader BufferedReader
* 字符输出流 Writer FileWriter BufferedWriter
*
* 字符流只能处理字符,字节流能处理图片,二进制文件
* */
public class FileReaderWriterTest { @Test
public void test() throws IOException {
//1.实例化File类的对象
//2.提供具体的流
FileReader fr = null;
try {
File file = new File("hello .txt");
System.out.println(file.getAbsolutePath()); File file1 = new File("C:\\Users\\ASUS\\Desktop\\JAVAEE\\practice\\IO_FIle\\hello.txt");
System.out.println(file1.getAbsolutePath()); fr = new FileReader(file); //3.数据的读入:
//read()方法:return一个读入的字符,如果读到结尾则输出-1
int data;
while((data = fr.read())!=-1)
System.out.println((char)data);
} catch (Exception e) {
e.printStackTrace();
}
finally {
//4.流的关闭操作
try {
if(fr != null)
fr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} //对read()操作升级:使用read的重载方法
@Test
public void testFileReader1() {
//2.FileReader流的实例化
FileReader fr = null;
try {
//1.File
File file = new File("hello.txt");
fr = new FileReader(file);
//3.读入的操作
//read(buf):返回每次读入buf的字符的个数,如果达到文件尾,返回-1
char [] buf = new char[5];
int len;
while((len = fr.read(buf)) != -1) {
String s = new String(buf,0,len);
System.out.println(s);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
//4.资源的关闭
if(fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /*
* 输出操作:对应的File可以不存在的
* 如果不存在,在输出的过程中会自动创建此文件
* 如果存在,则会覆盖此文件
* 但是可以增加第二个参数 true 进行追加
*/
@Test
public void testFileWriter() {
FileWriter fw = null;
try {
//1.提出File类的对象,指明写出到的文件
File file = new File("hello1.txt");
//2.提供FileWriter的对象,用于数据的写出
fw = new FileWriter(file);
//3.写出的操作
fw.write("i have a dream.\n");
fw.write("you have a dream too");
} catch (Exception e) {
e.printStackTrace();
}
finally {
//4.关闭流
try {
if(fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /*
* 进行文件复制
* */
@Test
public void testFileReaderFileWriter() {
FileReader fr = null;
FileWriter fw = null;
try {
File sfile = new File("hello.txt");
File ttfile = new File("hello2.txt"); fr = new FileReader(sfile);
fw = new FileWriter(ttfile); char [] buf = new char[5];
int len;
while((len = fr.read(buf))!=-1) {
fw.write(buf,0,len);
}
} catch (Exception e) {
e.printStackTrace();
}
finally {
//4.关闭资源
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
} try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

包装流:用来修饰节点流

缓冲流加速

package stream;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException; import org.junit.jupiter.api.Test; /*
* 1.缓冲流
* BufferedInputStream
* BufferedOutputStream
* BufferedReader
* BufferedWriter
*
* 2.作用:提高流的读取写入速度
*
* */ public class BufferedTest { /*
* 实现非文本文件的赋值
* */
@Test
public void BufferedStreamTest(){
BufferedInputStream brs = null;
BufferedOutputStream bos = null;
try {
File sfile = new File("zsben.jpg");
File tfile = new File("zsben3.jpg"); FileInputStream fis = new FileInputStream(sfile);
FileOutputStream fos = new FileOutputStream(tfile); brs = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos); byte[] buffer = new byte[10];
int len;
while((len = brs.read(buffer))!=-1) {
bos.write(buffer,0,len);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
//关闭外层流的同时, 内层流也会自动被关闭
brs.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
bos.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
}

转换流进行编码和解码

package stream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; import org.junit.jupiter.api.Test; /*
* 处理流之二:转换流
* InputStreamWriter:字节输入流->字符输入流
* OutputStreamWriter:字符输出流->字节输出流
* 字节->字符 (97->'a'):即一个解码过程
* 字符->字节 ('a'->97):即一个编码过程
* */ public class InputStreamReaderITest {
@Test
public void test1() throws IOException {
//第二个参数:file保存时使用的编码方式
InputStreamReader isr= null;
try {
File file = new File("hello.txt");
FileInputStream fis = new FileInputStream(file); isr = new InputStreamReader(fis,"gbk");//原来使用gbk编码存的,这儿换成UTF-8就会使中文字符乱码 char [] buf = new char[20];
int len;
while((len = isr.read(buf))!=-1) { String s = new String(buf,0,len);
System.out.println(s); }
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} isr.close();
}
}

java 节点流(字符流,字节流)和包装流(缓冲流,转换流)的更多相关文章

  1. io基础(字节流、字符流、转换流、缓冲字符流)

    首先需要明确的一点是输入流输出流的输入输出是站在内存的角度看的,读取文件,把文件内容写到内存中,是输入流:写文件,把内存中的数据写到文件中,是输出流. IO操作主要有4个抽象类: 字节输入输出流:In ...

  2. Java IO学习笔记(三)转换流、数据流、字节数组流

    转换流 1.转换流:将字节流转换成字符流,转换之后就可以一个字符一个字符的往程序写内容了,并且可以调用字符节点流的write(String s)方法,还可以在外面套用BufferedReader()和 ...

  3. Java转换流、缓冲流、流操作规律整理

    转换流 1.1                OutputStreamWriter类 OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字 ...

  4. JAVA-IO操作,字节-字符转换流

    掌握OutputStreamWriter和InputStreamReader类的作用 一般操作输入输出内容的时候,就需要使用字节或字符流,但是,有些时候,需要将字符流变成字节流形式,或者字节流变成字符 ...

  5. Java IO编程——转换流

    所谓的转换流指的是可以实现字节流与字符流操作的功能转换,例如:进行输出的时候OutputStream需要将内容变为字节数组后才可以进行输出,而Writer可以直接输出字符串,这一点是方便的,所以很多人 ...

  6. Java基础 使用转换流进行文件的复制 / RandomAccessFile 类进行文件的复制

    笔记:  **使用转换流进行文件的复制 文本文件---字节流FileInputStream--> [InputStreamReader] -----字符流BufferedReader------ ...

  7. 24_IO_第24天(转换流、缓冲流)_讲义

    今日内容介绍 1.转换流 2.缓冲流 01转换流概述 * A: 转换流概述 * a: 转换流概述 * OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流 ...

  8. 15、IO (转换流、缓冲流)

    转换流概述 * A: 转换流概述 * a: 转换流概述 * OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节 * 将字符串按照指定的 ...

  9. 24_java之转换流和缓冲流

    01转换流概述 * A: 转换流概述 * a: 转换流概述 * OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的字符编码表,将要写入流中的字符编码成字节 * 将字符串按照指 ...

  10. javaSE学习笔记(15) ---缓冲流、转换流、序列化流

    javaSE学习笔记(15) ---缓冲流.转换流.序列化流 缓冲流 昨天复习了基本的一些流,作为IO流的入门,今天我们要见识一些更强大的流.比如能够高效读写的缓冲流,能够转换编码的转换流,能够持久化 ...

随机推荐

  1. Microsoft Azure_Fabric

    目录 目录 前言 Microsoft Azure Microsoft Azure Fabric Controller 前言 WindowsAzure是相对于全球版Microsoft Azure而言的中 ...

  2. 【转载】阿里入局,独角兽估值30亿美金,谈谈RPA是什么

    缩短法定工作时间,已成国际劳动立法趋势,全球政府都曾面对这样的议题,过往企业IT也在思考这件事,开发出更好的软件系统帮助员工,就是普遍作法,这也已经行之有年,而现阶段最有效的作法,则是要用AI来提供帮 ...

  3. 测开之路六十一:接口测试平台之interface蓝图

    create的js //添加header的函数function add_header() { // 这里是动态拼接html语句,带着样式,拼凑成页面的 "key [] value []&qu ...

  4. 【EWM系列】SAP EWM模块-部分流程图

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[EWM系列]SAP EWM模块-部分流程图   ...

  5. Hadoop(1): HDFS基础架构

    1. What's HDFS? Hadoop Distributed File System is a block-structured file system where each file is ...

  6. Windows系统CVE整理

    CVE-2018-8420(RCE) 受影响版本: Microsoft Windows 10 Version 1607 for 32-bit Systems Microsoft Windows 10 ...

  7. Git014--Rebase

    Git--Rebase 本文来自于:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b00 ...

  8. Mac009--Axure RP安装

    Mac--Axure RP安装 一.下载Axure RP8.0 下载网址:https://www.axure.com/download  (下载mac版本) Axure RP说明: Axure RP是 ...

  9. U盘修复教程--使用量产工具和芯片检测工具

    U盘修复教程(金士顿DT101) 本教程使用量产工具(量产可理解为强制恢复出厂设置),量产有风险(U盘彻底报废),根据教程所造成所有损失均与本文作者无关. ·第一步:下载ChipGenius_v4_0 ...

  10. python学习第五天--函数进阶

    局部变量与全局变量下面代码中,old_price,rite为全局变量,final_price为局部变量 globals() 声明全局变量,在函数内可修改函数外的变量 内嵌函数:函数当中嵌套函数 闭包: ...