知识点一:Collection知识总结
(1)Set:无序不可重复
HashSet:使用哈希算法实现的Set集合
散列表,因为对象在数组中是散列保存的,越散列性能越好。
唯一缺点:要求内存连续。
LinkedHashSet。
TreeSet:基于二叉搜索树(红黑树)实现的Set集合,应用场景:频繁的检索数据,修改数据不多。
优点:对内存要求低,不要求连续,检索速度非常快,基于二分法。
缺点:插入删除速度非常慢。
(2)List有序可重复
ArrayLIst:基于数组实现的List,应用场景:适用于检索数据,修改数据少
删除或插入,arraycopy(源…,目标…),效率低
缺点:对内存要求高,要求连续,非末端数据的插入或删除时最慢的,因为有大量的元素移动。
优点;检索速度快。末端数据处理最快。
LInkedList:基于链表实现的List集合,应用场景:频繁的用用于修改数据。
优点:对内存要求低,不要求连续,插入和删除操作极快,因为只是修改两个指针
缺点:检索速度最慢,因为需要动态定位每个结点对象。
知识点二:IO流总结
IO流:数据从源节点流向目标节点,这样的流称为节点流
字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer
文件流:①FileInputStream,②FileOutputStream
缓冲流:③BufferedReader,④BufferedWriter
对象流:⑤ObjectInputStream,⑥ObjectOutptStream
转换流:⑦InputStreamReader,⑧OutputStreamWriter
(1)使用场景组合一:①⑤组合为读取一个二进制文件或者对象序列化
@Test
public void test2() {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("50个随机数");
ois = new ObjectInputStream(fis);
for (int i = 0; i < 50; i++) {
int n = ois.readInt();
System.out.println(n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
补充:编码和解码
@Test
public void test4() throws UnsupportedEncodingException {
int n = 0x6211;
System.out.println(n);
System.out.println((char)n);
int n2 = 0xCED2;
System.out.println(n2);
System.out.println((char)n2);
// 编码 : 字符串 => 字节数组
String string = "abc我和你qqq";
byte[] bytes1 = string.getBytes("utf8"); // 默认使用utf8 编码
for (int i = 0; i < bytes1.length; i++) {
System.out.print(Integer.toHexString(bytes1[i]) + " ");
}
System.out.println();
byte[] bytes2 = string.getBytes("gbk");
for (int i = 0; i < bytes2.length; i++) {
System.out.print(Integer.toHexString(bytes2[i]) + " ");
}
System.out.println();
// 解码 : 字节数组 => 字符串
String string1 = new String(bytes2, "utf8"); // 尝试用的解码方式是utf8.
System.out.println(string1);
String string2 = new String(bytes1, "utf8");
System.out.println(string2);
String string3 = new String(bytes2, "gbk");// 指定用gbk方式解码bytes2
System.out.println(string3);
String string4 = new String(bytes1, "gbk");
System.out.println(string4);
// 在java程序中, String字符串永远是Unicode字符串.
}
@Test
public void test6() {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("对象序列化");
ois = new ObjectInputStream(fis);
/*
Object object1 = ois.readObject();
Object object2 = ois.readObject();
Object object3 = ois.readObject();
System.out.println(object1);
System.out.println(object2);
System.out.println(object3);
System.out.println(((Student)object1).school);
*/
/*
Student[] arr = (Student[])ois.readObject();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}*/
List<Student> list = (List<Student>)ois.readObject();
Iterator<Student> iterator = list.iterator();
while (iterator.hasNext()) {
Student next = iterator.next();
System.out.println(next);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
对象序列化 的一定要对类实现Serializable接口,transient是一个反序列化的修饰符,可以对属性进行修饰,下面是对Student类的编写:
class Student implements Serializable {
private static final long serialVersionUID = 5;
static String school = "atguigu";
private int id;
private String name;
private int grade;
private transient double score;
public Student() {}
public Student(int id, String name, int grade, double score) {
this.id = id;
this.name = name;
this.grade = grade;
this.score = score;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", grade=" + grade +
", score=" + score +
'}';
}
}
(2)使用场景二:②⑥组合为写一个二进制文件
@Test
public void test1() {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("50个随机数");
oos = new ObjectOutputStream(fos);
for (int i = 0; i < 50; i++) {
oos.writeInt((int) (Math.random() * 100));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
@Test
public void test5() {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("对象序列化");
oos = new ObjectOutputStream(fos);
// writeObject(),
Student s1 = new Student(1, "小明", 5, 20);
Student s2 = new Student(2, "小丽", 1, 90);
Student s3 = new Student(3, "小刚", 4, 30);
Student.school = "sgg";
/*
oos.writeObject(s1);
oos.writeObject(s2);
oos.writeObject(s3);
*/
//Student[] arr = {s1, s2, s3};
//oos.writeObject(arr);
List<Student> list = new LinkedList<>();
list.add(s1);
list.add(s2);
list.add(s3);
oos.writeObject(list);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(3)①⑦③组合为利用转换流读取一个文件,其还可以和打印流一块使用,可以实现跨码的读取
@Test
public void test2() {
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader bufferedReader = null;
try {
fis = new FileInputStream("LinkedList.java");
isr = new InputStreamReader(fis, "gbk"); // 在转码时请注意, 所有字节数据都是gbk编码的文本
bufferedReader = new BufferedReader(isr);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
打印流的
public static void main2(String[] args) {
System.out.write('a'); // 打印流, 可以自动刷新.
System.out.flush();
System.out.println("******************************************");
InputStream is = System.in;
InputStreamReader isr = null;
BufferedReader bufferedReader = null;
try {
isr = new InputStreamReader(is);
bufferedReader = new BufferedReader(isr);
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.equals("exit")) {
break;
}
System.out.println(line);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
(4)②⑧④组合利用转换流写一个文件,可以实现跨码的写
@Test
public void test4() {
FileOutputStream fos = null;
OutputStreamWriter osw = null; // 转换流
BufferedWriter bufferedWriter = null;
try {
fos = new FileOutputStream("一个文本文件_gbk", true); // 输出流默认是以清空的方式写文件. 如果是第2个参数为true,表明是以追加方式写文件
osw = new OutputStreamWriter(fos, "gbk");
bufferedWriter = new BufferedWriter(osw);
bufferedWriter.write("abc");
bufferedWriter.newLine();
bufferedWriter.write("我是汉字");
bufferedWriter.newLine();
bufferedWriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
测试: 从键盘获取一些字符串, 把这些写入一个文件keyboard_gbk.txt.直到键盘输入quit为止.
FileOutputStreamOutputStreamWriter BufferedWriter
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(System.in));
bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("keyboard_gbk.txt"), "gbk"));
String line;
while ((line = bufferedReader.readLine()) != null) {
if (line.equals("quit")) {
break;
}
bufferedWriter.write(line);
bufferedWriter.newLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
知识点三:File知识总结
public class FileTest {
public static void main(String[] args) {
File file = new File("一个文本文件");
// 针对文件的操作, 创建文件, 删除文件, 重命名文件....
System.out.println("file.getAbsolutePath() : " + file.getAbsolutePath());
System.out.println("file.canRead() : " + file.canRead());// 是否能读
System.out.println("file.isFile() : " + file.isFile()); // 是不是文件
System.out.println("file.isDirectory() : " + file.isDirectory()); // 是否是目录
System.out.println("file.exists() : " + file.exists()); // 是否存在
System.out.println("file.length() : " + file.length()); // 获取文件长度
System.out.println("file.lastModified() : " + file.lastModified());
System.out.println("file.getTotalSpace() : " + file.getTotalSpace());
System.out.println("file.getFreeSpace() : " + file.getFreeSpace());
System.out.println("file.getName() : " + file.getName());
//System.out.println("file.delete() : " + file.delete());
}
}