javaMerea 2019-06-21
- 在人人都会编程的时代,老程序员的价值在哪里?编的更熟练吗?NONONO,必须和新人从不同维度竞争。
- 跟包容性强的人聊天感觉就是好
- 清醒的努力+选择,在3-5年成为业界80分水准的程序员
- 清醒的努力+选择,在一个方向专研,用10年时间成为99分的技术专家兼程序员。
- 清醒的努力+选择,用10年时间成为一个懂金融、懂营销、会写作、会演讲同时又“恰好会编程“的人。
- 大胆列计划【JavaEE、JavaWeb入行,往大数据方向转】、JavaScript【业余学习,应对物联网】、Python【业余学习,应对人工智能,并研究研究量化金融】的专家。
缓冲思想
BufferedInputStream
BufferedOutputStream
package com.test.demo001; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ //demo01(); FileInputStream fis = new FileInputStream("20.avi"); FileOutputStream fos = new FileOutputStream("copy.avi"); BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); int b; while((b = bis.read())!= -1){ bos.write(b); } bis.close(); bos.close(); } private static void demo01() throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream("cccccc.txt"); FileOutputStream fos = new FileOutputStream("bbbbbb.txt"); byte[] arr = new byte[2]; int len; //读到的有效字符个数 while((len = fis.read(arr))!= -1){ fos.write(arr,0,len); } fis.close(); fos.close(); } }
public class Demo009 { public static void main(String[] args) throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream("20.avi")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.avi")); int b; while((b = bis.read())!= -1){ bos.write(b); } bis.close(); bos.close(); }
字节流读中文的问题:可能出现读到半个中文乱码的情况
package com.test.demo001; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ FileInputStream fis = new FileInputStream("b.txt"); byte[] arr = new byte[2]; int len; while((len = fis.read(arr))!= -1){ System.out.println(new String(arr,0,len)); } } }
字节流写出中文的问题:
package com.test.demo001; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ FileOutputStream fos = new FileOutputStream("b.txt"); fos.write("你好世界".getBytes()); fos.write("\r\n".getBytes()); fos.write("你好世界".getBytes()); } }
package com.test.demo001; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("b.txt"); fos = new FileOutputStream("c.txt"); int b; while((b = fis.read())!= -1){ fos.write(b); } } finally { try { if(fis != null){ fis.close(); } } finally { fos.close(); } } } }
package com.test.demo001; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ try( FileInputStream fis = new FileInputStream("b.txt"); FileOutputStream fos = new FileOutputStream("c.txt"); ){ int b; while((b = fis.read())!= -1){ fos.write(b); } } } } class MyClose implements AutoCloseable { @Override public void close() throws Exception { System.out.println("我自动关闭了"); } }
package com.test.demo001; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Demo009 { public static void main(String[] args) throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream("dd.jpeg")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("ee.jpeg")); int len; while((len = bis.read())!= -1){ bos.write(len ^ 123); } bis.close(); bos.close(); } }
package com.test.demo001; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class Demo009 { public static void main(String[] args) throws IOException{ File file = getFile(); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file.getName())); int len; while((len = bis.read())!= -1){ bos.write(len); } bis.close(); bos.close(); } public static File getFile(){ Scanner sc = new Scanner(System.in); System.out.println("请输入一个文件路径"); while(true){ String line = sc.nextLine(); File file = new File(line); if(!file.exists()){ System.out.println("录入的路径文件不存在"); } else if(file.isDirectory()) { System.out.println("录入的路径是文件夹,请重新输入"); } else { return file; } } } }
package com.test.demo001; import java.io.FileOutputStream; import java.io.IOException; import java.util.Scanner; public class Demo009 { public static void main(String[] args) throws IOException{ Scanner sc = new Scanner(System.in); FileOutputStream fos = new FileOutputStream("text.txt"); System.out.println("请输入数据:"); while(true){ String line = sc.nextLine(); if("quit".equals(line)){ break; } fos.write(line.getBytes()); fos.write("\r\n".getBytes()); } } }
叨逼叨两句收拾收拾~18-11:Collection中的常见方法sortpackage com.test.demo001;System.out.println; //根据默认排序结果获取集合中的最大值