7月16枚举类和IO流
1.enum类
1.String
2.基本数据类型包装类
3.Math
4.Date
5.SimpleDateFormat
枚举类型 : 描述一种事物的所有情况|所有可能|所有实例
1.通过enum关键字定义枚举类型
2.枚举的成员,字段都作为当前枚举类型的实例存在,默认被public static final修饰
3.定义的枚举类型都会隐式的继承自java.lang.Enum 这是所有Java语言枚举类型的公共基类。
4.在枚举类型中构造器默认私有
public class Class001_Enum {
public static void main(String[] args) {
System.out.println(WeekDays.SUN);
//使用枚举类型的实例
WeekDays sun = WeekDays.SUN;
System.out.println(sun.getName());
//name() 获取枚举类型实例的字段名
System.out.println(sun.name());
//获取枚举对象在枚举类的字段列表中的索引值
System.out.println(sun.ordinal());
//values() 获取当前枚举类型的所有实例
WeekDays[] arr = sun.values();
System.out.println(Arrays.toString(arr));
sun = WeekDays.MON;
//switch()中jdk5新增对枚举的支持
switch (sun){
case MON:
System.out.println("星期一");
break;
case TUES:
System.out.println("星期二");
break;
case SUN:
System.out.println("星期天");
break;
}
}
}
//一周的中的天数 周1~周7
enum WeekDays{
//枚举字段|当前枚举类型的实例
MON,TUES,SUN("星期天");
// public static final WeekDay MON = new WeekDay();
//成员变量
private String name;
//构造器
private WeekDays(){}
private WeekDays(String name){
this.name = name;
}
//成员方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
class WeekDay{
public static final WeekDay day1 = new WeekDay();
public static final WeekDay day2 = new WeekDay();
public static final WeekDay day3 = new WeekDay();
public static final WeekDay day4 = new WeekDay();
public static final WeekDay day5 = new WeekDay();
public static final WeekDay day6 = new WeekDay();
public static final WeekDay day7 = new WeekDay();
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
//构造器私有化
private WeekDay(){}
}
SUN
星期天
SUN
2
[MON, TUES, SUN]
星期一
/*
存储bd的员工
1.遍历输出所有的员工信息
2.找到员工工作状态(空闲,正常,忙碌)为忙碌的员工涨薪10%
3.计算所有员工工资总和
分析:
员工类
属性
功能
测试类
**java是值传递
基本数据类型的数据值
引用数据类型的地址值
*/
public class Class002_EmpTest {
public static void main(String[] args) {
//存储员工的数组
/* int [] arr1=new int[]{1,2};
arr1[0]=arr1[0]+1;
System.out.println(Arrays.toString(arr1));*/
Employee[] arr = {
new Employee("zhangsan",15000, State.BUSY),
new Employee("lisi",11000, State.FREE),
new Employee("wangwu",13000, State.NORMAL),
new Employee("zhaoliu",10000, State.BUSY),
};
// 1.遍历输出所有的员工信息
print(arr);
// 2.找到员工工作状态(空闲,正常,忙碌)为忙碌的员工涨薪10%
System.out.println(Arrays.toString(addSalary(arr)));
System.out.println(Arrays.toString(arr));
// 3.计算所有员工工资总和
System.out.println(getSum(arr));;
}
// 3.计算所有员工工资总和
public static double getSum(Employee[] arr){
int sum = 0; //工资总和
for(int i=0;i<=arr.length-1;i++){
sum+=arr[i].getSalary();
}
return sum;
}
// 1.遍历输出所有的员工信息
public static void print(Employee[] arr){
for(Employee e:arr){
System.out.println(e);
}
}
//2.找到员工工作状态(空闲,正常,忙碌)为忙碌的员工涨薪10%
public static Employee[] addSalary(Employee[] arr){
for(Employee e:arr){
//找到员工工作状态
if(State.BUSY.equals(e.getState())){
e.setSalary(e.getSalary()*1.1);
}
}
return arr;
}
}
2.File类
/*
File 路径或者文件的抽象的表现形式
注意:
java中路径的分隔符可以使用: \\ / //
*/
public class Class001_File {
public static void main(String[] args) throws IOException {
//File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例。
File file1 = new File("D:\\test.txt");
File file2 = new File("D:\\AAA");
File file3 = new File("D:/");
//File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例。
File file4 = new File(file3,"test.txt");
File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例。
File file5 = new File("D://AAA","BBB");
File file6 = new File("D://haha.txt");
System.out.println(file1);
System.out.println(file2);
System.out.println(file4);
System.out.println(file1.equals(file4));
System.out.println(file2.equals(file5));
System.out.println(file5);
//常用方法
//boolean canWrite() 测试应用程序是否可以修改此抽象路径名表示的文件。
//boolean exists() 测试此抽象路径名表示的文件或目录是否存在。
//boolean setReadOnly() 标记此抽象路径名指定的文件或目录,以便仅允许读取操作。
if(file1.exists()){
System.out.println("只读状态设置: "+file1.setReadOnly());
System.out.println("判断文件file1是否可以编写 "+file1.canWrite());
}
//boolean createNewFile() 当且仅当具有此名称的文件尚不存在时,以原子方式创建由此抽象路径名命名的新空文件。
if(!file6.exists()){
System.out.println(file6.createNewFile());
}
//boolean delete() 删除此抽象路径名表示的文件或目录。 空文件夹才可以删除
System.out.println(file6.delete());
System.out.println(file2.delete());
//File getAbsoluteFile() 返回此抽象路径名的绝对形式。
//String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串。
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getAbsoluteFile());
//默认相对于当前项目下
File file = new File("test.txt");
System.out.println(file);
System.out.println(file.getAbsolutePath()); //D:\source_s\lesson\java49\0206_常用类\002_code\project_class\test.txt
System.out.println(file.getAbsoluteFile()); //D:\source_s\lesson\java49\0206_常用类\002_code\project_class\test.txt
//long getFreeSpace() 通过此抽象路径名返回分区 named中未分配的字节数。
System.out.println(file3.getFreeSpace());
//long getTotalSpace() 通过此抽象路径名返回分区 named的大小。
//String getName() 返回此抽象路径名表示的文件或目录的名称。
System.out.println(file1.getName());
System.out.println(file5.getName());
//String getParent() 返回此抽象路径名父项的路径名字符串,如果此路径名未指定父目录,则返回 null 。
//File getParentFile() 返回此抽象路径名父项的抽象路径名,如果此路径名未指定父目录,则返回 null 。
System.out.println(file1.getParent());
System.out.println(file1.getParentFile());
System.out.println(file5.getParent());
System.out.println(file5.getParentFile());
/*
boolean isAbsolute() 测试此抽象路径名是否为绝对路径。
boolean isDirectory() 测试此抽象路径名表示的文件是否为目录。
boolean isFile() 测试此抽象路径名表示的文件是否为普通文件。
*/
System.out.println(file1.isFile());
System.out.println(file1.isDirectory());
System.out.println(file1.isAbsolute());
//long lastModified() 返回上次修改此抽象路径名表示的文件的时间。
System.out.println(file1.lastModified());
System.out.println(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(new Date(file1.lastModified())));
//long length() 返回此抽象路径名表示的文件的长度。
System.out.println(file1.length());
//String[] list() 返回一个字符串数组,用于命名此抽象路径名表示的目录中的文件和目录。
//File[] listFiles() 返回一个抽象路径名数组,表示此抽象路径名表示的目录中的文件。
File src = new File("D://DDD");
System.out.println(Arrays.toString(src.list()));
File[] files = src.listFiles();
for(File f:files){
System.out.println(f);
}
File file8 = new File("D://haha");
File file9 = new File("D://hehe/heihei/hengheng.txt");
//boole9n mkdir() 创建此抽象路径名指定的目录。 一层
System.out.println(file8.mkdir());
//boolean mkdirs() 创建此抽象路径名指定的目录,包括任何必需但不存在的父目录。 多层
System.out.println(file9.getParentFile().mkdirs());
//boolean renameTo(File dest) 重命名此抽象路径名表示的文件。
//测试 : 跨盘符是否可以实现重命名???
File file10 = new File("D://test2.txt");
System.out.println(file1.renameTo(file10));
}
}
3.IO字节流
前提需求: 读写文件内部的内容,上传,下载
IO流
流: 管道 数据以先入先出的方式进行流动
数据源--数据-->目的地
io包: 一系列io相关类 File...
流的分类:
按照流向分:
输入流
输出流
(以大脑为中心,以程序为中心,明确数据源和目的地,能够确定输入还是输出)
按照操作单元分;
字节流 : 万能流,任意内容都能转为字节
字符流 : 只能传输纯文本的内容
按照功能分:
节点流 : 真实做读入写出的流
功能流 : 增强节点流的功能,加强性能
分类之间是相辅相成的
字节流:
字节输入流 InputStream
文件字节输入流 FileInputStream
功能: 节点流 流向分:输入流 操作单元:字节流
功能: 读入read() 关闭close
字节输出流 OutputStream
public class Class001_IO {
public static void main(String[] args) throws IOException {
//FileInputStream(File file) 通过打开与实际文件的连接来创建 FileInputStream ,该文件由文件系统中的 File对象 file命名。
File src = new File("D://test2.txt"); //数据源
//创建流
InputStream is = new FileInputStream(src);
//读入数据 int read() 从此输入流中读取一个字节的数据。 读到文件末尾返回-1
int num = is.read();
//处理数据
System.out.println((char)num);
System.out.println((char)(is.read()));
System.out.println((char)(is.read()));
System.out.println(is.read());
//关闭流
is.close();
}
}
1
2
3
51
3.1连续单个读取
/*
read() 从此输入流中读取一个字节的数据。 读到文件末尾返回-1
每次读取一个字节,重复通过循环读入,可以简化代码结构
*/
public class Class002_IO {
public static void main(String[] args) throws IOException {
//创建流
//FileInputStream (String name) 通过打开与实际文件的连接来创建 FileInputStream ,该文件由文件系统中的路径名 name命名。
InputStream is = new FileInputStream("D://test2.txt");
//读入数据
int num = -1;
while((num=is.read())!=-1){
//处理数据
System.out.println((char)num);
}
//关闭流
is.close();
}
}
3.2数组读取
/*
read() 每次读取一个字节,重复通过循环读入,效率较低
int read(byte[] b) 每次读入一个字节数组的数据,重复循环读入
返回值: 返回读入到字节数组中数据的个数 ,没有读到返回-1
*/
public class Class003_IO {
public static void main(String[] args) throws IOException {
//1.构建流
InputStream is = new FileInputStream("D://test2.txt");
//2.准备卡车-->字节数组
byte[] car = new byte[1024];
//3.读入
int len = -1;
while((len=is.read(car))!=-1){
//4.处理数据
System.out.println(new String(car,0,len));;
}
//5.关闭
is.close();
}
}
12334
3.3读取剩余字节
public byte[] readAllBytes() throws IOException
从输入流中读取所有剩余字节。 此方法将阻塞,直到读取了所有剩余字节并检测到流结束,或者抛出异常。 此方法不会关闭输入流。
当此流到达流的末尾时,此方法的进一步调用将返回空字节数组。请注意,此方法适用于方便将所有字节读入字节数组的简单情况。 它不用于读取包含大量数据的输入流。
起始版本: java9
public class Class004_IO {
public static void main(String[] args) throws IOException {
//1.构建流
InputStream is = new FileInputStream("D://test2.txt");
//2.读入所有数据
byte[] arr = is.readAllBytes();
//3.处理数据
System.out.println(new String(arr));
//4.关闭
is.close();
}
}
3.4字节输出流
字节输出流
OutputStream 此抽象类是表示输出字节流的所有类的超类。
FileOutputStream : 文件输出流,将数据写出到指定文件中
注意:
如果目的地文件不存在,系统会自动创建
输出流如果目的地文件存在,内容默认覆盖,设置追加
public class Class005_IO {
public static void main(String[] args) throws IOException {
//1.定义输出流
//FileOutputStream(String name)
//FileOutputStream(File file)
//FileOutputStream(File file, boolean append) 创建文件输出流以写入由指定的 File对象表示的文件。
//FileOutputStream(String name, boolean append) 创建文件输出流以写入具有指定名称的文件。
OutputStream os = new FileOutputStream("D://test.txt",true);
//2.准备数据
byte[] car = "你好就好,要过的没我好!!!!".getBytes();
//3.写出
//write(int b) 将指定的字节写入此文件输出流。
//void write(byte[] b) 将指定字节数组中的 b.length字节写入此文件输出流。
//void write(byte[] b, int off, int len) 将从偏移量 off开始的指定字节数组中的 len字节写入此文件输出流。
os.write(97);
os.write(car);
//4.刷出
os.flush();
//5.关闭
os.close();
}
}
3.5字节流拷贝
/*
文件拷贝
数据源--> 读入---> 程序 --> 写出 --> 目的地
步骤:
1.创建流(输入 输出)
2.准备小汽车 字节数组
3.读入-->写出
4.刷出
5.关闭(后打开的先关闭)
练习: 通过字节流实现图片的拷贝
文件拷贝封装成工具类
*/
public class Class006_CopyFile {
public static void main(String[] args){
//1.创建流(输入 输出)
//作用域提升,为了能够在finally中使用
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream("D://test2.txt");
os = new FileOutputStream("D://dest.txt");
//2.准备小汽车 字节数组
byte[] car = new byte[1024];
//3.读入-->写出
int len = -1; //记录每次读入到字节数组中数据的个数
while((len=is.read(car))!=-1){
System.out.println(Arrays.toString(car));
//读入多少字节数据写出多少字节数据
os.write(car,0,len);
}
//4.刷出
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.关闭(后打开的先关闭)
//预防空指针异常出现
if(os!=null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
4.IO字符流
4.1字符读取流
字符输入流 : 只能读写纯文本数据
输入流 : Reader 字符输入流的父类
FileReader 文件字符输入流
read()
read(char[])
close()
输出流 : Writer
public class Class001_IO {
public static void main(String[] args) throws IOException {
//1.创建流
//FileReader(String fileName)
//FileReader(File file)
Reader rd = new FileReader("D://test.txt");
//2.读入
//char ch = (char)(rd.read());
//小汽车
char[] car = new char[1024];
//循环读入
int len = -1;
while((len = rd.read(car))!=-1){
//3.处理数据
System.out.println(new String(car,0,len));
}
//4.关闭
rd.close();
}
}
a你好就好,要过的没我好!!!b你好就好,要过的没我好!!!
4.2字符输出流
/*
输出流 : Writer 字符输出流抽象父类
FileWriter 文件字符输出流
write()
flush()
close()
*/
public class Class002_IO {
public static void main(String[] args) throws IOException {
//1.创建流
//FileWriter(File file)
//FileWriter(String fileName)
//FileWriter(File file, boolean append)
//FileWriter(String fileName, boolean append)
Writer rt = new FileWriter("D://test.txt",true);
//2.准备数据
String msg = "今天也要加油鸭!!!";
//3.写出
rt.write(msg);
//4.刷出
rt.flush();
//5.关闭
rt.close();
}
}
4.3字符流文件拷贝
/*
字符流实现文件拷贝
注意: 只能为纯文本文件
*/
public class Class003_IO {
public static void main(String[] args){
//1.创建流
Reader rd = null;
Writer rt = null;
try {
rd= new FileReader(“D://test.txt”);
rt = new FileWriter(“D://hehe.txt”);
//2.读入,写出
char[] car = new char[1024];
int len = -1;
while((len = rd.read(car))!=-1){
rt.write(car,0,len);
}
//3.刷出
rt.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭
if(rt!=null){
try {
rt.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(rd!=null){
try {
rd.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}