Javase复习系列——IO流

    技术2022-07-21  63

    文章目录

    `cho1.`基本问题`cho2.`IO流体系常用方法IO流的分类注意事项 `cho3.`demodemo1demo2demo3demo4

    cho1.基本问题

    什么是IO流 用于处理设备间数据传输问题的技术。在Java中使用流来实现数据传输,与之相关的类与接口在java.io包下。什么是输入输出 首先要站在该设备的角度,从该设备向其他设备发送数据,则为输出;反之,则为输入。在写代码过程中,我们站在内存的角度。学习重点 根据demo多练习代码,自己也可以根据需要进行代码练习。 牢记文件输入输出的步骤。

    cho2.IO流体系

    记忆法:

    首先记住字节流以Stream为后缀,字符流以Writer/Reader为后缀。四个顶层流分别为:输入字节流、输出字节流、输出字符流、输入字符流。下面分别是文件流、缓冲流、对象流。IO系列的名称,见名知意,非常容易记忆。

    常用方法

    方法名说明read读取write写出close关闭流flush刷新

    IO流的分类

    根据传输方向:输入流(Input)、输出流(Output)根据设备类型数据单位:字节流(Stream)、字符1 流(Writer/Reader)根据作用:处理流、节点流

    注意事项

    使用完流以后,不要忘记关闭流。缓冲流有一个默认的缓冲区,数据先存到缓冲里,缓冲区满了以后再写入或者写出。flush表示手动刷新缓冲区。缓冲流可以自动刷新。记住转换流的两个方向: 字节输入流转换为字符输入流 字符输出流转化为字节流 jdk1.4以后出现了NIO,以更加高效的方式进行文件的读写操作。

    cho3.demo

    demo1

    描述使用字节流将数据读入内存,并输出到控制台。

    public void FileToConsoleStream(){ //1.创建文件类 //默认路径在项目根目录 File f = new File("demo1.txt"); //判断文件是否存在 if(!f.exists()){ try { f.createNewFile(); }catch(IOException e){ e.printStackTrace(); } } FileInputStream fis = null; try { //2.创建字符流 fis = new FileInputStream(f); //3.读入内存、输出到控制台 byte text[] = new byte[1024]; if(fis.read(text) != -1){ System.out.println(new String(text, 0, text.length)); } }catch(IOException e){ e.printStackTrace(); }finally { try { //4.关闭流 fis.close(); }catch(IOException e){ e.printStackTrace(); } } }

    demo2

    描述使用字符流将数据读入内存,并输出到控制台。

    public void FileToConsoleReader(){ //1.创建文件类 //默认路径在项目根目录 File f = new File("demo1.txt"); //判断文件是否存在 if(!f.exists()){ try { f.createNewFile(); }catch(IOException e){ e.printStackTrace(); } } FileReader fr = null; try { //2.创建字符流 fr = new FileReader(f); //3.读入内存、输出到控制台 char c[] = new char[1024]; if(fr.read(c) != -1){ System.out.println(c); } }catch(IOException e){ e.printStackTrace(); }finally { try { //4.关闭流 fr.close(); }catch(IOException e){ e.printStackTrace(); } } }

    demo3

    描述将图片文件读入到内存,并存储到内存的另一个位置。 注意

    图片使用字节流进行传输。写入时不要将多余字节写入新文件,否则会导致新图片打不开,注意代码中是如何处理的。 public void saveImage(){ //1.创建文件对象 File oldFile = new File("H:\\wisdom\\images", "old.jpg"); File newFile = new File("H:\\wisdom\\images\\new","new.jpg"); //文件不存在 if(!oldFile.exists()){ System.out.println("源文件不存在!"); return; } //文件不存在 if(!newFile.exists()){ try { newFile.createNewFile(); }catch(IOException e){ e.printStackTrace(); } } FileInputStream fis = null; FileOutputStream fos = null; try { //2.创建文件输入输出字节流 fis = new FileInputStream(oldFile); fos = new FileOutputStream(newFile); //3.读取文件存储到内存 int len = 0; //限定每次读取2M大小 byte b[] = new byte[1024*2]; while((len = fis.read(b))!=-1)//循环读取数据 { //4.将内存数据写入磁盘 //len解决文件无法打开的问题 fos.write(b,0,len); } }catch(IOException e){ e.printStackTrace(); }finally { System.out.println(newFile.getAbsolutePath()); try { fis.close(); fos.close(); }catch(IOException e){ e.printStackTrace(); } //5.关闭流 } }

    demo4

    描述网络上传文件,并存储在服务器上,返回存储地址。 注意

    代码中配置了swagger,可以忽略。代码中返回数据使用了链式编程进行处理。 @RestController @Api(description="视频模块") public class VideoController { private List<String> mysuffix = new ArrayList<>(); @PostMapping("/store/video") @ApiOperation(value = "存储视频文件") public R storeVideo(@ApiParam(name = "cho_video", value = "视频", required = true) @RequestParam(value = "cho_video") MultipartFile file){ //添加可接收格式 String s = "rm,rmvb,mpeg1-4,mov,mtv,dat,wmv,avi,3gp,amv,dmv,flv,mpg,mpe,mpa,m15,m1v,mp2,mp4"; String strs[] = s.split(","); for(String i : strs){ mysuffix.add(i); } try { //文件不存在 if(file==null){ return R.error().code(403).message("上传失败,上传文件数据为空"); } //后缀 String suffix = file.getContentType().toLowerCase(); suffix = suffix.substring(suffix.lastIndexOf("/")+1); if(mysuffix.contains(suffix)) { //文件名称 String fileName = "chocho_" + UUID.randomUUID().toString().replaceAll("-", "") + "." + suffix; //保存路径 String FilePath = "/project/static"; File targetFile = new File(FilePath, fileName); if(!targetFile.getParentFile().exists()){ targetFile.getParentFile().mkdirs(); } //保存 file.transferTo(targetFile); return R.ok().code(200).message("上传视频成功").data("adrress", targetFile.getAbsolutePath()); } return R.error().code(403).message("系统异常,上传视频格式非法"); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { return R.error().code(403).message("系统异常,上传视频失败"); } return null; } }

    1字节 = 8位;1字符 = 2字节 = 2*8位 = 16 bit。 ↩︎

    Processed: 0.012, SQL: 9