八: IO流,数据的读写传输
IO流概括图:

IO流的分类:
按流:
- 输入流(InputStream和Reader):从硬盘或者别的地方读入内存
- 输出流(OutputStream和Writer):从内存里向硬盘或别的地方输出
按操作类型:
- 字节流(InputStream和OutputStream): 字节流可以操作任何数据,因为在计算机中数据以字节的形式存储
- 字符流(Reader和Writer):字符流只能操作纯字符数据,比如文本,以防乱码
以上所述,InputStream、OutputStream、Reader、Writer都是抽象类
缓冲流:
- BufferedInputStream、BufferedOutputStream
- 优点:BufferedInputStream读取时会创建一个长度为8192的byte数组,读完返回-1。不用自己手动创建数组.
字符转换流:
- InputStreamReader和OutputStreamWriter可以设置编码,完成不同编码文件的转换
绝对路径和相对路径:
- 绝对路径:文件在硬盘上真正存在的路径,比如:D:\Java\HelloWorld.java
- 相对路径:只某个文件路径径和别的文件的路径关系 。
使用分隔符解决跨平台的问题:
- 使用File.separator解决不同系统路径问题
- 分隔符: windows: \
- linux : /
- 为了保证代码跨平台,使用File.separator代替分隔符
文件加密和解密:利用两次异或的原理
中文乱码问题: 一个中文占两个字节,如果一次读写入奇数个字节,就有可能乱码,这时候推荐使用字符流解决,一次读一行
字符流读写文件:一次读一行,记得换行bw.newLine()、刷新缓冲区bw.flush()
Code:
- test() :使用FileInputStream、FileOutputStream读取、写入文件中的内容
- test2():使用Buffered缓冲流进行文件拷贝
- test3():文件加密解密,利用两次异或
- test4():使用字节流读取文件乱码问题(一个中文占两个字节)
- test5():对乱码问题给出了解决,可使用字符流Writer、Reader
- test6():使用字符流拷贝文本文件
- test7():使用转换流解决不同文件编码问题
- test8():使用转换流解决不同文件编码问题,乱码解决
package io; import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL; import org.junit.Test; public class IODemo {
//定义加密code
static int code = 88;
//定义文件路径
static String fileName = "C:"+File.separator+"Users"+File.separator+"李腾"+File.separator+"Desktop"+File.separator+"写入文件.txt";
static String readName = "C:"+File.separator+"Users"+File.separator+"李腾"+File.separator+"Desktop"+File.separator+"读取文件.txt"; //使用FileInputStream、FileOutputStream读取、写入文件中的内容
@Test
public void test(){
FileInputStream fis = null;
FileOutputStream fos = null;
//绝对路径
String readFile = "D:"+File.separator+"mylog.log";
String writeFile = "D:"+File.separator+"newWrite.txt";
try {
fos = new FileOutputStream(writeFile,true);
fis = new FileInputStream(readFile);
//定义字节数组接收信息
byte[] bytes = new byte[6000];
int temp;
while((temp = fis.read(bytes))!=-1){
fos.write(temp);
//刷新并写出所有缓冲字节
fos.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fos.close();
fis.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
//使用Buffered缓冲流进行文件拷贝
@Test
public void test2(){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("D:"+File.separator+"mylog.log"));
bos = new BufferedOutputStream(new FileOutputStream("D:"+File.separator+"buf.txt"));
int temp;
while((temp=bis.read())!=-1){
bos.write(temp);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
} }
}
//文件加密解密,利用两次异或
//JDK7新特性,系统自动close
@Test
public void test3(){
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
//加密
try {
bis = new BufferedInputStream(new FileInputStream("D:"+File.separator+"IO流.rar"));
bos = new BufferedOutputStream(new FileOutputStream("D:"+File.separator+"加密.rar"));
int temp;
while((temp=bis.read())!=-1){
bos.write(temp^code);
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
//解密
try {
bis = new BufferedInputStream(new FileInputStream("D:"+File.separator+"加密.rar"));
bos = new BufferedOutputStream(new FileOutputStream("D:"+File.separator+"解密.rar"));
int temp;
while((temp=bis.read())!=-1){
bos.write(temp^code);
bos.flush();
}
} catch (IOException e) {
e.printStackTrace();
}
} //使用字节流读取文件乱码问题(一个中文占两个字节)
//test5对乱码问题给出了解决,可使用字符流Writer、Reader
@Test
public void test4(){
FileOutputStream fos = null;
FileInputStream fis = null;
String msg = "好好";
try {
fos = new FileOutputStream(fileName);
fis = new FileInputStream(readName);
int temp;
//每次读三个字节,也乱码
byte[] arr = new byte[3];
//每次写三个字节,因为中文占两,所以乱码
fos.write(msg.getBytes(),0,3);
//换行
fos.write("\r\n".getBytes());
fos.flush();
while((temp=fis.read(arr))!=-1){
System.out.println(new String(arr,0,temp));
} } catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /* 用字符流解决中文乱码问题
* BufferdReader可以一次读一行文字,readLine方法返回null
* */
@Test
public void test5(){
BufferedReader buf = null;
FileWriter fw = null;
BufferedWriter bw = null;
try {
buf = new BufferedReader(new FileReader(readName));
String s;
//写
bw = new BufferedWriter(new FileWriter(fileName));
bw.write("我喜欢学习java");
bw.write(97);
bw.flush();
fw = new FileWriter(fileName);
fw.write("FileWriter写入");
fw.flush();
//读
while((s = buf.readLine())!=null){
// bw.write(s);
System.out.print(s);
}
} catch (IOException e) {
e.printStackTrace();
}
} //使用字符流拷贝文本文件
@Test
public void test6(){
BufferedReader br = null;
BufferedWriter bw = null; try {
br = new BufferedReader(new FileReader(readName));
bw = new BufferedWriter(new FileWriter(fileName));
String s ;
while((s=br.readLine())!=null){
//读一行
bw.write(s);
//读完换行
bw.newLine();
bw.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //使用转换流解决不同文件编码问题
//字符流乱码
@Test
public void test7(){
try(
//将utf-8的内容写出到GBK.txt中,发现乱码
BufferedReader br=
new BufferedReader(new FileReader("D:\\JAVA基础\\7.15\\1javaSe\\file\\utf-8.txt"));
BufferedWriter bw=
new BufferedWriter(new FileWriter("D:\\JAVA基础\\7.15\\1javaSe\\file\\GBK.txt"));
){
String msg;
while((msg=br.readLine())!=null){
bw.write(msg);
}
bw.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} //使用转换流解决不同文件编码问题,乱码解决
@Test
public void test8(){
try(
BufferedReader br =
new BufferedReader(new InputStreamReader(new FileInputStream("D:\\JAVA基础\\7.15\\1javaSe\\file\\utf-8.txt"), "utf-8"));
BufferedWriter bw =
new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\JAVA基础\\7.15\\1javaSe\\file\\GBK.txt"),"GBK")); ){
String msg;
while((msg=br.readLine())!=null){
bw.write(msg);
}
bw.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
八: IO流,数据的读写传输的更多相关文章
- 零基础学习java------day16-----文件,递归,IO流(字节流读写数据)
1.File 1.1 构造方法(只是创建已经存在文件的对象,并不能创建没有的文件) (1)public File(String pathname) (2)public File(String pare ...
- Java API —— IO流(数据操作流 & 内存操作流 & 打印流 & 标准输入输出流 & 随机访问流 & 合并流 & 序列化流 & Properties & NIO)
1.操作基本数据类型的流 1) 操作基本数据类型 · DataInputStream:数据输入流允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型.应用程序可以使用数据输出 ...
- java IO流文件的读写具体实例(转载)
引言: 关于java IO流的操作是非常常见的,基本上每个项目都会用到,每次遇到都是去网上找一找就行了,屡试不爽.上次突然一个同事问了我java文件的读取,我一下子就懵了第一反应就是去网上找,虽然也能 ...
- C# IO流与文件读写学习笔记
本笔记摘抄自:https://www.cnblogs.com/liyangLife/p/4797583.html,记录一下学习过程以备后续查用. 一.文件系统 1.1文件系统类的介绍 文件操作类大都在 ...
- java IO流文件的读写具体实例
IO流的分类:1.根据流的数据对象来分:高端流:所有的内存中的流都是高端流,比如:InputStreamReader 低端流:所有的外界设备中的流都是低端流,比如InputStream,Output ...
- IO流数据读写总结
1.用java自带的IO读写方法 官方API网站:http://docs.oracle.com/javase/7/docs/api/ 2.Apache的Commons-io-2.4.jar中的方法,参 ...
- Java IO流之随机读写流RandomAccessFile
随机读写流RandomAccessFile 简介 此类的实例支持对随机访问文件的**读取和写入**. 随机访问文件的行为类似存储在文件系统中的一个大型 byte 数组. 存在指向该隐含数组的光标或索引 ...
- io流对文件读写操作
public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedRead ...
- IO流简要总结
IO流小总结 IO流的本质就是用于数据的传输,根据流的方向的不同,有输入流.输出流.根据数据类型的不同,又有字节流.字符流. 字节流 字节输入流 InputStream 字节输出流 Outpu ...
随机推荐
- 洛谷 P5057 [CQOI2006]简单题(树状数组)
嗯... 题目链接:https://www.luogu.org/problem/P5057 首先发现这道题中只有0和1,所以肯定与二进制有关.然后发现这道题需要支持区间更改和单点查询操作,所以首先想到 ...
- 栈结构Stack
package seday12; import java.util.Deque; import java.util.LinkedList; /** * @author xingsir * 栈结构. 栈 ...
- CSS - 权重,样式优先级
关于CSS权重,一套计算公式来去计算,就是 CSS Specificity,我们称为CSS 特性或称非凡性,它是一个衡量CSS值优先级的一个标准. 遇到样式应用问题,计算一下权重就知道优先级. 具体规 ...
- 第一个Tornado程序
环境:Python3.8 系统:win10 1903 工具:pycharm2019.3 import tornado.web # web服务基本功能都封装在此模块中 import tornado.io ...
- 计算机网络历史与基本概念&分层与参考模型(TCP/IP与OSI)&通信过程
Definition: 计算机网络:使用单一技术相互连接的自主计算机的互联集合. 单台计算机独立自主(不受制于其他计算机),连接介质可以使光纤.铜线也可以是微波.红外.卫星. 互联网络(Interne ...
- 3 Struts2的常见配置解析
1 package标签的相关配置 package标签:包,与Java中的包概念不一致.旨在更好的管理actionpackage标签的属性: name : 包的名称,在一个项目不重名即可,无具体含义 ...
- C 语言入门---第十一章---C语言重要知识点补充
====C语言typedef 的用法==== 1. C语言允许为一个数据类型起一个新的别名,就像给人起绰号一样. typedef OldName newName; typedef 和 #define ...
- LeetCode简单题(二)
题目一: 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的 ...
- Java连载80-数字类格式、随机数、BigDecimal
一.数字类 1.关于数字格式化:java.text.DecimalFormat; 2.数字格式元素: # 任意数字 , 千分位 . 小数点 0 不够补零 package com.bjpowernode ...
- 利用TPL(任务并行库)构建Pipeline处理Dataflow
https://www.cnblogs.com/CoderAyu/p/9757389.html