根据网上已有的视频进行的和自己的理解写的
1.IO的基本介绍
IO 可以理解为in和Out,即输入和输出 所以IO的基本功能就是读和写
IO流 读取设备上的数据,硬盘文件,内容,键盘,网络
根据数据的走向可分为输入流和输出流,根据数据的大小分为字节流,和字符流
字节流可以处理所有类型的数据,读取时,读到一个字节就返回一个字节
java中所对应的类都以Sream结尾的
字符流处理纯文本的数据,读取时,读到一个或者多个字节,先查找指定的编码表,然后将查到的字符返回。
在java中,对应的类都是以Reader或Writer结尾的
字节(byte)
字节是通过网络传输信息或在硬盘或内存中存储信息的单位,是计算机信息技术用于计量存储和传输的一种计量单位。
一个字节等于8位二进制,即一个8位的二进制数,是一个具体的存储空间
如 0x01 0x23,0x78
字符(Char)
字符是人们使用的记号,抽象意义的一个符号
字符集(Charset)
字符集也成为“编码”
这种ANSI编码标准所规定的内容
1.使用哪个汉字字母符号被收入标准,所包含的“字符”的集合叫字符集
2.规定每个字符分别用一个字节还是多个字节来存储,用哪些字节来存储,这个规定叫“编码”。
ANDI
字符串在内存中,如果字符是以ANSI编码存在,即一个字符使用多个字节,或一个字节 来表示
UNICODE
不管在什么环境中,多代表的字符都不变
2..文件的读取(把文本读取出来)
2.1字节流的读取
public class ReadByteStream {
/**
*通过字节流进行文件的读取
*/
public static void main(String[] args) {
//字节流读取文件
try {
FileInputStream fis = new FileInputStream("text.txt");
//创建字节数组,指定字节大小
byte input[] = new byte[21];
//将读取的数据填写到数组中
fis.read(input);
//将字节数组转换成字符串数组
String inputString = new String(input);
System.out.println(inputString);
fis.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.2字符流的读取
public class CopyByCharStream {
/**
* 字符流的文件读取显示在屏幕上
*/
public static void main(String[] args) {
try {
File file = new File("java.txt");
//字节流
FileInputStream fis = new FileInputStream(file);
//将字节流转换为字符流
InputStreamReader isr = new InputStreamReader((fis),"GBK");
//创建数组,存放读取的文件
char input[] = new char[100];
int l = 0;//读取到文件末尾,即使没有被填满,也会进行读取
//当isr.read()!=-1的时候,进行读取操作
while((l = isr.read(input)) != -1) {
System.out.println(new String(input,0,l));//将字符转换成字符串
}
isr.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.3带有缓冲流的文件读取
public class ReadBybufferedByteStream {
/**
* 用带有缓冲的字节流读取数据
*/
public static void main(String[] args) {
try {
//获取原始的字节输入流
FileInputStream fis = new FileInputStream("bigwhite.kux");
//带有缓冲效果的
BufferedInputStream bis = new BufferedInputStream(fis);
//创建字节行的数组,每次读取100个字节,需要进行循环的判断
byte intput[] = new byte[100];
int count = 0;
//获取使用的毫秒数
long before = System.currentTimeMillis();
while ((bis.read(intput) != -1)) {
//判断读取了多少次
count++;
//读取
bis.read( );
}
bis.close();
fis.close();
System.out.println("读取了"+count + "次");
System.out.println("一共使用了"+(System.currentTimeMillis()-before)+"ss");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.文件的复制
public class RWByCharStream {
/**
* 将字节流转换成字符流来进行读取后进行复制
*/
public static void main(String[] args) {
try {
// File file = new File("java.txt");
// 字节流
FileInputStream fis = new FileInputStream("java.txt");
FileOutputStream fos = new FileOutputStream("new java.txt");
// 将字节流转换为字符流
InputStreamReader isr = new InputStreamReader(fis, "GBK");
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
// 创建数组,存放读取的文件
char input[] = new char[100];
int l = 0;// 读取到文件末尾,即使没有被填满,也会进行读取
// 当isr.read()!=-1的时候,进行读取操作
while ((l = isr.read(input)) != -1) {
osw.write(input, 0, l);// 需要写的文件,偏移量,和写入的字符
}
isr.close();
fis.close();
osw.close();
fos.close();
System.out.println("done");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
缓冲流的复制
public class RWByCharStreamBuffered {
/**
* 带有缓冲的字符复制
*/
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("java.txt");
FileOutputStream fos = new FileOutputStream("new_java_buffered.txt");
// 将字节流转换为字符流
InputStreamReader isr = new InputStreamReader(fis, "GBK");
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
// bufferedReader有读取行的操作
BufferedReader br = new BufferedReader(isr);
// 可以写出换行符,打印输出流
PrintWriter pw = new PrintWriter(osw,true);// 第二个参数将未满的缓冲区的内容强制输出
// BufferedWriter bw = new BufferedWriter(osw);
// 先写入缓冲区,当缓冲区写满后,才向硬盘写入一次
String input;
while ((input = br.readLine()) != null) {// 带有自带的缓冲区,如果不为null,则执行
// bw.write(input);
pw.println(input);
}
// 将未满的缓冲区的内容强制输出
//bw.flush();
// bw.close();
pw.close();
br.close();
isr.close();
fis.close();
osw.close();
fos.close();
System.out.println("done");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.文件的的写入
5.1字节文件的写入
public class WriteBytesStrean {
/**
*通过字节流进行文件的写入
*/
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("tewx.txt");
String outString = "write 123456写出数据";
byte[] output = outString.getBytes("GBK");
fos.write(output);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
6.利用IO外部包类进行的基本操作
下载地址:
Commons IO - Download Commons IO
http://mirrors.hust.edu.cn/apache//commons/io/binaries/commons-io-2.4-bin.zip
public class TestFileUtils {
/**
* 利用外部类进行的操作
*/
public static void main(String[] args) {
File file = new File("text.txt");
File file2 = new File("text_new.txt");
try {
// 利用外部的库IO进行文件的读取
// String input = FileUtils.readFileToString(file, "GBK");
// System.out.println(input);
// 把第一个文件复制给第二个文件,第二个文件不存在
FileUtils.copyFile(file, file2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
百度云的全部源代码:http://pan.baidu.com/s/1bn9Kkhx