Java IO流(二)

*****************************I/O流的分类****************************************

一.按照数据的流向分

1. 输入流


2. 输出流


二. 每次存取的单位

1. 字节流


2. 字符流

三.按照管道是否直接和数据源相连

1. 节点流

如果管道 直接和数据源相连 该管道属于节点流


2. 处理流(包装流)


套在节点流之上的管道 叫做处理流,也叫做包装流..

4个常用的抽象类

字节 字符


输入 InputStream Reader read()方法


输出 OutputStream Writer write()方法


*****************************字节读****************************************

FileInputStream


演示代码:

package com.chapter13.演示字节读;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* 公司:蓝桥软件学院
* 作者:zhangzy
* 时间:2017年7月21日 下午2:10:56
* 功能:演示字节读
*/
public class TestFileInputStream {

public static void main(String[] args) {

//一.建立通道
FileInputStream fis = null;
int b;

try {
fis = new FileInputStream("d:\\jidi16\\io\\HelloIO.txt");
} catch (FileNotFoundException e) {
System.out.println("文件没有找到");
e.printStackTrace();

System.exit(-1);
}

//二.利用read循环读

//fis.read() 每次读一个字节 把读到的这1个字节 存到int类型的低8位中
// read() 如果读到了文件末尾 返回-1
try {
while((b=fis.read())!=-1){

System.out.print((char)b);
}
} catch (IOException e) {
System.out.println("读取文件失败");
e.printStackTrace();
}finally{

//三.关闭通道
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
System.out.println("关闭fis通道失败");
e.printStackTrace();
}
}
}
}
}

常用方法

public int read() throws IOException


每次读一个字节 读到int类型的低8位中 如果读到文件末尾 返回-1
*****************************JDK7.0的新特性---可以自动释放资源的try****************************************

try-with-resources


演示代码:


package com.chapter13.演示字节读;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* 公司:蓝桥软件学院 作者:zhangzy 时间:2017年7月21日 下午2:10:56 功能:可以自动释放资源的try
*/
public class TestTryWithResources {

public static void main(String[] args) {

// 一.建立通道

int b;

try (
FileInputStream fis = new FileInputStream("d:\\jidi16\\io\\HelloIO.txt");
) {

while ((b = fis.read()) != -1) {

System.out.print((char) b);
}
} catch (FileNotFoundException e) {
System.out.println("文件没有找到");
e.printStackTrace();

System.exit(-1);
} catch (IOException e) {
System.out.println("读取文件失败");
e.printStackTrace();
}

}
}


****************************************字符读****************************************

FileReader

常用方法

public int read() throws IOException


每次读一个字符 中文是一个字符 英文也是一个字符 (并不是说英文就读2个字节 是错误的)


package com.chapter13.演示字节读;

import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* 公司:蓝桥软件学院
* 作者:zhangzy
* 时间:2017年7月21日 下午2:10:56
* 功能:演示字符读
*/
public class TestFileReader {

public static void main(String[] args) {

//一.建立通道
FileReader fis = null;
int b;

int sum = 0;

try {
fis = new FileReader("d:\\jidi16\\io\\HelloIO.txt");
} catch (FileNotFoundException e) {
System.out.println("文件没有找到");
e.printStackTrace();

System.exit(-1);
}

//二.利用read循环读


try {
while((b=fis.read())!=-1){
sum++;
System.out.print((char)b);
}
} catch (IOException e) {
System.out.println("读取文件失败");
e.printStackTrace();
}finally{

//三.关闭通道
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
System.out.println("关闭fis通道失败");
e.printStackTrace();
}
}
}

System.out.println();
System.out.println(System.getProperty("file.encoding"));//获得本地平台的默认字符编码集
System.out.println("一共读取了" + sum + "次");
}
}

****************************************编码方式****************************************


1. ANSI

American National Standards Insitute

美国国家标准协会编码


中文操作系统 gbk 1个中文 2个字节


日文操作系统

2. UTF-8


UTF-8(8-bit Unicode Transformation Format)是一种针对Unicode的可变长度字符编码,又称万国码。由Ken Thompson于1992年创建。现在已经标准化为RFC 3629。UTF-8用1到6个字节编码Unicode字符。用在网页上可以统一页面显示中文简体繁体及其它语言(如英文,日文,韩文)。


国际化 1个中文 3-4个字节 占3个字节的居多


3.中文

GB2312:国家简体中文字符集,兼容ASCII。
BIG5:统一繁体字编码
GBK:它是GB2312的扩展,支持简体和繁体字,兼容GB2312
GB18030:在GBK基础上继续扩展生僻字和日文、朝鲜语等的编码,兼容GBK


4.UNICODE:为世界650种语言进行统一编码,只兼容ASCII对GB系列都不兼容

5.ASCII:西欧字符集


***********************************为什么会出现乱码?*********************************

文件的编码方式 和解码方式 不一致的造成的..

编码: 看的懂的明文-------------> 看不懂的密文


解码: 看不懂的密文--------------> 看的懂的明文

ASCII


明文 密文

A 65

B 66


? ...


****在控制台下运行程序的时候

java com.chapter13.TestFileReader


启动JVM 使用的编码方式 是 gbk(中文操作系统 默认使用的是gbk),文件编码是(utf-8),读到程序中的字节是utf-8编码的字节,

解码使用的是gbk,二者不一致 所以乱码!如何解决?


java -Dfile.encoding=utf-8 com.chapter13.TestFileReader


设置JVM运行时 的默认字符编码集为utf-8

****在MyEclipse下设置

读的那个文件的编码方式(utf-8) 要和 MyEclipse的控制台编码方式一致就可以了


和 右键 -- Run As - Run Configurations---Common-- encoding 一致就可以了


*****************************使用字节读中文(面试题)*************************


解决方案: 一下子把整个文件全部读进来

FileInputStream常用方法

1.public int read() throws IOException


每次读一个字节 读到int类型的低8位中 如果读到文件末尾 返回-1


2.public int read(byte[] b) throws IOException


每次读一个字节数组 把读到的字节数组 存到b 返回的是真正读取的字节数

package com.chapter13.演示字节读;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
* 公司:蓝桥软件学院
* 作者:zhangzy
* 时间:2017年7月21日 下午2:10:56
* 功能:演示字节读中文
* 原理: 一下子把文件整个读进来
*/
public class TestInputStreamReadChinese {

public static void main(String[] args) {

//一.建立通道
FileInputStream fis = null;
int b;

try {
fis = new FileInputStream("d:\\jidi16\\io\\HelloIO.txt");
} catch (FileNotFoundException e) {
System.out.println("文件没有找到");
e.printStackTrace();

System.exit(-1);
}

//二.利用read循环读


try {
byte[] byteArr = new byte[fis.available()];

while((b=fis.read(byteArr))!=-1){

System.out.print(new String(byteArr,"utf-8"));// 字节数组(密文)--->明文 解码
}
} catch (IOException e) {
System.out.println("读取文件失败");
e.printStackTrace();
}finally{

//三.关闭通道
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
System.out.println("关闭fis通道失败");
e.printStackTrace();
}
}
}
}
}

猜你喜欢

转载自www.cnblogs.com/MrTanJunCai/p/9906824.html