文章目录
缓冲流
- JDK提供的缓冲类:
- BufferedInputStream类
- BufferedOutputStream类
- BufferedReader类
- BufferedWriter类
BufferedInputStream(字节输入缓冲流)
1、构造方法
BufferedInputStream(InputStream in) ;
BufferedInputStream(InputStream in, int size) ;//给出缓冲区的大小
2、常用方法:
read()//用来读取缓冲区数据
read(byte [] b , int offset, int length) //读数据至byte数组中,从offset开如,读取长度为length
它尝试通过重复调用基础流的read方法来读取尽可能多的字节
- 指定的字节数已被读取,
- 底层流的read方法返回-1 ,表示文件末尾,或
- 底层流的available方法返回零,表示进一步的输入请求将阻塞。
如果基础流上的第一个read返回-1以指示文件结束,则此方法返回-1 。 否则,此方法返回实际读取的字节数。 - 例子
FileInputStream fileInputStream = null;
BufferedInputStream buffer = null;
try {
fileInputStream = new FileInputStream(new File("C:\\java-code\\io_file\\stu1.txt"));
/* int i = 0;
while ((i=fileInputStream.read())!=-1){
System.out.println((char)i);
}*///如果使用非缓冲的流则需要用此循环进行读取
buffer = new BufferedInputStream(fileInputStream);
byte b[] = new byte[1024];
int i = 0;
while ((i = buffer.read(b)) != -1) {
System.out.println(new String(b, 0, i));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (buffer!=null) {
try {
buffer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedOutputStream(字节输出缓冲流)
构造方法:
BufferedOutputStream(OutputStream out)
BufferedOutputStream(OutputStream out, int size)
//主要方法:
flush()//刷新缓冲输出流。 这将强制任何缓冲输出字节写入底层输出流
write(byte[] b, int off, int len) //从偏移量 off开始的指定字节数组写入 len字节到缓冲输出流。
write(int b)
- 实例
try {
FileOutputStream fileOutputStream = new FileOutputStream(new File("C:\\java-code\\io_file\\stu3.txt"));
String str= "hello ,huike!";
BufferedOutputStream buffer= new BufferedOutputStream(fileOutputStream);
buffer.write(str.getBytes());//通过缓冲流对象,提供的write方法,将内容写入到输出流
buffer.flush();
buffer.close();
System.out.println(" 执行结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
通过字节缓冲流实现文件的拷贝
示例:
public static void main(String[] args) {
long start = System.currentTimeMillis();//计算有缓冲流情况下copy文件的时间
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
BufferedInputStream bufferinput = null;
BufferedOutputStream bufferoutput = null;
try {
fileInputStream = new FileInputStream(new File("C:\\java-code\\io_file\\stu1.txt"));
fileOutputStream = new FileOutputStream(new File("C:\\java-code\\io_file\\stu2.txt"));
bufferinput = new BufferedInputStream(fileInputStream);
bufferoutput = new BufferedOutputStream(fileOutputStream);
int i = 0;
while ((i = bufferinput.read()) != -1) {
bufferoutput.write(i);
bufferoutput.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferoutput != null) {
bufferoutput.close();
}
if (bufferinput != null) {
bufferinput.close();
}
if (fileOutputStream != null) {
fileOutputStream.close();
}
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();
System.out.println("程序一共执行了:"+(end-start)+"毫秒");
BufferedWriter/BufferedWriter(字符缓冲流)
- 用法相似
NIO(非阻塞IO)
- NIO java non-blocking IO java在1.4以后推出一个新的面向非阻塞式的IO流
优点:异步IO ,多路复用,增加了高效缓冲区(buffer)、通道(channel) 。
高效缓冲区
/*
缓冲区的使用步骤:
缓冲区的主要属性:
1、allocate():创建缓冲区对象指定缓冲区的长度
2、position();当前缓冲区中指针的只想位置
3、limit():当前缓冲区可读写的范围
4、capacity:缓冲区的容量
对缓冲区的核心操作方法
allocate():创建缓冲区对象时指定缓冲区的长度
position():当前缓冲区中指针的指向位置
limit():表示缓冲区在进行读写时的最小长度。
capacity():表示缓冲区的容量大小
5、rewind()
*/
//建立缓冲区
ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
System.out.println("------------缓冲区------------");
System.out.println(byteBuffer.position());
System.out.println(byteBuffer.limit());
System.out.println(byteBuffer.capacity());
//
System.out.println("------------写入数据-----------");
String b = "hello word";
byteBuffer.put(b.getBytes());
System.out.println(byteBuffer.position());
System.out.println(byteBuffer.limit());
System.out.println(byteBuffer.capacity());
byteBuffer.flip();
System.out.println("------------flip()-----------");
System.out.println(byteBuffer.position());
System.out.println(byteBuffer.limit());
System.out.println(byteBuffer.capacity());
System.out.println("------------读出数据-----------");
byte c[] = new byte[10];
byteBuffer.get(c, 0, 6);
System.out.println(byteBuffer.position());
System.out.println(byteBuffer.limit());
System.out.println(byteBuffer.capacity());
System.out.println(new String(c,0,6));
System.out.println("------------clear()----------");
byteBuffer.clear();
System.out.println(byteBuffer.position());
System.out.println(byteBuffer.limit());
System.out.println(byteBuffer.capacity());
byteBuffer.get(c,0,8);
System.out.println(new String(c,0,6));
System.out.println("------------rewind()----------");
byteBuffer.clear();
System.out.println(byteBuffer.position());
System.out.println(byteBuffer.limit());
System.out.println(byteBuffer.capacity());
byteBuffer.get(c,0,8);
}
}
//使用管道进行复制
try {
FileInputStream fileInputStream = new FileInputStream(new File("F:\\java学习资料\\test\\test.txt"));
FileOutputStream fileOutputStream = new FileOutputStream(new File("F:\\java学习资料\\test\\test1.txt"));
//BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
FileChannel inchannel = fileInputStream.getChannel();//创建输入流的管道
FileChannel outchannel = fileOutputStream.getChannel();//创建输入流的管道
ByteBuffer buffer = ByteBuffer.allocate(1024);
while ((inchannel.read(buffer) != -1)) {
buffer.flip();
outchannel.write(buffer);
buffer.clear();
}
System.out.println("复制完成");
对象序列化
- 将对象以文件的形式储存在硬盘中方便发送
- 将要序列化的类用关键字Serializable
- 并不是所有数据都可以在网络上传输,不想进行传输的数据可以使用transient关键字来保护
- 另一种序列化的方式Externalizable
public class Employee implements Serializable(){};
try {
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(new File("F:\\java学习资料\\test\\Class.txt"));
ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
Employee employee = new Employee("martin","4000",10000);
objectOutputStream.writeObject(employee);
objectOutputStream.close();;
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//反序列化
try {
FileInputStream fileInputStream = new FileInputStream(new File("F:\\java学习资料\\test\\Class.dat"));
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Employee employee = (Employee)objectInputStream.readObject();
System.out.println(employee);
objectInputStream.close();
fileInputStream.close();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}