示例:判断E盘目录下是否有后缀名为.jpg的文件,如果有,就输出此文件名称
//方式1File file = new File("e://");File[] fileArray = file.listFiles();for (File f : fileArray) { if (f.isFile()) { if (f.getName().endsWith(".jpg")) { System.out.PRintln(f.getName()); } }}//方式2File file = new File("e://");String[] strArray = file.list(new FilenameFilter() { @Override public boolean accept(File dir, String name) { // 把这个文件或者文件夹的名称加不加到数组中,取决于这里的返回值是true还是false return new File(dir, name).isFile() && name.endsWith(".jpg"); } });for (String s : strArray) { System.out.println(s);}IO用于在设备间进行数据传输的操作
分类: 1. 流向 输入流 读取数据 输出流 写出数据 2. 数据类型 字节流 字节输入流 字节输出流 字符流 字符输入流 字符输出流 注意: a:如果我们没有明确说明按照什么分,默认按照数据类型分。 b:除非文件用windows自带的记事本打开我们能够读懂的,才采用字符流,否则建议使用字节流。示例:字节流四种方式复制文件对比
/* * 基本字节流一次读写一个字节: 耗时:117235毫秒 * 基本字节流一次读写一个字节数组: 耗时:156毫秒 * 高效字节流一次读写一个字节: 耗时:1141毫秒 * 高效字节流一次读写一个字节数组: 耗时:47毫秒 */public static void main(String[] args) throws IOException { long start = System.currentTimeMillis(); method1("e://hello.mp4", "copy1.mp4"); method2("e://hello.mp4", "copy2.mp4"); method3("e://hello.mp4", "copy3.mp4"); method4("e://hello.mp4", "copy4.mp4"); long end = System.currentTimeMillis(); System.out.println("耗时:" + (end - start) + "毫秒"); } // 高效字节流一次读写一个字节数组: public static void method4(String srcString, String destString) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString)); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); } bos.close(); bis.close(); } // 高效字节流一次读写一个字节: public static void method3(String srcString, String destString) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString)); int by = 0; while ((by = bis.read()) != -1) { bos.write(by); } bos.close(); bis.close(); } // 基本字节流一次读写一个字节数组 public static void method2(String srcString, String destString) throws IOException { FileInputStream fis = new FileInputStream(srcString); FileOutputStream fos = new FileOutputStream(destString); byte[] bys = new byte[1024]; int len = 0; while ((len = fis.read(bys)) != -1) { fos.write(bys, 0, len); } fos.close(); fis.close(); } // 基本字节流一次读写一个字节 public static void method1(String srcString, String destString) throws IOException { FileInputStream fis = new FileInputStream(srcString); FileOutputStream fos = new FileOutputStream(destString); int by = 0; while ((by = fis.read()) != -1) { fos.write(by); } fos.close(); fis.close(); }Reader |– InputStreamReader |– OutputStreamWriter |– BufferedReader |– BufferedWriter |– FileReader |– FileWriter
字节流操作中文数据不是特别的方便,所以就出现了转换流。转换流的作用就是把字节流转换字符流来使用。 转换流其实是一个字符流 字符流 = 字节流 + 编码表
编码表 A:就是由字符和对应的数值组成的一张表 B:常见的编码表 ASCII ISO-8859-1 GB2312 GBK GB18030 UTF-8 C:字符串中的编码问题 编码String -- byte[] 解码 byte[] -- StringIO流中的编码问题 A:OutputStreamWriter OutputStreamWriter(OutputStream os):默认编码,GBK OutputStreamWriter(OutputStream os,String charsetName):指定编码。 B:InputStreamReader InputStreamReader(InputStream is):默认编码,GBK InputStreamReader(InputStream is,String charsetName):指定编码 C:编码问题其实很简单 编码只要一致即可示例:复制文本文件(5种方式)
/* * 分析: * 复制数据,如果用记事本打开并能够读懂,就用字符流,否则用字节流。 * 通过该原理,应该采用字符流更方便一些。 * 而字符流有5种方式。推荐掌握第5种。 */public class CopyFileDemo { public static void main(String[] args) throws IOException { String srcString = "c://a.txt"; String destString = "d://b.txt"; method1(srcString, destString); method2(srcString, destString); method3(srcString, destString); method4(srcString, destString); method5(srcString, destString); } //方式5:字符缓冲流一次读写一个字符串 private static void method5(String srcString, String destString) throws IOException { BufferedReader br = new BufferedReader(new FileReader(srcString)); BufferedWriter bw = new BufferedWriter(new FileWriter(destString)); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); br.close(); } //方式4:字符缓冲流一次读写一个字符数组 private static void method4(String srcString, String destString) throws IOException { BufferedReader br = new BufferedReader(new FileReader(srcString)); BufferedWriter bw = new BufferedWriter(new FileWriter(destString)); char[] chs = new char[1024]; int len = 0; while ((len = br.read(chs)) != -1) { bw.write(chs, 0, len); } bw.close(); br.close(); } //方式3:字符缓冲流一次读写一个字符 private static void method3(String srcString, String destString) throws IOException { BufferedReader br = new BufferedReader(new FileReader(srcString)); BufferedWriter bw = new BufferedWriter(new FileWriter(destString)); int ch = 0; while ((ch = br.read()) != -1) { bw.write(ch); } bw.close(); br.close(); } //方式2: 基本字符流一次读写一个字符数组 private static void method2(String srcString, String destString) throws IOException { FileReader fr = new FileReader(srcString); FileWriter fw = new FileWriter(destString); char[] chs = new char[1024]; int len = 0; while ((len = fr.read(chs)) != -1) { fw.write(chs, 0, len); } fw.close(); fr.close(); } //方式1:基本字符流一次读写一个字符 private static void method1(String srcString, String destString) throws IOException { FileReader fr = new FileReader(srcString); FileWriter fw = new FileWriter(destString); int ch = 0; while ((ch = fr.read()) != -1) { fw.write(ch); } fw.close(); fr.close(); }}可以操作任意类型的数据
种类: 1. 字节流打印流:PrintStream 2. 字符打印流:PrintWriter特点: 1. 只有写数据的,没有读取数据。只能操作目的地,不能操作数据源 2. 可以操作任意类型的数据 3. 如果启动了自动刷新,能够自动刷新 4. 该流是可以直接操作文本文件的看API,查流对象的构造方法,如果同时有File类型和String类型的参数,一般来说就是可以直接操作文件的,如:FileInputStream / FileOutputStream / FileReader / FileWriter / PrintStream / PrintWriter
/* * DataStreamDemo.java复制到Copy.java中 * 启动自动刷新 * PrintWriter pw = new PrintWriter(new FileWriter("pw2.txt"), true); * * println() * 等价于: * bw.write(); * bw.newLine(); * bw.flush(); */public class CopyFile { public static void main(String[] args) throws IOException { // 以前的版本 /* BufferedReader br = new BufferedReader(new FileReader("DataStreamDemo.java")); BufferedWriter bw = new BufferedWriter(new FileWriter("Copy.java")); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.newLine(); bw.flush(); } bw.close(); br.close(); */ // 打印流的改进版 BufferedReader br = new BufferedReader(new FileReader("DataStreamDemo.java")); PrintWriter pw = new PrintWriter(new FileWriter("Copy.java"), true); String line = null; while((line=br.readLine())!=null){ pw.println(line); } pw.close(); br.close(); }}/* * 合并文件:把ByteArrayStreamDemo.java和DataStreamDemo.java的内容复制到Copy.java中 * SequenceInputStream(InputStream s1, InputStream s2) */public class SequenceInputStreamDemo { public static void main(String[] args) throws IOException { InputStream s1 = new FileInputStream("ByteArrayStreamDemo.java"); InputStream s2 = new FileInputStream("DataStreamDemo.java"); SequenceInputStream sis = new SequenceInputStream(s1, s2); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Copy.java")); // 以前怎么读写,现在还是怎么读写 byte[] bys = new byte[1024]; int len = 0; while ((len = sis.read(bys)) != -1) { bos.write(bys, 0, len); } bos.close(); sis.close(); }}新闻热点
疑难解答