Java IO 基本类FileInputStream、FileOutputStream、FileReader、FileWriter、ByteArrayInputStream和Output(二)


基本类的读取流程

文件系统
FIleInputStream
FileOutputStream
程序
ByteArrayInputStream
内存
Java的ByteArrayOutputStream

java 的FileInputStream类

是将文件读入到程序中,所以每次都会向操作系统去借资源,最后再申请关闭资源
本代码有格式的固定写法,可以多联系几遍,方便后面代码的阅读和代码的理解

  • 基本的步骤:
    • 1.创建源文件
    • 2.选择流
    • 3.读取文件
    • 4.释放资源

逐个的导入字节

static void readyFile1() {
  //1.创建源文件
  File file = new File("tast.txt");
  //2.选择流
  InputStream os = null;
  try {
   os = new FileInputStream(file);
   //定义一个容器来暂时的存放我们的read内容
    int temp;
    //3.读取文件
   while ((temp = os.read())!=-1) {
    System.out.println((char)temp);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   //关闭文件
   try {
    if(null!=null) {
    os.close();}
   } catch (IOException e) {
    e.printStackTrace();
   }
  }

逐个的导入字节数组

/***
  * 每次搬运三个,然后放在数组里
  */
 static void readyFileBlock() {
  //1.创建源文件
  File file = new File("tast.txt");
  //2.选择流
  InputStream is= null;
  try {
   is = new FileInputStream(file);
   //选择一个容器来存放我们读取的几个字节
   byte b[]= new byte[3];
   //再定义一个长度,用来记录我们一次读取了多少个字节
   int len=-1;
   while((len= is.read(b))!=-1) {
    //文件操作
    String sr = new String(b, 0,len);
    System.out.print(sr);
   }
   
  } catch (FileNotFoundException e) {
   
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   //关闭流
   try {
    if(null!=is) {
     is.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
 } 

java的FileOutputStream类

可以将程序内的字节数组写入到文件

static void fileOutputstream() {
//创建源
  File file = new File("out.txt");
  //选择流
  OutputStream os = null;
  try {
   os= new FileOutputStream(file,true);//true表示会累加的写入
   String msg = "每个人都可以追逐我们的梦想";
   //创建缓存容器
   byte[] a = msg.getBytes();//字符--->字节(编码)
   //文件操作
   os.write(a);
   //输出的流,可以使用flush的方法来清理驻留
   os.flush();
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
  //关闭文件
   try {
    if(os!=null) {
     os.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }

java的FileReader类

这个每次读取的单位是个char类型的数据,可以逐个,也可以建立个容器来进行大量的读取,其他的操作和FIle Input Stream类一样

public class fileIO6 {
 /***
  * 从文件总读取数据
  */
 static void reader() {
  //1、创建源
  File file = new File("tast.txt");
  //2、选着流
  Reader fr = null;
  try {
   fr = new FileReader(file);
   char[] c = new char[3];//3、操作
   int len=-1;
   while((len=fr.read(c))!=-1) {
    System.out.print(c);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }catch (IOException e) {
   e.printStackTrace();
  }finally {
   try {
    if(fr!=null) {
     fr.close();//4、关闭文件流
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  
  
 
 }

java的FileWriter类

这个类和FileOutputStream差不多,唯一的区别就他读如的最小单位为char类型

/***
  * 
  * 从字符串中读取文件存入文件
  */
 static void write() {
  //创建源
  File file = new File("out3.txt");
  String sr = "每个人都有一个梦想。在这个魅力的世界里,我们可以尽情的实现我们的梦想的梦";
  //选着流
  Writer fw = null;
  try {
   //方法一
   fw = new FileWriter(file,true);//true代表可以累加的写入,默认为False
//   char[] c = sr.toCharArray();
//   fw.write(c);
   //方法二
//   fw.write(sr);//可以直接进行写入字符串
   
   //方法三
   fw.append(sr, 0, 2).append('c').append("可以加入字符串");//链式调用
   fw.flush();
  } catch (IOException e) {
   e.printStackTrace();
  }finally {
   try {
    if(fw!=null) {
     fw.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
 

java的ByteArrayInputStream类

字节数组ByteArrayInputStream,
1、建立在内存中,不用担心我们会会找不到文件的情况,不用再进行处理
2、在java虚拟机中没有关闭文件这个功能,但是为了保持形式,就建立了一个空的方法
&sync
3、缓冲区中的数据保存直到缓冲区满后才写出,也可以使用flush方法将缓冲区中的数据强制写出或使用close()方法关闭流,
4、关闭流之前,缓冲输出流将缓冲区数据一次性写出。
5、lz的例子中,flash()和close()都使数据强制写出,所以两种结果是一样的,
6、如果都不写的话,会发现不能成功写出
因为close的时候,会把你没flush掉的一起flush掉。

举个恶心但贴切(flush原意)的例子你的fw好比一个抽水马桶
每个人用完都冲一下(flush),直到最后你要拆掉它的时候(close)拆他的人,会强制冲(fush)一下

static void tast_ByteArrayInputStream1() {
     //建立源
 	String sr = "生活多美好";
 	InputStream is = null;
 
 	try {
  		is = new ByteArrayInputStream(sr.getBytes());
  		byte[] b = new byte[2];//建立缓存容器
  		int len =-1;//接收字符数
  		while((len=is.read(b))!=-1) {
  			//为了方便与观察我们将他进行打印
   			String ss = new String(b, 0, len);
   			System.out.println(ss);
   		}
	 } catch (IOException e) {
 		 e.printStackTrace();
 	}finally{
 		try {
   			if(os!=null) {
   			//关闭流
    				bos.close();//这是个空的方法,有没有都可以
  			 }
 		 } catch (IOException e) {
  			 e.printStackTrace();
 		 }}
}

Java的ByteArrayOutputStream类

这个方法中的 toByteArray();的方法是个新的方法无法使用多态

/***
 * 无法使用多态部分内容:我们要使用的是本部分的子类的新的方法
 *  多态的使用前提:
 *   使用重写的方法
 *   使用父类的方法
 * 无法使用多态:
 *   子类新增的方法
 */
static void tast_ByteArrayOutputStream1() {
	 //创建源:
 	byte[] b = null;//创建一个容器,用来存放我们内存类的数据
 	//选择流:
	 ByteArrayOutputStream byos= null;
 
	 try {
		  byos = new ByteArrayOutputStream();
 		 String sr = "来自阜阳";
 		 //获取存入的输出内容的数据
 		 byte[] data = sr.getBytes();//字符串---->字节数组(编码)
 		 //写出内容
 		 byos.write(data, 0, data.length);
 		 byos.flush();
 		 //读出数据,看有没有存入
 		 b = byos.toByteArray();
 		 String aaa= new String(b,0,b.length);
  		//String aaa = new String(b,0,byos.size)
 		 System.out.println(aaa);
	 } catch (IOException e) {
 		 e.printStackTrace();
	 }
}

利用基本的类,将图片读取到内存再读入到文件系统

将文件加入到内存里

private static byte[] fileToByte(String src) {
  //创建源
  File file = new File(src);
  InputStream is = null;
  ByteArrayOutputStream bis = null;
  //建立流(选择流)
  try {
   	is = new FileInputStream(file);
   	bis = new ByteArrayOutputStream();
   	byte[] b = new byte[1024*4];//建立缓存容器4K
   	int len = -1;//读入的长度
   	//操作
   	while ((len=is.read(b))!=-1) {
    		bis.write(b, 0, len);//读入到内存
   	}
   	return bis.toByteArray();
  } catch (FileNotFoundException e) {
   	e.printStackTrace();
  } catch (IOException e) {
   	e.printStackTrace();
  }finally {
   	try {
    		if(is!=null) {
    		 //先打开的那个那个流,最后关闭
     	is.close();//File流必须进行关闭
     	bis.close();//这是个空的方法,有没有都可以
    		}
   } catch (IOException e) {
    		e.printStackTrace();
   }
   
  }
  return null;
  
 } 

将内存的文件放在文件系统里

/***
  * 将内存里的图片加载处理到文件系统里
  */
 private static void byteToFile(byte[] data,String destination){
  //创建源
  File file = new File(destination);
  
  //选着流
  OutputStream os = null;
  InputStream bos = null;
  try {
   	os = new FileOutputStream(file);
   	bos = new ByteArrayInputStream(data);
   	//操作
   	byte [] b = new byte[1024*4];
   	int len = -1;
   	while((len = bos.read(b))!=-1) {
    		os.write(b, 0, len);
   }
   	os.flush();
   	//这个东西就像我们的那个拆分这个流的东西,
   	//如果不写的也不会出现问题,关闭文件的时候也会给他清除一下缓存
  } catch (FileNotFoundException e) {
   	e.printStackTrace();
  } catch (IOException e) {
   	e.printStackTrace();
  }finally {
   	try {
    		if(os!=null) {
     	//关闭流
     		bos.close();//这是个空的方法,有没有都可以
     		os.close();//这个是必须进行关闭的,他借用的是操作系统的资源,必须是要通知关闭的
    		}
   	} catch (IOException e) {
    		e.printStackTrace();
   }
  } 
 }

最后利用两个函数

public static void copyFile(String src_path,String des_path) {
  byteToFile(fileToByte(src_path), des_path);
 }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值