Java IO简介
字节流与字符流
字节流可以处理所有类型的数据,如MP3、图片、文字、视频等。在读取时,读到一个字节就返回一个字节
在Java中对应的类都以“Stream”结尾
字符流仅能处理纯文本数据,如txt文本等。在读取时,读到一个或者多个字节,先查找指定的编码表,然后将查到的字符返回。
在Java中对应的类都以“Reader”或“Writer”结尾
使用字节流读数据:
FileInputStream fis = new FileInputStream("text.txt"); byte[] input = new byte[20]; fis.read(input); // String inputString = new String(input); String inputString = new String(input, "UTF-8"); System.out.println(inputString); fis.close();
FileOutputStream fos = new FileOutputStream("textw.txt"); String outString = "write 123456写出数据"; byte[] output = outString.getBytes("UTF-8"); fos.write(output); fos.close();
FileInputStream fis = new FileInputStream("mao.mp3"); FileOutputStream fos = new FileOutputStream("mao_new.mp3"); byte[] input = new byte[50]; while (fis.read(input) != -1) { fos.write(input); } fis.close(); fos.close(); System.out.println("done");
使用带有缓冲的字符流读写数据:
FileInputStream fis = new FileInputStream("mao.mp3"); BufferedInputStream bis = new BufferedInputStream(fis, 100000); FileOutputStream fos = new FileOutputStream("mao_new1.mp3"); BufferedOutputStream bos = new BufferedOutputStream(fos, 100000); byte[] input = new byte[1000]; int count = 0; long before = System.currentTimeMillis(); // System.out.println(System.currentTimeMillis()); while (bis.read(input) != -1) { bos.write(input); count++; } bis.close(); fis.close(); bos.close(); fos.close(); System.out.println(System.currentTimeMillis() - before + "ms"); System.out.println(count + "times");
使用字符流读写数据:
// File file = new File("java.txt"); // FileInputStream fis = new FileInputStream(file); FileInputStream fis = new FileInputStream("java.txt"); FileOutputStream fos = new FileOutputStream("java_new.txt"); InputStreamReader isr = new InputStreamReader(fis, "GBK"); OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK"); char[] input = new char[100]; int l = 0; while ((l = isr.read(input)) != -1) { // System.out.println(input); // System.out.println(new String(input, 0, l)); // String inputString = new String(input, 0, l); osw.write(input, 0, l); } isr.close(); fis.close(); osw.close(); fos.close(); System.out.println("done");
Ctrl + i
代码格式化
使用带有缓冲的字符流读写数据:
// File file = new File("java.txt"); FileInputStream fis = new FileInputStream("java.txt"); FileOutputStream fos = new FileOutputStream("java_new_buff.txt"); InputStreamReader isr = new InputStreamReader(fis, "GBK"); OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK"); BufferedReader br = new BufferedReader(isr); // 使用BufferedWriter会导致换行符丢失 // BufferedWriter bw = new BufferedWriter(osw); PrintWriter pw = new PrintWriter(osw, true); // auto flush String input; while ((input = br.readLine()) != null) { // bw.write(input); pw.println(input); } // pw.flush(); br.close(); // bw.flush(); // bw.close(); pw.close(); isr.close(); fis.close(); osw.close(); fos.close(); System.out.println("done");
FileReader与FileWriter:
FileReader fr = new FileReader("java.txt"); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter("java_new.txt"); BufferedWriter bw = new BufferedWriter(fw); String line; while ((line = br.readLine()) != null) { bw.write(line + "\n"); } bw.flush(); bw.close(); fw.close(); br.close(); fr.close(); System.out.println("done");