//1.直接创建一个字节输入流管道和源文件对接接通
InputStream is = new FileInputStream("Day09Demo/src/dilei02.txt");
//2.必须使用循环,但是还是无法解决中文乱码的问题
//a.定义一个字节数组代表桶
byte[] buffer = new byte[3];
int len;//存储每次读取的字节数
//3.循环,括号以及等号右边有优先级,因此is.read(buffer)拿到桶后装三滴水,返回3然后判断不等于-1
while ((len = is.read(buffer))!=-1){
//读多少倒多少!
String rs = new String(buffer,0,len);
System.out.print(rs);//不用换行
}
3.2 字节输出流写数据
内容
写一个字节
写一个字节数组
写部分字节数组
追加管道
关闭管道
代码
//1.创建一个字节输出流管道与目标文件接通,默认是数据覆盖管道
//追加数据管道,可以追加数据,原来数据不清掉
//OutputStream os = new FileOutputStream("Day09Demo/src/dilei04.txt",true);
OutputStream os = new FileOutputStream("Day09Demo/src/dilei04.txt");//不用自己再建文件,因为源码中已经有创建文件
//2.写一个字节出去(写一滴水出去)
os.write(97);//字节a
os.write('b');//字节b
//os.write('芬');//只会写出中文的第一个字节,写出去就是乱码了!
os.write("\r\n".getBytes());//加了\r是在哪个系统都可以换行
//3.写一个字节数组出去(写一个桶出去)
byte[] bytes = new byte[]{34,54,65,76,87,6,54};//写了一堆字节
os.write(bytes);
byte[] bytes1 = "Java是最优美的语言".getBytes();//直接拿字符串调getbytes得到字节,默认以当前编码UTF-8提取
System.out.println(bytes1.length);//看看长度
os.write(bytes1);
os.write("\r\n".getBytes());//换行
//4.写一个字节数组的一部分出去
byte[] bytes2 = "Java是最优美的语言".getBytes();
os.write(bytes2,0,19);
os.write("\r\n".getBytes());//换行
//6.刷新或者关闭下
os.flush();//立即刷新数据到文件中去,刷新后管道可以继续使用
os.close();//记得关闭资源管道,关闭包含了刷新,关闭后管道不能使用了
3.3 字节流作文件复制
内容:一半字节流用来做文件复制的多,因为读写文件难避免中文字符出现乱码的情况
建两根输入流和输出流管道
循环先读后写
捕获下异常
代码
public static void main(String[] args) {
//try后面小括号放置资源对象,用完会自动调用close关闭,省的finally里面写了
try(//相当于常量,只能给一次初始值
//1.创建一个字节输入流管道和源文件接通
InputStream is = new FileInputStream("C:\\Users\\CZyue\\Desktop\\壁纸\\girl.jpg");
//2.创建一个字节输出流与目标文件接通
OutputStream os = new FileOutputStream("C:\\Users\\CZyue\\Desktop\\littlegirl.jpg");
)
{
//3.创建一个字节数组作为桶
byte[] butter = new byte[1024];
//4.从字节输入流读取数据,写到字节输出流管道即可
//定义一个变量存储每次桶读取的字节数
int len;
while ((len = is.read(butter))!=-1){
//读取多少就倒出多少
os.write(butter,0,len);
}
System.out.println("复制完成!!");
}catch (Exception e){
e.printStackTrace();
}
}
4 一般字符流
4.1 字符输入流读数据
内容
和字节输入流差不多,把字节byte换成字符char即可
代码
//1.创建一个字符输入流管道与源文件路径接通
Reader fr = new FileReader("Day10Demo\\src\\dilei01.txt");
//2.使用循环读取
char[] chars = new char[1024];
//定义一个整数记录每次桶读取的字符数据量
int len = 0;
while((len = fr.read(chars))!=-1){
//读多少倒出多少
System.out.print(new String(chars,0,len));
}
public static void main(String[] args) throws Exception {
//1.定义一个低级的字节输入流与源文件接通
InputStream is = new FileInputStream("Day10Demo\\src\\dilei03.txt");
//2.把低级的字节输入流包装成一个高级的缓冲字节输入流,多了个缓冲池
BufferedInputStream bis = new BufferedInputStream(is);
//3.定义一个字节数组按照循环读取
byte[] buffer = new byte[3];
int len;
while((len=bis.read(buffer))!=-1){
String rs = new String(buffer,0,len);
System.out.println(rs);
}
}
5.2 字节缓冲输出流写文件
内容
new 一个字节缓冲输出流,然后将原来的字节输出流对象丢进去
代码
public static void main(String[] args) throws Exception {
//1.写一个原始的字节输出流
OutputStream os = new FileOutputStream("Day10Demo\\src\\dilei05.txt");
BufferedOutputStream bos = new BufferedOutputStream(os);
//2.写数据出去
bos.write('a');
bos.write(100);
bos.write("我爱中国".getBytes());
bos.close();
}
5.3 利用字节流的复制统计各种写法形式的性能执行情况
内容:分别比较四种字节流的复制性能
使用一般的字节流一个一个字节形式的复制文件
使用一般的字节流一个一个字节数组形式的复制文件
使用高级的缓冲字节流一个一个字节形式的复制文件
使用高级的缓冲字节流一个一个字节数组形式的复制文件
代码
public class CopyDemo {
public static final String SRC_FILE="C:\\Users\\CZyue\\Desktop\\04_问题答疑.vip";
public static final String DEST_FILE="C:\\Users\\CZyue\\Desktop\\Hadoop\\";
public static void main(String[] args) {
//一般的字节流一个一个字节形式的复制文件耗时141.218s
//一般流一个一个字节复制,速度跟蜗牛有的一拼,直接淘汰,静止使用!
copy01();
//一般的字节流一个一个字节数组的复制文件耗时0.312s,速度较慢
copy02();
//高级的字节缓冲流一个一个字节形式的复制文件耗时0.309s,速度较慢
copy03();
//高级的字节缓冲流一个一个字节数组的复制文件耗时0.186s,速度极快,这流可以处!
copy04();
}
//1.使用一般的字节流一个一个字节形式的复制文件
public static void copy01(){
long startTimer = System.currentTimeMillis();
try{
//1.创建一个一般字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
//2.创建一个一般的字节输出流和目标文件接通
FileOutputStream os = new FileOutputStream(DEST_FILE+"缓冲字节流复制的test1.vip");
//3.定义一个整形变量存储读取的字节
int ch;
while ((ch = is.read())!=-1){
os.write(ch);
}
long endTimer = System.currentTimeMillis();
System.out.println("一般的字节流一个一个字节形式的复制文件耗时"+(endTimer-startTimer)/1000.0);
}catch (Exception e){
e.printStackTrace();
}
}
//2.使用一般的字节流一个一个字节数组形式的复制文件
public static void copy02(){
long startTimer = System.currentTimeMillis();
try{
//1.创建一个一般字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
//2.创建一个一般的字节输出流和目标文件接通
FileOutputStream os = new FileOutputStream(DEST_FILE+"缓冲字节流复制的test2.vip");
//3.定义一个字节数组存储字节
byte[] butter = new byte[1024];//1KB
//定义一个变量存储每次读取的字节数量
int len;
while ((len=is.read(butter))!=-1){
os.write(butter,0,len);
}
long endTimer = System.currentTimeMillis();
System.out.println("一般的字节流一个一个字节数组的复制文件耗时"+(endTimer-startTimer)/1000.0);
}catch (Exception e){
e.printStackTrace();
}
}
//3.使用高级的缓冲字节流一个一个字节形式的复制文件
public static void copy03(){
long startTimer = System.currentTimeMillis();
try{
//1.创建一个高级字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
BufferedInputStream bis = new BufferedInputStream(is);
//2.创建一个高级的字节输出流和目标文件接通
FileOutputStream os = new FileOutputStream(DEST_FILE+"缓冲字节流复制的test3.vip");
BufferedOutputStream bos = new BufferedOutputStream(os);
//3.定义一个整形变量存储读取的字节
int ch;
while ((ch = bis.read())!=-1){
bos.write(ch);
}
long endTimer = System.currentTimeMillis();
System.out.println("高级的字节缓冲流一个一个字节形式的复制文件耗时"+(endTimer-startTimer)/1000.0);
}catch (Exception e){
e.printStackTrace();
}
}
//4.使用高级的缓冲字节流一个一个字节数组形式的复制文件
public static void copy04(){
long startTimer = System.currentTimeMillis();
try{
//1.创建一个低级字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
BufferedInputStream bis = new BufferedInputStream(is);
//2.创建一个低级的字节输出流和目标文件接通
FileOutputStream os = new FileOutputStream(DEST_FILE+"缓冲字节流复制的test4.vip");
BufferedOutputStream bos = new BufferedOutputStream(os);
//3.定义一个字节数组存储字节
byte[] butter = new byte[1024];//1KB
//定义一个变量存储每次读取的字节数量
int len;
while ((len=is.read(butter))!=-1){
os.write(butter,0,len);
}
long endTimer = System.currentTimeMillis();
System.out.println("高级的字节缓冲流一个一个字节数组的复制文件耗时"+(endTimer-startTimer)/1000.0);
}catch (Exception e){
e.printStackTrace();
}
}
//1.直接创建一个字节输入流管道和源文件对接接通
InputStream is = new FileInputStream("Day09Demo/src/dilei02.txt");
//2.必须使用循环,但是还是无法解决中文乱码的问题
//a.定义一个字节数组代表桶
byte[] buffer = new byte[3];
int len;//存储每次读取的字节数
//3.循环,括号以及等号右边有优先级,因此is.read(buffer)拿到桶后装三滴水,返回3然后判断不等于-1
while ((len = is.read(buffer))!=-1){
//读多少倒多少!
String rs = new String(buffer,0,len);
System.out.print(rs);//不用换行
}
3.2 字节输出流写数据
内容
写一个字节
写一个字节数组
写部分字节数组
追加管道
关闭管道
代码
//1.创建一个字节输出流管道与目标文件接通,默认是数据覆盖管道
//追加数据管道,可以追加数据,原来数据不清掉
//OutputStream os = new FileOutputStream("Day09Demo/src/dilei04.txt",true);
OutputStream os = new FileOutputStream("Day09Demo/src/dilei04.txt");//不用自己再建文件,因为源码中已经有创建文件
//2.写一个字节出去(写一滴水出去)
os.write(97);//字节a
os.write('b');//字节b
//os.write('芬');//只会写出中文的第一个字节,写出去就是乱码了!
os.write("\r\n".getBytes());//加了\r是在哪个系统都可以换行
//3.写一个字节数组出去(写一个桶出去)
byte[] bytes = new byte[]{34,54,65,76,87,6,54};//写了一堆字节
os.write(bytes);
byte[] bytes1 = "Java是最优美的语言".getBytes();//直接拿字符串调getbytes得到字节,默认以当前编码UTF-8提取
System.out.println(bytes1.length);//看看长度
os.write(bytes1);
os.write("\r\n".getBytes());//换行
//4.写一个字节数组的一部分出去
byte[] bytes2 = "Java是最优美的语言".getBytes();
os.write(bytes2,0,19);
os.write("\r\n".getBytes());//换行
//6.刷新或者关闭下
os.flush();//立即刷新数据到文件中去,刷新后管道可以继续使用
os.close();//记得关闭资源管道,关闭包含了刷新,关闭后管道不能使用了
3.3 字节流作文件复制
内容:一半字节流用来做文件复制的多,因为读写文件难避免中文字符出现乱码的情况
建两根输入流和输出流管道
循环先读后写
捕获下异常
代码
public static void main(String[] args) {
//try后面小括号放置资源对象,用完会自动调用close关闭,省的finally里面写了
try(//相当于常量,只能给一次初始值
//1.创建一个字节输入流管道和源文件接通
InputStream is = new FileInputStream("C:\\Users\\CZyue\\Desktop\\壁纸\\girl.jpg");
//2.创建一个字节输出流与目标文件接通
OutputStream os = new FileOutputStream("C:\\Users\\CZyue\\Desktop\\littlegirl.jpg");
)
{
//3.创建一个字节数组作为桶
byte[] butter = new byte[1024];
//4.从字节输入流读取数据,写到字节输出流管道即可
//定义一个变量存储每次桶读取的字节数
int len;
while ((len = is.read(butter))!=-1){
//读取多少就倒出多少
os.write(butter,0,len);
}
System.out.println("复制完成!!");
}catch (Exception e){
e.printStackTrace();
}
}
4 一般字符流
4.1 字符输入流读数据
内容
和字节输入流差不多,把字节byte换成字符char即可
代码
//1.创建一个字符输入流管道与源文件路径接通
Reader fr = new FileReader("Day10Demo\\src\\dilei01.txt");
//2.使用循环读取
char[] chars = new char[1024];
//定义一个整数记录每次桶读取的字符数据量
int len = 0;
while((len = fr.read(chars))!=-1){
//读多少倒出多少
System.out.print(new String(chars,0,len));
}
public static void main(String[] args) throws Exception {
//1.定义一个低级的字节输入流与源文件接通
InputStream is = new FileInputStream("Day10Demo\\src\\dilei03.txt");
//2.把低级的字节输入流包装成一个高级的缓冲字节输入流,多了个缓冲池
BufferedInputStream bis = new BufferedInputStream(is);
//3.定义一个字节数组按照循环读取
byte[] buffer = new byte[3];
int len;
while((len=bis.read(buffer))!=-1){
String rs = new String(buffer,0,len);
System.out.println(rs);
}
}
5.2 字节缓冲输出流写文件
内容
new 一个字节缓冲输出流,然后将原来的字节输出流对象丢进去
代码
public static void main(String[] args) throws Exception {
//1.写一个原始的字节输出流
OutputStream os = new FileOutputStream("Day10Demo\\src\\dilei05.txt");
BufferedOutputStream bos = new BufferedOutputStream(os);
//2.写数据出去
bos.write('a');
bos.write(100);
bos.write("我爱中国".getBytes());
bos.close();
}
5.3 利用字节流的复制统计各种写法形式的性能执行情况
内容:分别比较四种字节流的复制性能
使用一般的字节流一个一个字节形式的复制文件
使用一般的字节流一个一个字节数组形式的复制文件
使用高级的缓冲字节流一个一个字节形式的复制文件
使用高级的缓冲字节流一个一个字节数组形式的复制文件
代码
public class CopyDemo {
public static final String SRC_FILE="C:\\Users\\CZyue\\Desktop\\04_问题答疑.vip";
public static final String DEST_FILE="C:\\Users\\CZyue\\Desktop\\Hadoop\\";
public static void main(String[] args) {
//一般的字节流一个一个字节形式的复制文件耗时141.218s
//一般流一个一个字节复制,速度跟蜗牛有的一拼,直接淘汰,静止使用!
copy01();
//一般的字节流一个一个字节数组的复制文件耗时0.312s,速度较慢
copy02();
//高级的字节缓冲流一个一个字节形式的复制文件耗时0.309s,速度较慢
copy03();
//高级的字节缓冲流一个一个字节数组的复制文件耗时0.186s,速度极快,这流可以处!
copy04();
}
//1.使用一般的字节流一个一个字节形式的复制文件
public static void copy01(){
long startTimer = System.currentTimeMillis();
try{
//1.创建一个一般字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
//2.创建一个一般的字节输出流和目标文件接通
FileOutputStream os = new FileOutputStream(DEST_FILE+"缓冲字节流复制的test1.vip");
//3.定义一个整形变量存储读取的字节
int ch;
while ((ch = is.read())!=-1){
os.write(ch);
}
long endTimer = System.currentTimeMillis();
System.out.println("一般的字节流一个一个字节形式的复制文件耗时"+(endTimer-startTimer)/1000.0);
}catch (Exception e){
e.printStackTrace();
}
}
//2.使用一般的字节流一个一个字节数组形式的复制文件
public static void copy02(){
long startTimer = System.currentTimeMillis();
try{
//1.创建一个一般字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
//2.创建一个一般的字节输出流和目标文件接通
FileOutputStream os = new FileOutputStream(DEST_FILE+"缓冲字节流复制的test2.vip");
//3.定义一个字节数组存储字节
byte[] butter = new byte[1024];//1KB
//定义一个变量存储每次读取的字节数量
int len;
while ((len=is.read(butter))!=-1){
os.write(butter,0,len);
}
long endTimer = System.currentTimeMillis();
System.out.println("一般的字节流一个一个字节数组的复制文件耗时"+(endTimer-startTimer)/1000.0);
}catch (Exception e){
e.printStackTrace();
}
}
//3.使用高级的缓冲字节流一个一个字节形式的复制文件
public static void copy03(){
long startTimer = System.currentTimeMillis();
try{
//1.创建一个高级字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
BufferedInputStream bis = new BufferedInputStream(is);
//2.创建一个高级的字节输出流和目标文件接通
FileOutputStream os = new FileOutputStream(DEST_FILE+"缓冲字节流复制的test3.vip");
BufferedOutputStream bos = new BufferedOutputStream(os);
//3.定义一个整形变量存储读取的字节
int ch;
while ((ch = bis.read())!=-1){
bos.write(ch);
}
long endTimer = System.currentTimeMillis();
System.out.println("高级的字节缓冲流一个一个字节形式的复制文件耗时"+(endTimer-startTimer)/1000.0);
}catch (Exception e){
e.printStackTrace();
}
}
//4.使用高级的缓冲字节流一个一个字节数组形式的复制文件
public static void copy04(){
long startTimer = System.currentTimeMillis();
try{
//1.创建一个低级字节输入流与源文件接通
InputStream is = new FileInputStream(SRC_FILE);
BufferedInputStream bis = new BufferedInputStream(is);
//2.创建一个低级的字节输出流和目标文件接通
FileOutputStream os = new FileOutputStream(DEST_FILE+"缓冲字节流复制的test4.vip");
BufferedOutputStream bos = new BufferedOutputStream(os);
//3.定义一个字节数组存储字节
byte[] butter = new byte[1024];//1KB
//定义一个变量存储每次读取的字节数量
int len;
while ((len=is.read(butter))!=-1){
os.write(butter,0,len);
}
long endTimer = System.currentTimeMillis();
System.out.println("高级的字节缓冲流一个一个字节数组的复制文件耗时"+(endTimer-startTimer)/1000.0);
}catch (Exception e){
e.printStackTrace();
}
}