赞
踩
java IO流,对数据流进行输入输出,其中输入流的抽象类为InputStream,其抽象方法read()可以将数据读入到内存中,输出流的抽象类为OutputStream,其抽象方法write()可将数据流写出。
inputStream主要方法只有read(),读取字节文件到buf内存中,值得注意的是会中文乱码,所以需要自己转GBK或者UTF-8,当buf过小的时候会分多次读入到buf中,available() 方法可以返回文件长度,但是如果文件过大,会占用很大的内存,所以慎用。
- FileInputStream in = new FileInputStream("D:\\test\\abc.txt");
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) != -1) {
- String s = new String(buf, 0,len,"GBK");
- System.out.println(s);
- }
- in.close();
OutputStream核心方法write(),将数据流写出。
- FileInputStream in = new FileInputStream("D:\\test\\abc.txt");
- FileOutputStream out = new FileOutputStream("D:\\test\\cba.txt");
- byte[] buf = new byte[1024];
- int len;
- //循环写入buf中
- while ((len = in.read(buf)) != -1) {
- out.write(buf,0,len);
- }
- in.close();
- out.close();
Reader是在InputStream基础上提供了字符转码的功能,其还是调用inputStream,这字符流很好的解决了中文乱码的问题。
- FileReader fileReader = new FileReader("D:\\test\\abc.txt");
- int len;
- char[] buf = new char[1024];
- while ((len = fileReader.read(buf)) != -1) {
- String s = new String(buf, 0,len);
- System.out.println(s);
- }
- fileReader .close();
Writer需要调用flush()才能将数据流写出到指定文件。
- FileReader fileReader = new FileReader("D:\\test\\abc.txt");
- FileWriter out = new FileWriter("D:\\test\\cba.txt");
- int len;
- char[] buf = new char[1024];
- while ((len = fileReader.read(buf)) != -1) {
- out.write(buf,0,len);
- }
- //需要手动 out.flush(),close 包含了
- out.close();
- fileReader.close();
上面的代码我都是手动去创建一个buf缓存内存,去存储临时数据流,比较麻烦,而BufferedReader和BufferedWriter就帮我解决了这个临时数据存储的问题,不需要手动创建一个临时的数据缓存,同提供了一行一行去读取数据readLien()方法。
- BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\test\\abc.txt"));
- String str = null;
- while ((str = bufferedReader.readLine()) != null){
- System.out.println(str);
- }
- BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\test\\abc.txt"));
- Stream<String> lines = bufferedReader.lines();
- lines.forEach(System.out::println);
- BufferedReader in = new BufferedReader(new FileReader("D:\\test\\abc.txt"));
- BufferedWriter out = new BufferedWriter(new FileWriter("D:\\test\\cba.txt"));
- int len ;
- char [] buf = new char[1024];
- while ((len = in.read(buf)) != -1){
- out.write(buf,0,len);
- }
- in.close();
- out.close();
- {
-
- HashMap<String, String> pathMap = new HashMap<>(16);
- pathMap.put("D:\\test\\A.rar", "D:\\test\\a\\A.rar");
- pathMap.put("D:\\test\\B.rar", "D:\\test\\b\\B.rar");
-
- ExecutorService threadPool = Executors.newFixedThreadPool(2);
- System.out.println("主线程开始工作...");
-
- //使用多线程去处理,不影响主线程的工作
- pathMap.entrySet().forEach(entry -> {
- threadPool.submit(() -> {
- String key = entry.getKey();
- String value = entry.getValue();
-
- FileInputStream in = null;
- FileOutputStream out = null;
- long startTime = System.currentTimeMillis();
- System.out.println(Thread.currentThread().getName() + "线程搬运工 开始搬运" + key);
- try {
- in = new FileInputStream(key);
- out = new FileOutputStream(value);
- byte[] buf = new byte[1024];
- int len;
- while ((len = in.read(buf)) != -1) {
- out.write(buf, 0, len);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- if (out != null) {
- out.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- long endTime = System.currentTimeMillis();
- System.out.println(Thread.currentThread().getName() + "线程搬运工 搬运结束" + " 总耗时: " + (endTime - startTime) + " ms");
- //删除文件
- File file = new File(key);
- file.delete();
- });
-
- });
- System.out.println("主线程结束工作...");
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。