缓冲字节流
缓冲流就是先把数据缓冲到内存里,在内存中去做io操作,基于内存的io操作大概比基于硬盘的io操作快7万5千多倍
package day13;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class BufferedStreamLearn {
public static void main(String[] args) {
try {
testBufferedInputStream();
testBufferedOutputStream();
copyFile();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 缓冲字节输入流
*
*/
public static void testBufferedInputStream() throws Exception {
FileInputStream in = new FileInputStream("D:\\eclipse workspace\\Learn\\src\\day13\\test.txt");
//文件字节输入流对象
BufferedInputStream bf = new BufferedInputStream(in);
//把文件字节输入流放到缓冲字节输入流对象
byte[] b = new byte[10];
int len = 0;
while((len = bf.read(b)) !=- 1) {
System.out.println(new String(b,0,len));
}
//关闭流的时候,最晚开的最早关(先开后关)
bf.close();
in.close();
}
public static void testBufferedOutputStream() throws Exception {
FileOutputStream out = new FileOutputStream("D:\\eclipse workspace\\Learn\\src\\day13\\test2.txt");
//创建字节输出流对象
BufferedOutputStream bo = new BufferedOutputStream(out);
//把字节输出流对象放到缓冲字节输出流中
String s = "Hello World!";
bo.write(s.getBytes());//写到内存中
bo.flush();//刷到硬盘上
//晚开早关
bo.close();
out.close();
}
/**
* 缓冲流实现文件的复制
* @throws Exception
*
*/
public static void copyFile() throws Exception {
BufferedInputStream bi = new BufferedInputStream(new FileInputStream("D:\\eclipse workspace\\Learn\\src\\day13\\test2.txt"));
//缓冲输入流
BufferedOutputStream bo = new BufferedOutputStream(new FileOutputStream("D:\\eclipse workspace\\Learn\\src\\day13\\test3.txt"));
//缓冲输出流
byte[] b = new byte[1024];
int len = 0;
while((len = bi.read(b)) != -1) {
bo.write(b,0,len);//写到内存中
}
bo.flush();
bo.close();
bi.close();
}
}
缓冲字符流
package day13;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
/**
* 缓冲字符流
*
*/
public class BufferedCharSLearn {
public static void main(String[] args) {
try {
testBufferReader();
testBufferWritter();
copyFile();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 缓冲字符输入流
*/
public static void testBufferReader() throws Exception{
FileReader r = new FileReader("D:\\eclipse workspace\\Learn\\\\src\\day13\\test.txt");
BufferedReader br = new BufferedReader(r);
char[] c = new char[100];
int len = 0;
while((len = br.read(c)) != -1) {
System.out.println(new String(c,0,len));
}
br.close();
r.close();
}
public static void testBufferWritter() throws Exception{
FileWriter w = new FileWriter("D:\\eclipse workspace\\Learn\\src\\day13\\test4.txt");
BufferedWriter bw = new BufferedWriter(w);
String s = "干就完了!";
int len = 0;
bw.write(s);
bw.flush();
bw.close();
w.close();
}
/**
* 用缓冲字符流复制文件
*
*/
public static void copyFile() throws Exception{
BufferedReader br = new BufferedReader(new FileReader("D:\\eclipse workspace\\Learn\\src\\day13\\test4.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\eclipse workspace\\Learn\\src\\day13\\copytest4.txt"));
int len = 0;
char[] c = new char[100];
while((len = br.read(c)) != -1) {
bw.write(c,0,len);
}
bw.flush();
bw.close();
br.close();
}
}
转换流
package day13;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
/**
* 转换流
*
*/
public class ExchangeStreamLearn {
public static void main(String[] args) {
try {
testInputStreamReader();
testOutputStreamWriter();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 转换字符输入流
* 注意编码要一致
* 经测试,没写编码,只传1个参数也能成功运行
* InputStreamReader
*/
public static void testInputStreamReader() throws Exception {
FileInputStream fs = new FileInputStream("D:\\eclipse workspace\\Learn\\src\\day13\\e.txt");
InputStreamReader turn = new InputStreamReader(fs,"UTF-8");
//把字节流转换成字符流,参数1是字节流,参数2是编码
char[] c = new char[10];
int len = 0;
while((len = turn.read(c)) != -1) {
System.out.println(new String(c,0,len));
}
turn.close();
fs.close();
}
/**
* 转换字符输出流
* 经测试,没写编码,只传1个参数也能成功运行
* InputStreamReader
*/
public static void testOutputStreamWriter() throws Exception{
FileOutputStream fo = new FileOutputStream("D:\\eclipse workspace\\Learn\\src\\day13\\e2.txt");
OutputStreamWriter ow = new OutputStreamWriter(fo);
String s = "面对恐惧";
ow.write(s);
ow.flush();
ow.close();
fo.close();
}
}
处理流之三:标准输入输出流
package day13;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* 标准输入输出流
*/
public class StdioLearn {
public static void main(String[] args) {
try {
testSystemIn();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void testSystemIn() throws Exception{
InputStreamReader is = new InputStreamReader(System.in);
//传建一个接受键盘输入数据的输入流
BufferedReader br = new BufferedReader(is);
//把输入流放到缓冲流里
String str;//临时接受的字符串
while((str = br.readLine()) != null) {
System.out.println(str);
}
br.close();
is.close();
}
}
注意:String类型有个equals方法,可以用作循环终止条件
打印流(了解)
数据流(了解)
处理流之六:对象流
package day13;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* 对象流
* 注:对象的序列化与反序列化使用的类要严格一致
* 包名,类名,类机构等等所有都要一致
*/
public class ObjectStream {
public static void main(String[] args) {
try {
testSerialize();
testDeserialize();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 对象的序列化
*/
public static void testSerialize() throws Exception{
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("D:\\eclipse workspace\\Learn\\src\\day13\\test5.txt"));
//定义对象的输出流,把对象的序列化之后的流放到制定的文件中
Person p = new Person();
p.name = "巨魔";
p.age = 40;
out.writeObject(p);
out.flush();
out.close();
}
/**
* 对象的反序列化
*/
public static void testDeserialize() throws Exception {
ObjectInputStream in = new ObjectInputStream(new FileInputStream("D:\\eclipse workspace\\Learn\\src\\day13\\test5.txt"));
//创建输入流对象,从指定文件中把对象的序列化后的流读取出来
Object per = in.readObject();
Person p = (Person)per;
System.out.println("age="+p.age+" name:"+p.name);
}
}
RandomAccessFile类
package day13;
import java.io.RandomAccessFile;
/**
* 文件的随机读写
*
*/
public class RandomStreamLearn {
public static void main(String[] args) {
try {
testRandomAccessFileRead();
testRandomAccessFileWrite();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 经测试,一个字对应3个字符!
* 读
*/
public static void testRandomAccessFileRead() throws Exception{
RandomAccessFile ra = new RandomAccessFile("D:\\eclipse workspace\\Learn\\src\\day13\\test6.txt", "r");
//两个参数,1是文件路径,2是访问模式
ra.seek(0);//设置读取文件内容的起始点,按字符计算位置,注意\n
byte[] b = new byte[1024];
int len = 0;
while((len = ra.read(b)) != -1) {
System.out.println(new String(b,0,len));
}
ra.close();
}
/**
* 写
*/
public static void testRandomAccessFileWrite() throws Exception{
RandomAccessFile ra = new RandomAccessFile("D:\\eclipse workspace\\Learn\\src\\day13\\test6.txt", "rw");
ra.seek(5);//设置写的起始点,0从开头,ra.length()代表结尾写
ra.write("Love".getBytes());
ra.close();
}
}