《从零开始的Java世界》系列主要讲解Javase部分,从最简单的程序设计到面向对象编程,再到异常处理、常用API的使用,最后到注解、反射,涵盖Java基础所需的所有知识点。学习者应该从学会如何使用,到知道其实现原理全方位式地学习,才能为以后框架的学习打下良好的基础。
目录
3.3FileInputStream及FileOutputStream














读写文本文件用字符流
- @Test
- public void test(){
- FileReader fr = null;
- try {
- //1.创建File类对象,对应一个文件
- File file = new File("hello.txt");
- //2.创建输入型的字符流,用于读取数据
- fr = new FileReader(file);
- //3.读取数据,显示在控制台上
- //方式一:
- // int data = fr.read();
- // while (data != -1){
- // System.out.print((char)data);
- // data = fr.read();
- //方式二:
- char buffer[] = new char[5];
- int len = fr.read(buffer);
- while (len != -1){
- for(int i = 0; i < len; i++){
- System.out.print(buffer[i]);
- }
- len = fr.read(buffer);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- //4.关闭流资源
- try {
- fr.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- @Test
- public void test2() {
- FileWriter fw = null;
- try {
- //1.创建File对象,指明操作的文件名称
- File file = new File("info.txt");
- //2.创建输出流
- fw = new FileWriter(file);
- //3.输出过程
- fw.write("ykx666");
- } catch (IOException e) {
- e.printStackTrace();
- }
- //4.关闭资源
- try {
- fw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
读写非文本文件(如图片、音视频)用字节流
- @Test
- public void test(){
-
- FileInputStream fis = null;
- FileOutputStream fos = null;
- try {
- //1.创建相关的File类对象
- File file1 = new File("sdz.jpg");
- File file2 = new File("copy_sdz.jpg");
- //2.创建相关字节流
- fis = new FileInputStream(file1);
- fos = new FileOutputStream(file2);
- //3.数据的输入和输出
- byte[] buffer = new byte[1024];
- int len;
- while((len = fis.read(buffer)) != -1)
- fos.write(buffer,0,len);
- System.out.println("复制成功!");
- }catch (IOException e) {
- e.printStackTrace();
- }
- //4.关闭资源
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }





- /*
- * 使用BufferedInputStream/BufferedOutputStream复制一张图片
- * @author yangkx
- * @date 2023-05-22 19:48
- */
- @Test
- public void test(){
- FileInputStream fis = null;
- FileOutputStream fos = null;
- BufferedInputStream bis = null;
- BufferedOutputStream bos = null;
- try {
- //1.创建相关的File类对象
- File file1 = new File("sdz.jpg");
- File file2 = new File("copy_sdz2.jpg");
- //2.创建相关字节流、缓冲流
- fis = new FileInputStream(file1);
- fos = new FileOutputStream(file2);
-
- bis = new BufferedInputStream(fis);
- bos = new BufferedOutputStream(fos);
-
- //3.数据的输入和输出
- byte[] buffer = new byte[1024];
- int len;
- while((len = bis.read(buffer)) != -1)
- bos.write(buffer,0,len);
- System.out.println("复制成功!");
- }catch (IOException e) {
- e.printStackTrace();
- }
- //4.关闭资源
- //先关外层
- try {
- bis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- bos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }

















内容来源于尚硅谷javase课程的ppt,仅作为学习笔记参考
