一.IO
1.IO概念
·输入流:把能够读取一个字节序列的对象称为输入流(百度百科)
·输出流:把能够写一个字节序列的对象称为输出流(百度百科)
从定义上看可能会让你感到困惑,这里解释一下:输入输出是相对于内存设备而言的,将外设(硬盘,键盘等)中的数据读取到内存设备中叫输入;将内存设备中的数据写入到外设中叫输出,所以有读入,写出的称呼:读入到内存,写出内存。
可以这样比喻:输入流和输出流中间连接着内存,输入流和输出流将读写分离开来进行操作,先从外设读入内存,然后再写出内存转移到其他外设。
·总体结构图(摘自网友)
2.字节流 (用字节流处理字符数据可能会有编码问题,因为字节流是以字节为单位,没有编码,而字符流是以字符为单位传送数据,字符流即以字节流+编码)
·两个顶层父类 (抽象类)及实现类
·InputStream(读入内存) :所有字节输入流相关类的父类
··FileInputStream :obtain input bytes from a file in a file system,for reading streams of raw bytes(原始字节) such as image data..For writing streams of characters,consider using FileReader
初始化时(构造函数)要和文件关联,读取的对象,要首先判断文件是否存在
——read():read a byte of data from this inputStream.
——read(byte [] b):read up to b.length bytes of data from this inputStream into an array of bytes.
——read(byte [] b,int off,int length)
——close()
import java.io.*;
/**
* Created by 111 on 2016/1/29.
*/
public class Demo1 {
public static void main(String [] args){
File file = new File("d:/helloWorld.txt");
InputStream in = null;
try {
if (!file.exists()){ //文件不存在则创建
file.createNewFile();
}
in = new FileInputStream(file);
byte [] buf = new byte[1024]; //先写到缓存中,然后再一起写到其他外设中
int length = 0;
while ((length=in.read(buf))!=-1){ //-1 represent the end of the file is reached ,
//字节一个一个地读入到内存
System.out.println(new String(buf,0,length)); //需要将int转为字节,如果为中文的话输出乱码字符 ,
//此处其实是写出到了外设(控制台)上,System.out返回的是PrintStream对象
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
··ByteArrayInputStream:包含一个内置的缓冲器存储字节
构造函数要和字节数组相关联:byte [] buff
——read():从输入流中读取下一个字节
——read(byte [] buff,int off,int len):
——close():关闭后并没有影响,类中的方法仍然可以调用而不抛出IO异常
·OutputStream(写出内存):所有和输出字节流相关类的父类
··FileOutputStream:for writing data to a file or a FileDescriptor,for writing streams of raw data(原始字节)such as image data.For writing streams of characters,consider using FileWriter.
初始化时要和文件关联,写出的目的地。没有该文件时会自动创建。
——write(int b):write the specified(指定的) byte to this file output stream.
——write(byte [] b):
——write(byte [] b,int off,int len)
——close()
import java.io.*;
/**
* Created by 111 on 2016/2/25.
*/
public class Demo2 {
public static void main(String [] args){
File file = new File("d:/helloWorld3.txt"); //没有<span style="font-family:KaiTi_GB2312;">会自动创建</span>
OutputStream out = null;
try {
out = new FileOutputStream(file);
out.write(69); //文件中产生ASC码对应的字符
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
★ FileInputStream & FileOutputStream 协同完成文件复制(不会乱码)
import java.io.*;
/**
* Created by 111 on 2016/2/25.
*/
public class Demo3 {
/**
* 从一个文件复制到另一个文件
* @param args
*/
public static void main(String [] args){
File origin = new File("d:/helloWorld.txt");//原始文件
if (!origin.exists()){
try {
origin.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
File destination = new File("d:/helloWorld4.txt");//目的文件
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(origin);
out = new FileOutputStream(destination);
byte [] buff = new byte[1024];
int len = 0;
while ((len=in.read(buff))!=-1){
out.write(buff,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null){
in.close();
}
if (out != null){
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3.字符流
·两个顶层父抽象类及其实现类
·Reader:for reading character streams
··InputStreamReader:从字节流到字符流的桥梁:读取字节流然后按指定的编码方式进行解码(看不懂→能看懂)
构造函数要和输入流InputStream/编码方式Charset相关联:System.in/FileInputStream传入
——read() :读入一个字符
——read(char [] cbuf,int offset,int length)
——close()
···FileReader:读字符文件的方便类,本质是InputStreamReader在构造时 指定了默认的编码方式,用于读取字符流
构造函数要和文件相关联
★InputStreamReader 接收键盘上输入的数据,写入文件中(中文会乱码)
import java.io.*;
/**
* Created by 111 on 2016/2/25.
*/
public class Demo4 {
/**
* 控制台输入,写到文件夹中
* @param args
*/
public static void main(String [] args){
File file = new File("d:/helloWorld.txt");//会覆盖之前的数据
OutputStream out = null;
InputStreamReader reader = null;
try {
reader = new InputStreamReader(System.in);
out = new FileOutputStream(file);
int len = 0;
while ((len = reader.read())!= -1){
out.write(len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
··BufferedReader:从一个字符输入(character-input)流中读取文本(text),并进行缓冲字符,默认缓存8192(8M),行最长80
构造函数要和Reader in/int size 关联:InputStreamReader
——in read()
——in read(char [] cbuf,int off,int len)
——String readLine()
——close()
★键盘录入,使用缓存
BufferedReader buffr = new BufferedReader(new InputStreamReader(System.in))
·Writer:for writing to character streams (字符流的写操作基本上后面都需要进行flush()操作)
··OutputStreamWriter :从字符流到字节流的桥梁:写出的字符被用指定的编码方式进行编码。
构造函数要和OutputStream out/charset 关联:System.out/FileOutputStream
——write(int c):写一个单独的字符
——write(char [] cbuf,int off,int len)
——write(String str,int off,int len)
——flush():刷新流
——close()
···FileWriter:写字符文件的方便类,实质是:OutputStreamWriter指定了默认的本机编码方式,且可以处理文件
··BufferedWriter:写文本到一个字符输出(character-out)流,并对传入的字符进行缓存
构造函数要和 Writer out/int size 相关联
——write(int c):写一个单独的字符
——write(char [] cbuf,int off,int len)
——write(String str,int off,int len)
——newLine():换行
——flush():刷新流
——close()
★控制台输出,使用缓存
BufferedWriter buffw= new BufferedWriter(new OutputStreamWriter(System.out,"utf-8"));
★键盘输入,控制台输出功能
import java.io.*;
/**
* Created by 111 on 2016/2/26.
*/
public class Demo5 {
/**
* 键盘输入,控制台输出
* @param args
*/
public static void main(String[]args){
BufferedReader buff = null;
BufferedWriter bufferedWriter = null;
String line = null;
try {
buff = new BufferedReader(new InputStreamReader(System.in,"utf-8"));
bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out,"utf-8"));
<span style="font-family:SimSun;">while</span> ((line=buff.readLine())!=null){
bufferedWriter.write(line);
<span style="font-family:KaiTi_GB2312;"> <span style="font-family:SimSun;"> }</span></span>
bufferedWriter.flush(); //一定要刷新
} catch (IOException e) {
e.printStackTrace();
} finally {
if (buff!=null){
try {
buff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
☆面试题:简述一下将文件中的数据输入到另一个文件中的步骤
1.首先创建File对象,并且和需要操作的文件相关联,这时候需要对文件进行判断是否存在,不存在则会报错
2.既然是读取文件并且写到文件,属于纯文本,可以选择FileReader和FileWriter进行读写操作,如果出现乱码可以使用其父类指定编码方式
3.创建FileReader对象用于读取文件中的数据,这里可以使用缓冲流进行处理,提高效率,创建一个BufferedReader对象
BufferedReader buffr = new BufferedReader(new InputStreamReader(new FileReader(file)));
4.创建FileWriter,同上使用缓存
★代码如下
import java.io.*;
/**
* Created by 111 on 2016/2/26.
*/
public class Demo6 {
public static void main(String[] args){
File origin = new File("d:/helloWorld.txt");
File destination = new File("d:/helloWorld6.txt");
InputStreamReader in = null;
OutputStreamWriter out = null;
BufferedReader reader = null;
BufferedWriter writer = null;
if (!origin.exists()){
try {
origin.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(origin),"ISO-8859-1"));
// reader = new BufferedReader(new FileReader(origin));
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(destination),"ISO-8859-1"));
String line = null;
while ((line = reader.readLine())!=null){
writer.write(line);
writer.newLine();
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in!=null){
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader!= null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}