1.IO流分类
1)字节流 InputStream :将外部数据读取到内存中
File file = new File("文件地址/自定义文件名"); InputStream input = new FileInputStream(file); // 进行读取操作 byte b[] = new byte[1024]; input.read(b); input.close(); System.out.println("内容为:" + new String(b)); // 把byte数组变为字符串输出
2)字节流 OutputStream:从内存将数据写入设备中
File file = new File("文件地址/自定义文件名"); // 声明File对象 OutputStream out = new FileOutputStream(file); // 进行写入操作 String str = "Hello World!!!"; byte b[] = str.getBytes(); // 只能输出byte数组,所以将字符串变为byte数组 out.write(b); // 将内容输出,保存文件 文件不存在会自动创建 out.close() ;
3)字节流 FileInputSream:将文件数据以字节形式读取
File file = new File("文件地址/自定义文件名"); FileInputStream fis = new FileInputStream(file); // 调用read方法读取 byte[] name = new byte[12]; int count = fis.read(name); fis.close(); System.out.println(count+" "+new String (name));
4)字节流 FileOutputSream:将文件以字节形式写入数据
File file = new File("文件地址/自定义文件名"); FileOutputStream fos = new FileOutputStream(file); // 调用write方法写入 byte[] text = {'1','2','3','4'}; fos.write(text); fos.close();
5)字节流 BufferedInputStream:将文件数据以字节形式读取
String sourcePath = "C:/Users/ASUS/Desktop/1.PNG"; FileInputStream fin = new FileInputStream("file1.txt"); BufferedInputStream bin = new BufferedInputStream(fin); System.out.println(bin.available()); bis.close();
6)字节流 BufferedOutputStream:将文件以字节形式写入数据
// 保存文件的地址 String desPath = "F:/homework/java/day1/java/src/main/java/day8/1.jpeg"; FileOutputStream os = new FileOutputStream(desPath); BufferedOutputStream bos = new BufferedOutputStream(os); byte[] in = new byte[1024]; int count = 0; while ((count = bis.read(in)) != -1){ bos.write(in,0,count); } bos.close();
7)字符流 BufferedReader:将文件数据以字符形式读取
File file = new File("文件地址/自定义文件名"); BufferedReader br = new BufferedReader(new FileReader(file)); String str = null; while((str = br.readLine()) != null){ System.out.println(str); //一行一行地读取 } br.close() ;
8)字符流 BufferedWriter:将文件以字符形式写入数据
File file = new File("文件地址/自定义文件名"); // 实例化BufferedReader对象 BufferedReader br = new BufferedReader(new FileReader(file)); // 写入数据 BufferedWriter.Write("I'm so tired"); BufferedWriter.newLine(); BufferedWriter.write("I want to sleep!!!"); br.close() ;
9)字符流 FileReader:按字符往流中写入数据
File file = new File("文件地址/自定义文件名"); FileReader fr = new FileReader(file);读取数据 char[] book = new char[4]; int count = fr.read(book); fr.close(); System.out.println(count+" "+new String (book));
10)字符流 FileWriter:按字符往流中写入数据
File file = new File("文件地址/自定义文件名"); FileWriter fw = new FileWriter(file); //写入数据 char[] name = {'困','死','我','啦'}; fw.write(name); fr.close();
2.BIO、NIO、AIO
3.java的泛型机制
6.List、set 和Map
1)List:接口 按对象进入顺序保存对象,允许有重复
API:.add(val) 添加,.remove(index) 删除; .set(index,val) 修改; .get(index) .contain(val) .indexof(val)
实现类:ArraryList 动态数组,查询,修改效率高;
LinkedList 链表结构,增加,删除效率高;(Deque 队列 先进先出)
Vector 和 ArrayList 形似,动态数组, 线程安全。(Stack 栈 后进先出)
List集合遍历:
public class ListFor { public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add(1,"dxj"); //for for (int i=0;i<list.size();i++){ System.out.println(list.get(i)); } //foreach for (String t:list) { System.out.println(t); } //迭代器 Iterator Iterator<String> iterator=list.iterator(); while (iterator.hasNext()){ System.out.println(iterator.next()); } } }
2)Set: 接口 无序、不重复 无索引
实现类:HashSet(底层基于HashMap) TreeSet LinkedHashSet
利用Set集合解决List重复问题:
public class ListFor { public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add(1,"dxj"); Set<String> set=new HashSet<>(); set.addAll(list); System.out.println(set); list.addAll(set); System.out.println(list); } }
3)Map: <key,value> 键值对
API:.put(key,value) 添加;.remove(key) 删除; .get(key)
实现类:HashMap HashTable LinkedHashMap(输入和输出顺序一致)
Map集合遍历:
public class MapFor { public static void main(String[] args) { Map<String,String> map=new HashMap<>(); map.put("1", "d"); //遍历所有的key,将key装入set集合中,获取所有的值 for(Object key:map.keySet()){ System.out.println(key+":"+map.get(key)); } //获取所有的值,将值装进Collection集合 for(Object value:map.values()){ System.out.println(value); } //将绑定entry对象放入set集合 for(Map.Entry<String,String> e:map.entrySet()){ System.out.println( e.getKey() +":"+e.getValue()); } //通过Map.entrySet使用iterator遍历key和value: Iterator<Map.Entry<String, String>> it = map.entrySet().iterator(); while (it.hasNext()) { Map.Entry<String, String> entry = it.next(); System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue()); } } }
7.ArrayList和Vector
8.ArrayList和LinkedList
9.HashMap HashTable ConcurrentHashMap TreeMap
1)HashMap:keyvalue可为null;非线性安全
2)HahTable:继承自Dictionary类;keyvalue不可为null;线性安全
3)TreeMap:基于红黑树;线性不安全
4)ConcurrentHashMap :和HashMap比,线程安全,比HashTable快;keyvalue不可为空