一、按数据来源(去向)分类:
1、是文件: FileInputStream, FileOutputStream, FileReader, FileWriter
2、是byte[]:ByteArrayInputStream, ByteArrayOutputStream
3、是Char[]: CharArrayReader, CharArrayWriter
4、是String: StringBufferInputStream, StringReader, StringWriter
5、网络数据流:InputStream, OutputStream, Reader, Writer
二、按是否格式化输出分:
1、要格式化输出:PrintStream, PrintWriter
三、按是否要缓冲分:
1、要缓冲:BufferedInputStream, BufferedOutputStream, BufferedReader, BufferedWriter
四、按数据格式分:
1、二进制格式(只要不能确定是纯文本的): InputStream, OutputStream及其所有带Stream结束的子类
2、纯文本格式(含纯英文与汉字或其他编码方式);Reader, Writer及其所有带Reader, Writer的子类
五、按输入输出分:
1、输入:Reader, InputStream类型的子类
2、输出:Writer, OutputStream类型的子类
文件拷贝
try { File inputFile = new File(args[0]); if (!inputFile.exists()) { System.out.println("源文件不存在,程序终止"); System.exit(1); } File outputFile = new File(args[1]); InputStream in = new FileInputStream(inputFile); OutputStream out = new FileOutputStream(outputFile); byte date[] = new byte[1024]; int temp = 0; while ((temp = in.read(date)) != -1) { out.write(date); } in.close(); out.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
String fileName = "D:/date.java.bak"; // String fileName = "D:/test.qqq"; String line; int i = 0, j = 0, f = 0, k = 0; try { BufferedReader in = new BufferedReader(new FileReader(fileName)); line = in.readLine(); while (line != null) { // System.out.println(line); char c[] = line.toCharArray(); for (int i1 = 0; i1 < c.length; i1++) { // 如果是字母 if (Character.isLetter(c[i1])) i++; // 如果是数字 else if (Character.isDigit(c[i1])) j++; // 是空格 else if (Character.isWhitespace(c[i1])) f++; } line = in.readLine(); k++; } in.close(); System.out .println("字母:" + i + ",数字:" + j + ",空格:" + f + ",行数:" + k); } catch (IOException e) { e.printStackTrace(); }
try { BufferedReader br = new BufferedReader(new FileReader( "D:\\test.txt")); StringBuilder sb = new StringBuilder(); while (true) { String str = br.readLine(); if (str == null) break; sb.append(str); } Pattern p = Pattern.compile("aa"); Matcher m = p.matcher(sb); int count = 0; while (m.find()) { count++; } System.out.println("\"aa\"一共出现了" + count + "次"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }