功能流
功能流都要包裹节点流使用
缓冲流:功能流
作用:提高读写效率
字节节点流:
FileInputStream 文件流 | ByteArrayInputStream 字节数组流
FileOutputStream | ByteArrayOutputStream
字节缓冲流:字节流功能流中的一种
BufferedInputStream 字节输入缓冲流
BufferedOutputStream 字节输出缓冲流无新增功能,可以发生多态
字符节点流和他的缓冲流
FileReader BufferedReader
FileWriter BufferedWriter
InputStream is = new BufferedInputStream(new FileInputStream("D://xxxx"));
OutputStream os = new BufferedOutputStream(new FileOutputStream("D://xxxx"));
基本数据类型流
data流
功能:是功能流 操作单元:字节流
作用:保留数据自身的数据类型(基本数据类型+string)
DataInputStream
新增功能: readXxx()
DataOutputStream
新增功能:writeXxx()
注意:
读入和写出的顺序要保持一致
可能会出现 java.io.EOFException : 读入的文件不是源文件
public static void main(String[] args) throws IOException {
readFromFile("D:/d.txt");
}
//读入
public static void readFromFile(String path) throws IOException {
//1.创建流
DataInputStream is = new DataInputStream(new BufferedInputStream(new FileInputStream(path)));
//2.读入
int i = is.readInt();
boolean flag = is.readBoolean();
char ch = is.readChar();
String s = is.readUTF();
//3.处理数据
System.out.println(i);
System.out.println(flag);
System.out.println(ch);
System.out.println(s);
//4.关闭
is.close();
}
//写出
public static void writeToFile(String path) throws IOException {
//1.创建输出流
DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(path)));
//2.准备数据
int i = 1;
boolean flag = false;
char ch = 'c';
String str = "哈哈";
//3.写出
out.writeInt(i);
out.writeBoolean(flag);
out.writeChar(ch);
out.writeUTF(str);
//4.刷出
out.flush();
//5.关闭
out.close();
}
Object 流
object流 也可以叫对象流、引用数据类型流
作用:读写对象数据|引用数据类型的数据(包含基本数据类型)
ObjectInputStream 反序列化输入流 新增功能: readObject() ObjectOutputStream 序列化输出流 新增功能: writeObject(Object obj)
序列化:将对象数据转换一个可存储或者可传输的状态过程
先序列化在反序列化
- 只有实现了序列化接口的类才可以序列化(java.io.Serializable)
- transient 修饰的字段不会序列化
- 静态的内容不会被序列化
- 如果父类实现序列化,子类实现序列化,可以序列化所有的成员
- 如果子类实现序列化,父类实现,只能序列化子类成员
通过序列号控制版本不一致问题:
- 实现Serializable接口的类型会默认生成序列号,序列号会根据成员的修改做更新
- 控制工具生成序列号:控制类型i需改之前之后的序列号不变
- 通过工具生成序列号:
- 实现Serializable接口
- 在设置中改设置
- 选中类名->alt + enter ->生成序列号
public class Class001_Object { public static void main(String[] args) throws IOException, ClassNotFoundException { //writeToFile("D://e.txt"); readFile("D://e.txt"); } //反序列化 public static void readFile(String path) throws IOException, ClassNotFoundException { //1.输入流 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(path))); //2.读入 User obj1 = (User) in.readObject(); int[] arr = (int[]) in.readObject(); //3.处理数据 System.out.println(obj1); System.out.println(Arrays.toString(arr)); //4.关闭 in.close(); } //序列化输出 public static void writeToFile(String path) throws IOException { //1.输出流 ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(path))); //2.数据 User user = new User("zhangsan",18,"123"); int[] arr = {1,2,3,4}; //3.写出 out.writeObject(user); out.writeObject(arr); //4.刷出 out.flush(); //5.关闭 out.close(); //修饰user对象的成员,静态 user.username = "lisi"; user.password = "4321"; } } class User implements Serializable{ private static final long serialVersionUID = -5204947308809070324L; public String username; //transient 修饰的字段不会序列化 public transient int age; public static String password; //成员的修改: 新增的成员 public int vip; public int id; //用户编号 public User() { } public User(String username, int age, String password) { this.username = username; this.age = age; this.password = password; } @Override public String toString() { return "User{" + "username='" + username + '\'' + ", age=" + age + ", password='" + password + '\'' + '}'; } }
集合
容器|集合 1.容量跟随内容的多少进行动态的增删 2.存储任意引用类型数据
public class Class001_App {
public static void main(String[] args) {
//容器类型
ArrayList list = new ArrayList();
list.add("zhangsan");
list.add(123);
System.out.println(list.size());
System.out.println(list.get(0));
System.out.println(list.get(1));
MyContainer my = new MyContainer();
my.add("abc");
my.add("efg");
System.out.println(my.size());
my.add("efg");
my.add("22");
my.add("23");
my.add("24");
my.add("25");
System.out.println(my.size());
System.out.println(my.get(0));
System.out.println(my.get(1));
System.out.println(my.get(2));
System.out.println(my.get(2));
System.out.println(my.get(3));
System.out.println(my.get(4));
System.out.println(my.get(5));
System.out.println(my.get(6));
System.out.println("-----------------");
my.set(1,"sss");
System.out.println(my.get(1));
my.remove(1);
System.out.println("-----------------------");
System.out.println(my.get(0));
System.out.println(my.get(1));
System.out.println(my.get(2));
System.out.println(my.get(2));
System.out.println(my.get(3));
System.out.println(my.get(4));
System.out.println(my.get(5));
System.out.println(my.size());
}
}
//封装自定义容器类型 : 只能存储字符串类型的数据
class MyContainer{
//存储数据的结构
private String[] elementData;
//存储数据的个数
private int size;
public MyContainer() {
}
//添加方法
public void add(String value) {
//判断是否为第一次添加数据
if(elementData==null && size==0){
//创建新数组
elementData = new String[1];
//直接把value放入数组
elementData[0] = value;
//长度+1
size++;
return;
}
//备份原数组: 用来做数组数据拷贝
String[] temp = elementData;
//创建新数组
elementData = new String[size+1];
//1)数组数据的拷贝 i作为新数组与原数组索引
for(int i=0;i<size;i++){
elementData[i] = temp[i];
}
//2)添加新数据
elementData[size++] = value;
//长度+1
}
//size
public int size(){
return this.size;
}
/**
* 根据索引获取数据
* @param index 索引
* @return index索引位置的数据
*/
public String get(int index) {
if(index<0 || index>=size){
throw new IndexOutOfBoundsException("索引越界啦!!!");
}
return elementData[index];
}
//修改制定索引位置的数据
public void set(int index, String value) {
if(index<0 || index>=size){
throw new IndexOutOfBoundsException("请睁大你的眼睛!!!");
}
elementData[index]=value;
}
//根据索引删除
public void remove(int index) {
if(index<0 || index>=size){
throw new IndexOutOfBoundsException("请睁大你的眼睛!!!");
}
//判断索引位置
if(index==size-1){
elementData[index] =null;
size --;
}else {
//拷贝数组
String[] temp = elementData;
for (int i =index; i<size-1;i++){
elementData[i]=temp[i+1];
}
size --;
}
}
}
Collection
集合层次结构中的根接口。 集合表示一组对象,称为其元素 。
public static void main(String[] args) {
//集合
Collection col = new ArrayList();
Collection col2 = new ArrayList();
//常用方法
//boolean add(E e) 确保此集合包含指定的元素(可选操作)。
//boolean addAll(Collection<? extends E> c) 将指定集合中的所有元素添加到此集合中(可选操作)。
col.add("abc");
col.add(false);
col.add(100);
col2.add("aaa");
col2.add("bbb");
System.out.println(col);
col.addAll(col2);
System.out.println(col);
//boolean contains(Object o) 如果此collection包含指定的元素,则返回 true 。
//boolean containsAll(Collection<?> c) 如果此集合包含指定集合中的所有元素,则返回 true 。
System.out.println(col.contains(100));
col2.add("ccc");
System.out.println(col.containsAll(col2));
//boolean remove(Object o) 从此集合中移除指定元素的单个实例(如果存在)(可选操作)。
//boolean removeAll(Collection<?> c) 删除此集合的所有元素,这些元素也包含在指定的集合中(可选操作)。
System.out.println(col.remove(100));
System.out.println(col);
//System.out.println(col.removeAll(col2));
//System.out.println(col);
//boolean retainAll(Collection<?> c) 仅保留此集合中包含在指定集合中的元素(可选操作)。
System.out.println(col.retainAll(col2));
System.out.println(col);
System.out.println(col2);
//Object[] toArray() 返回包含此集合中所有元素的数组。
System.out.println(Arrays.toString(col.toArray()));
}
工具包 CommonsIO
CommonsIO 是apache的一个开源的工具包,封装了IO操作的相关类,使用Commons IO可以很方便的读写文件,url源代码等。 使用第三方组件的方式|步骤: 1.下载源码资源,找到核心jar包 2.项目下新建文件夹lib,放入jar包 3.commons-IO 需要加入classpath 的第三方 jar 包内的 class 文件才能在项目中使用 选中jar包右键->add as lib...
public static void main(String[] args) throws IOException {
//1.创建File对象
File src = new File("D://a.txt");
File dest = FileUtils.getFile("D://f.txt");
//FilenameUtils
//isExtension(String fileName, String text) // 判断fileName是否是text后缀名
//FilenameUtils.getBaseName(String filename) // 去除目录和后缀后的文件名
System.out.println(FilenameUtils.getBaseName("D://DDD/haha.txt"));;
System.out.println(FilenameUtils.getName("D://DDD/haha.txt"));;
System.out.println(FilenameUtils.isExtension("D://DDD/haha.txt","txt"));;
//FileUtils
//FileUtils.copyFile(File srcFile, File destFile)` // 复制文件
FileUtils.copyFile(src,dest);
// **复制文件夹**
//FileUtils.copyDirectory(File srcDir, File destDir)` // 复制文件夹(文件夹里面的文件内容也会复制)
FileUtils.copyDirectory(new File("D://DDD"), new File("D://hehe/DDD"));
//FileUtils.copyDirectory(File srcDir, File destDir, FileFilter filter)` // 复制文件夹,带有文件过滤功能
FileUtils.copyDirectory(new File("D://DDD"), new File("D://houhou/DDD"), FileFilterUtils.fileFileFilter());
//**把字符串写入文件**
//`FileUtils.writeStringToFile(File file, String data, String encoding)` // 字符串以指定的编码写到文件
//`FileUtils.writeStringToFile(File file, String data, String encoding, boolean append)`// 指定知否追加
FileUtils.writeStringToFile(src,"yyds!!!!","UTF-8",true);
}