泛型
泛型就是限制我们得数据类型
使用泛型
List<类型> list=new ArrayList<类型>(); 只能在该集合中存储指定得类型。
public class Demo01 {
public static void main(String[] args) throws IOException {
//没有指定类型 可以存放任意类型数据
List list = new ArrayList();
list.add("java1");
list.add("java2");
list.add("java3");
list.add("java4");
//获取数据需要强制转换
String a = (String) list.get(0);
System.out.println(a);
}
}
===================================================================
public class Demo02 {
public static void main(String[] args) {
//jdk1.7以后new后<>可以省略
//限制了集合中每个数据的类型
List<String> list = new ArrayList<>();
list.add("java01");
list.add("java02");
list.add("java03");
list.add("java04");
//在获取集合中元素时,默认就是相应的类型 无需在进行转换
String s = list.get(0);
System.out.println(s);
//<K,V> K表示键的泛型 V 表示值的泛型
Map<String,Integer> map = new HashMap<>();
map.put("小明",12);
}
}
自定义泛型类
public class 类名<标识,标识....> {
标识 变量名;
public 标识 方法名(){
}
public void 方法名(标识 参数名){
}
.........
}
例:定义坐标
public class Demo03 {
public static void main(String[] args) {
//在创建泛型类对象时没有指定相应泛型类型,默认为Ob
Point<String> point = new Point();
point.setX("12");
point.setY("12");
point.show();
}
}
class Point<T>{
private T x;
private T y;
public void show(){
System.out.println("x轴坐标"+x+"y轴坐标"+y);
}
public T getX() {
return x;
}
public void setX(T x) {
this.x = x;
}
public T getY() {
return y;
}
public void setY(T y) {
this.y = y;
}
}
File对象
File 类 就是当前系统中 文件或者文件夹的抽象表示
通俗的讲 就是 使用File对象 来操作我们电脑系统中的文件或者文件夹
学习File类 其实就是学习 如果通过file对象 对系统中的文件/文件夹进行增删改查
创建File对象
1. 在磁盘准备一个目录AAA。对文件得操作都放在该目录下。
public class Demo04 {
public static void main(String[] args) throws IOException {
//创建File对象
File file = new File("D:/AAA/aaa.txt");
//第二种 File.separator
File file1 = new File("D:"+File.separator+"AAA"+File.separator);
}
}
增加操作
file.createNewFile();//创建文件
File file2 = new File("D:/AAA/aaa");
file2.mkdir();//创建目录 make directory 单机目录
File file3 = new File("D:/AAA/aaa/bbb");
file3.mkdirs();//多级目录
删除操作
public class Demo05 {
public static void main(String[] args) throws InterruptedException {
File file = new File("D:/AAA/aaa.txt");
file.delete();//删除空目录
File file1 = new File("D:/AAA/aaa/bbb");
file1.deleteOnExit();//程序退出时删除
Thread.sleep(5000);//程序休眠5s
}
}
修改操作
public class Demo06 {
public static void main(String[] args) throws IOException {
File file = new File("D:/AAA/a.txt");
file.createNewFile();
file.setReadable(false);//设置该文件的权限为不能读
file.setWritable(false);//设置文件不能写
file.setReadOnly();//设置只读权限
file.renameTo(new File("C:/AAA/c.txt"));
}
}
查询操作
public class Demo07 {
public static void main(String[] args) {
File file = new File("D:/AAA/c.txt");
String name = file.getName();//获取文件名称
System.out.println(name);
String parent = file.getParent();//获取父级目录名称
System.out.println(parent);
String path = file.getPath();//获取文件路径的名称
System.out.println(path);
boolean file1 = file.isFile();//判断是否是文件类型
System.out.println(file1);
boolean directory = file.isDirectory();//判断是否是目录类型
System.out.println(directory);
File file2 = new File("D:/AAA");
String[] list = file2.list();
System.out.println(Arrays.toString(list));//列出AAA下所有子目录的名称
File[] files = file2.listFiles();
//System.out.println(Arrays.toString(files));//列出AAA下所有目录文件对象
for (File f:files
) {
System.out.println(f);
}
}
}
经典题目:
获取指定目录下所有文件
递归调用
public class Demo08 {
public static void main(String[] args) {
show("D:/AAA");
}
public static void show(String path){
File file = new File(path);
//判断文件是否存在 目录是否存在
if (!file.exists()||!file.isDirectory()){
return;
}
File[] files = file.listFiles();
for (File f:files
) {
if (f.isDirectory()){
System.out.println(f.getPath()+"dir");
show(f.getPath());
}else {
System.out.println(f.getPath());
}
}
}
}
IO流
1. IO 表示有两个单词的缩写。
I: Input 输入 O: Output 输出
2. IO的作用:就是对文件中的内容进行操作。
输入: 读操作(读取文件的内容) 输出: 写操作(往文件中写内容)
3. IO流的分类:
(1)根据流的方向:
---输入流: 程序可以从中读取数据的流。
---输出流: 程序能向其中写入数据的流。
(2)根据流的单位:
---字节流: 以字节为单位传输数据的流
---字符流: 以字符为单位传输数据的流
(3)根据功能
---节点流: 直接和文件进行交互
---处理流: 不是直接作用在文件上。
四个基本的流: 其他的流都是在这四个流的基础上进行扩展的
字节输入流
字节输出流
字符输入流
字符输出流
Writer字符输出流
它是所有字符输出流的跟类。---FileWriter类
public class TestWriter {
public static void main(String[] args) throws IOException {
//字符输出流 指定对哪个文件(路径)进行操作
//true 表示对内容进行追加到文件
Writer writer = new FileWriter("D:/AAA/b.txt",true);
String str ="BBBB";
writer.write(str);
writer.flush();//刷新
writer.close();//关闭
}
}
Reader字符输入流
它是所有字符输入流的根类 它的实现类有很多,我们使用FileReader实现类
public class TestReader {
public static void main(String[] args) throws IOException {
Reader reader = new FileReader("D:/AAA/b.txt");
//char read = (char) reader.read();//每次读取一个字符 返回读取的内容
//System.out.println(read);
char[] chars = new char[20];
int read = reader.read(chars);
String str = new String(chars);
//System.out.println(read);
//System.out.println(Arrays.toString(chars));
System.out.println(str);
}
}
public class Demo09 {
public static void main(String[] args) throws IOException {
Reader reader = new FileReader("D:/AAA/d.txt");
int count= 0;
char[] chars = new char[10];
//int read = reader.read(chars);//读取字符的个数 将读取到的字符存入数组
while ((count = reader.read(chars))!=-1){
String str = new String(chars,0,count);
System.out.print(str);
}
}
}
完成文件内容的复制
public class Demo02 {
@Test
public void test01() throws IOException {
//创建一个字符输入流
FileReader fr = new FileReader("D:/AAA/a.txt");
//创建一个字符输出流
FileWriter fw = new FileWriter("E:/AAA/f.txt");
int c = 0; //读取字符的个数
char[] chars = new char[10];
while ((c = fr.read(chars))!= -1){
fw.write(chars,0,c);
fw.flush();
}
fr.close();
fw.close();
}
上面只能进行字符的赋值, 咱们能不能复制一个电影 图片 压缩文件等
上面的字符流没法对图片和电影 或压缩文件等进行操作。
它只能对文本进行操作。
因为视频图片 这些都属于二进制。
字节流
字节输出流--OutputStream
它可以对任意文件进行操作,对文件进行输出操作。以字节为单位。 它是所有字节输出流的父类------FileOutputStream
@Test
public void test() throws IOException {
//字节输出流
OutputStream fo = new FileOutputStream("D:/AAA/l.txt");
String str = "abcd";
//通过getBytes()
byte[] bytes = str.getBytes();
fo.write(bytes);
fo.flush();
fo.close();
}
字节输入流---InputStream
public class DemoInputStream {
//字节输入流
@Test
public void test01() throws IOException {
InputStream fs = new FileInputStream("D:/AAA/a.txt");
byte[] bytes = new byte[10];
int a = fs.read(bytes);
System.out.println(bytes+"===>"+a);
a = fs.read(bytes);
System.out.println(bytes+"===>"+a);
a = fs.read(bytes);
System.out.println(bytes+"===>"+a);
//读取没有内容了 返回-1
a = fs.read(bytes);
System.out.println(bytes+"===>"+a);
}
@Test
public void test02() throws IOException {
InputStream fs = new FileInputStream("D:/AAA/a.txt");
int c = 0;
byte[] b = new byte[10];
while ((c = fs.read(b))!=-1){
String str = new String(b,0,c);
System.out.print(str);
}
fs.close();
}
使用字节输入和输出流完成文件的复制功能:
@Test
public void testCopy() throws Exception{
//1.创建字节输入流 视频
InputStream is=new FileInputStream("D:/AAA/1.jpg");
//2.字节输出流
OutputStream fos=new FileOutputStream("E:/AAA/2.jpg");
byte[] bytes=new byte[10];
int c=0;
while( (c=is.read(bytes)) !=-1 ){
fos.write(bytes,0,c);
}
is.close();
fos.close();
}
缓存流
缓存流是在基础流[InputStream OutputStream Reader Writer]之上 添加了一个缓存池功能.
BufferInutStream BufferOutputStream BufferReader BufferWriter 提高IO的效率,降低IO的次数。
public class TestBuffer {
@Test
public void test() throws IOException {
OutputStream out = new FileOutputStream("D:/AAA/j.txt");
//缓冲流作用于在基础流上 默认缓冲8192个字节 可以自定义缓冲池字节
BufferedOutputStream bf = new BufferedOutputStream(out);
String str = "abcdefghijklmn";
byte[] bytes = str.getBytes();
bf.write(bytes);
bf.flush();//刷新缓冲池 将缓冲池内容输入到文件中
bf.close();//关闭 先刷新缓冲池在关闭流资源
}
}
对象流--对java对象进行IO操作
为什么需要对象流
我们现在操作IO流的时候 都是将字符串读写操作 可不可以将java对象在文件中进行读写呢? 可以的 Student st=new Student();对象
将java对象进行读写操作 意义在于持久化信息 例如: 游戏存档。
// 因为运行的时候 所有的数据都是在运行内存中的 持久化 将运行内存的数据 保存到硬盘上 存档(写) 读档(读)
public class TestObject {
@Test
public void test1() throws IOException {
OutputStream out = new FileOutputStream("D:/AAA/2.txt");
ObjectOutputStream os = new ObjectOutputStream(out);
//使用对象输出流调用输出方法 输出的类对象 该类必须实现 Serializable==>序列化接口
Game game = new Game("王者荣耀","小明",12);
os.writeObject(game);
os.close();
}
//测试读档 反序列化
@Test
public void test2() throws IOException, ClassNotFoundException {
InputStream is = new FileInputStream("D:/AAA/2.txt");
ObjectInputStream os = new ObjectInputStream(is);
Object o = os.readObject();
System.out.println(o);
}
1. 序列化: 把内存中的java对象存储到磁盘[网盘]的过程。
---java对象所属的类必须实现序列化接口.implements Serializable
2. 反序列化: 把磁盘中的内容读取到java对象内存中的过程。
总结:
1. 通过字符流完成文件的复制---->它只能复制文本文件
2. 字节流:---字节输入流和字节输出流。
3. 字节流 我们也可以完成文件的复制功能---它可以复制任意类型的文件.
4. 缓存流--->它基本流的基础上 添加了一个缓存池
5. 对象流: ObjectInputStream ObjectOutputStream
序列化:
反序列化: