——- android培训、java培训、期待与您交流! ———-
IO流中常用的类及方法
按照方向:
输入流
输出流
按照操作数据的类型
字节流
字节输入流 InputStream 读
字节输出流 OutputStream 写
字符流
字符输入流 Reader 读
字符输出流 Writer 写
FileOutputStream 字节输出流对应的写的方法:
public void write(int b):写一个字节
public void write(byte[] b):写一个字节数组
public void write(byte[] b,int off,int len):写一个字节数组的一部分
FileInputStream 字节输入流对应的写的方法:
public int read() : 读一个字节
public int read(byte[] b) : 读有一个字节数组
用FileOutputStream和FileInputStream复制一个文件的案例:
public class CopyFileDemo {
// 创建字节输入流和字节输出流对象
FileInputStream fis = new FileInputStream("C:\\mv.jpg") ;
FileOutputStream fos = new FileOutputStream("copy.jpg") ;
// 频繁的读写操作第一种方式
// int by = 0 ;
// while((by = fis.read()) != -1){
// fos.write(by) ;
// }
// 频繁的读写操作第二种方式
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = fis.read(bytes)) != -1){
fos.write(bytes, 0, len) ;
}
// 释放资源
fos.close() ;
fis.close() ;
}
}
BufferedInputStream 是InputStream的包装,来达到高效的目的,同理BufferedOutputStream是对OutputStream的包装
下面是使用高效的字节输入输出流来对复制文件的改进:
package cn.itcast_test06;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileTest {
public static void main(String[] args) throws IOException {
// 获取运行前的时间
long start = System.currentTimeMillis() ;
copy1() ;
// 获取运行后的时间
long end = System.currentTimeMillis() ;
// 输出
System.out.println(end - start );
}
/**
* 使用高效的流对象一次读取一个字节数组复制mp3文件
* @throws IOException
* 90ms
*/
public static void copy2() throws IOException{
// 创建高效的字节输入流和字节输出流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\a.mp3")) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\a.mp3")) ;
// 频繁的读写操作
byte[] bytes = new byte[1024] ;
int len = 0 ;
while((len = bis.read(bytes)) != -1){
bos.write(bytes, 0, len) ;
}
// 释放资源
bos.close() ;
bis.close() ;
}
/**
* 使用高效的流对象一次读取一个字节复制mp3文件
* @throws IOException
* 344ms
*/
public static void copy1() throws IOException {
// 创建高效的字节输入流和字节输出流对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\a.mp3")) ;
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\a.mp3")) ;
// 频繁的读写操作
int by = 0 ;
while((by = bis.read()) != -1){
bos.write(by) ;
}
// 释放资源
bos.close() ;
bis.close() ;
}
}
关于编码解码的概述
编码:
public byte[] getBytes()使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
public byte[] getBytes(String charsetName) throws UnsupportedEncodingException使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
解码:
public String(byte[] bytes)通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
public String(byte[] bytes, String charsetName) throws UnsupportedEncodingException通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
编码: 把字符串转换成字节数组
解码: 把字节数组转换成一个字符串
编解码问题:使用什么进行编码的 ,那么就使用什么进行解码
代码示例:
public class StringDemo {
public static void main(String[] args) throws IOException {
// 定义一个字符串
String s = "中国";
// public byte[] getBytes()使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的
// byte 数组中。
// byte[] bytes = s.getBytes() ; // 默认的字符编码就是GBK [-42, -48, -71, -6]
// public byte[] getBytes(String charsetName) throws
// UnsupportedEncodingException使用指定的字符集将此 String 编码为 byte 序列,
// 并将结果存储到一个新的 byte 数组中。
// byte[] bytes = s.getBytes("GBK") ; // [-42, -48, -71, -6]
byte[] bytes = s.getBytes("UTF-8"); // [-28, -72, -83, -27, -101, -67]
// 把字节数组转换成一个字符串
System.out.println(Arrays.toString(bytes));
System.out.println("--------------------------------");
// public String(byte[] bytes)通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
// String result = new String(bytes) ; // 默认的字符编码就是GBK
// public String(byte[] bytes, String charsetName) throws
// UnsupportedEncodingException通过使用指定的 charset 解码指定的 byte 数组,
// 构造一个新的 String。
// String result = new String(bytes , "UTF-8") ;
String result = new String(bytes, "GBK");
// 输出
System.out.println(result);
}
}