记忆法:
首先记住字节流以Stream为后缀,字符流以Writer/Reader为后缀。四个顶层流分别为:输入字节流、输出字节流、输出字符流、输入字符流。下面分别是文件流、缓冲流、对象流。IO系列的名称,见名知意,非常容易记忆。描述使用字节流将数据读入内存,并输出到控制台。
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(); } } }描述使用字符流将数据读入内存,并输出到控制台。
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(); } } }描述将图片文件读入到内存,并存储到内存的另一个位置。 注意
图片使用字节流进行传输。写入时不要将多余字节写入新文件,否则会导致新图片打不开,注意代码中是如何处理的。 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.关闭流 } }描述网络上传文件,并存储在服务器上,返回存储地址。 注意
代码中配置了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。 ↩︎