TreeSet迭代
1 import java.util.Iterator; 2 import java.util.TreeSet; 3 4 public class TreeSetDemo2 { 5 6 public static void main(String[] args) { 7 TreeSet<String> ts = new TreeSet<String>(); 8 ts.add("tom1"); 9 ts.add("tom3"); 10 ts.add("tom2"); 11 ts.add("tom5"); 12 13 //得到迭代器 14 Iterator<String> it = ts.iterator(); 15 while (it.hasNext()) { 16 String tmp = it.next(); 17 System.out.println(tmp); 18 } 19 } 20 21 }
Map迭代
- entry
- keySet
- valu
1 import java.util.Collection; 2 import java.util.HashMap; 3 import java.util.Iterator; 4 import java.util.Map; 5 import java.util.Map.Entry; 6 import java.util.Set; 7 8 public class HashMapDemo { 9 10 public static void main(String[] args) { 11 Map<String, String> map = new HashMap<String, String>(); 12 map.put("key001", "tom"); 13 map.put("key002", "tom2"); 14 map.put("key003", "tom3"); 15 map.put("key004", "tom4"); 16 map.put("key005", "tom5"); 17 map.put("key005", "tom6"); 18 19 //通过Entry迭代 20 Set<Entry<String, String>> set = map.entrySet(); 21 Iterator<Entry<String, String>> it = set.iterator(); 22 while (it.hasNext()) { 23 Entry <String, String> entry = it.next(); 24 String key = entry.getKey(); 25 String value = entry.getValue(); 26 System.out.println(key + "===" + value); 27 } 28 29 //通过key迭代 30 Set<String> keySet = map.keySet(); 31 Iterator<String> keyIt = keySet.iterator(); 32 while (keyIt.hasNext()) { 33 String key = keyIt.next(); 34 String value = map.get(key); 35 System.out.println(key + "===" + value); 36 } 37 38 //通过value迭代 39 Collection<String> valueSet = map.values(); 40 Iterator<String> valueIt = valueSet.iterator(); 41 while (valueIt.hasNext()) { 42 String value = valueIt.next(); 43 System.out.println(value); 44 } 45 46 } 47 48 }
装饰模式
实现方式: Buffered类继承Writer类,在类中添加Writer类型的成员变量,对相应方法进行重写时,调用成员变量的方法进行完成。 示例如下:
1 class BufferedWriter extends Writer{ 2 Writer out; 3 char[] cb = new char[8192]; 4 5 public void writer(String str){ 6 // 1.将数据写入缓冲区 7 cb.xxx 8 // 2.如果cb已满写入out 9 } 10 11 public void close() { 12 // 1.清理cb 13 // 2.关闭out 14 } 15 }
比较FileWriter和BufferedWriter写入效率
1 import java.io.BufferedWriter; 2 import java.io.FileWriter; 3 import java.io.IOException; 4 5 public class CompareBufferedWriter { 6 7 public static void main(String[] args) { 8 fileWriter(); 9 bufferedWriter(); 10 11 12 } 13 14 private static void bufferedWriter() { 15 // BufferedWriter 16 long l = System.currentTimeMillis(); 17 String line = System.getProperty("lline.separator"); 18 BufferedWriter writer = null; 19 try { 20 writer = new BufferedWriter(new FileWriter("h:\hello2.txt", false)); 21 for (int i = 0; i < 10000000; i++) { 22 writer.write(i + line); 23 } 24 } catch (IOException e) { 25 e.printStackTrace(); 26 } finally { 27 if (writer != null) { 28 try { 29 writer.close(); 30 System.out.println("BufferedWriter: " + (System.currentTimeMillis() - l)); 31 } catch (IOException e) { 32 e.printStackTrace(); 33 } 34 } 35 } 36 } 37 38 private static void fileWriter() { 39 // FileWriter 40 long l = System.currentTimeMillis(); 41 String line = System.getProperty("lline.separator"); 42 FileWriter writer = null; 43 try { 44 writer = new FileWriter("h:\hello1.txt", false); 45 for (int i = 0; i < 10000000; i++) { 46 writer.write(i + line); 47 } 48 } catch (IOException e) { 49 e.printStackTrace(); 50 } finally { 51 if (writer != null) { 52 try { 53 writer.close(); 54 System.out.println("FileWriter: " + (System.currentTimeMillis() - l) ); 55 } catch (IOException e) { 56 e.printStackTrace(); 57 } 58 } 59 } 60 } 61 62 }
控制台输出的结果(ms):
FileWriter: 7925 BufferedWriter: 3902
比较FileReader和BufferedReader的效率
1 import java.io.BufferedReader; 2 import java.io.FileNotFoundException; 3 import java.io.FileReader; 4 import java.io.IOException; 5 import java.io.LineNumberReader; 6 7 import org.junit.Test; 8 9 public class BufferedReadTest { 10 11 public static void main(String[] args) throws Exception { 12 ReadFileNoBuffered(); 13 14 ReadFileWithBuffered(); 15 ReadFileWithBuffered2(); 16 17 } 18 19 private static void ReadFileWithBuffered() throws FileNotFoundException, IOException { 20 BufferedReader br = new BufferedReader (new FileReader("d:\hello.txt")); 21 long start = System.currentTimeMillis(); 22 int len = -1; 23 24 while ((len = br.read()) != -1) { 25 26 } 27 br.close(); 28 System.out.println("ReadFileWithBuffered :" + (System.currentTimeMillis() - start) ); 29 } 30 31 private static void ReadFileNoBuffered() throws FileNotFoundException, IOException { 32 FileReader reader = new FileReader("d:\hello.txt"); 33 long start = System.currentTimeMillis(); 34 int len = -1; 35 while ((len = reader.read()) != -1) { 36 37 } 38 reader.close(); 39 System.out.println("ReadFileNoBuffered :" + (System.currentTimeMillis() - start) ); 40 } 41 /** 42 * 考察磁盘的read性能 43 * @throws FileNotFoundException 44 * @throws IOException 45 */ 46 private static void ReadFileWithBuffered2() throws FileNotFoundException, IOException { 47 BufferedReader br = new BufferedReader (new FileReader("d:\hello.txt")); 48 long start = System.currentTimeMillis(); 49 int len = -1; 50 char[] cbuf= new char[68888898]; 51 br.read(cbuf); 52 br.close(); 53 System.out.println("ReadFileWithBuffered2 :" + (System.currentTimeMillis() - start) ); 54 } 55 public void hexNum() { 56 int i = 5; 57 i = 0x5; 58 i = -0x5; 59 60 //byte b = 0x80;//error 61 byte b = -0x80;//- 1000 0000 -128 byte(-128~127) 62 } 63 64 /** 65 * 测试BufferedReader.readLine() 66 * 取一整行文本 67 * @throws Exception 68 */ 69 @Test 70 public void readLineWithBuffered3() throws Exception{ 71 BufferedReader br = new BufferedReader(new FileReader("d:\hello.txt")); 72 String line = null; 73 74 while ((line = br.readLine()) != null){ 75 System.out.println(line); 76 } 77 br.close(); 78 } 79 80 /** 81 * 测试LineNumberReader.getLineNumber() 82 * @throws Exception 83 */ 84 @Test 85 public void lineNumbreReaderTest() throws Exception{ 86 LineNumberReader br = new LineNumberReader(new FileReader("d:\hello.txt")); 87 String line = null; 88 System.out.println(br.getLineNumber()); 89 while ((line = br.readLine()) != null){ 90 //br.setLineNumber(100); 91 System.out.println(br.getLineNumber()); 92 } 93 br.close(); 94 } 95 96 }
控制台输出结果:
ReadFileNoBuffered :8417
ReadFileWithBuffered :1713
ReadFileWithBuffered2 :218
字节流的读写
1 import java.io.FileInputStream; 2 import java.io.FileOutputStream; 3 4 import org.junit.Test; 5 6 7 public class ByteStreamDemo { 8 /** 9 * 复制图片 10 * @author zhengguohuang 11 * 12 */ 13 @Test 14 public void copyImage() throws Exception{ 15 FileInputStream fin = new FileInputStream("h:\testPicture\clipboard.png"); 16 FileOutputStream fout = new FileOutputStream("d:\1.png"); 17 18 byte[] buffer = new byte[1024]; 19 int len = -1; 20 while ((len = fin.read(buffer)) != -1) { 21 fout.write(buffer, 0, len); 22 } 23 fin.close(); 24 fout.close(); 25 } 26 }
字节流
-
FileInputStream
- 支持skip()方法,skip向后跳的时候不能超过文件头地址,可以超过尾地址
-
FileOutputStream
- 不支持持skip
-
RandomAccessFile
- 随机访问文件,定位到文件的任意位置
1 /** 2 * 使用文件输出流写文本文件 3 * 4 * @throws Exception 5 */ 6 @Test 7 public void writeWithFileOutputStream() throws Exception { 8 System.out.println(Charset.defaultCharset()); 9 FileOutputStream fos = new FileOutputStream("d://hello.txt"); 10 11 String str = "你adaac"; 12 // 编码7 bytes 13 // fos.write(str.getBytes("GBK")); 14 // 编码6 bytes 15 // fos.write(str.getBytes("ios8859-1")); 16 17 // 编码8 bytes 18 fos.write(str.getBytes("utf-8")); 19 fos.close(); 20 System.out.println("over"); 21 } 22 23 /** 24 * 使用文件输入流读取文本 25 * @throws Exception 26 */ 27 @Test 28 public void readFileWithFileInputStream() throws Exception { 29 FileInputStream fis = new FileInputStream("d://hello.txt"); 30 char c = (char)fis.read();//读一个字节 31 fis.skip(3);//跳过三个字节,可前可后 32 c = (char)fis.read();//读一个字节 33 System.out.println(c); 34 }