2019.07.18(day15)
●数学相关的类:
-Math类:
1.Math类是final类
2.构造方法私有,不可以创建对象
3.主要的用法是提供大量的静态方法
4.在计算小数的时候不够准确
-BigDecimal类:
1.用于精确计算的类
2.在精确计算的时候要求参数以字符串的形式传入此类的对象
-BigInteger类:
1.用于存储任意大小的整型数据的类
2.在存储数据的时候,最好用字符串的形式传入对象
●日期相关的类:
-Date类:
1.表示日期的类
2.提供很多操作日期的方法,但很多的方法已经过时了(Deprecated)
-SimpleDateFormat类:
1.parse方法
将日期的字符串转换为日期
public void testMethod() throws Exception{
//设置默认的日期格式
SimpleDateFormat simpleDateFormat=new SimpleDateFormat();
//设置指定的日期格式
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
//把当前的日期字符串转换成日期
Date date=simpleDateFormat.parse("19-7-18 下午19:30");
Date date1=simpleDateFormat1.parse("2019-07-18 10:37:35:801");
System.out.println(date);//Fri Jul 19 07:30:00 CST 2019
System.out.println(date1);//Thu Jul 18 10:37:35 CST 2019
}
2.format方法
将日期的对象转换为字符串
public void testMethod() throws Exception{
//设置指定的日期格式
SimpleDateFormat simpleDateFormat1=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
SimpleDateFormat simpleDateFormat2=new SimpleDateFormat("yyyy年MM月dd日 HH小时mm分钟ss秒SSS毫秒");
SimpleDateFormat simpleDateFormat3=new SimpleDateFormat("yyyy/MM/dd HH/mm/ss#SSS");
//把日期对象转换成日期的字符串
String str1=simpleDateFormat1.format(new Date());
String str2=simpleDateFormat2.format(new Date());
String str3=simpleDateFormat3.format(new Date());
System.out.println(str1);//2019-07-18 19:32:05:883
System.out.println(str2);//2019年07月18日 19小时32分钟05秒884毫秒
System.out.println(str3);//2019/07/18 19/32/05#884
}
3.在转换的时候可以提供转换的格式
-Calendar类:
1.与日历相关的类,控制时区
2.提供大量的方法来操作时间
3.Calendar是一个抽象类,不能直接new实例化对象
Calendar cal=Calendar.getInstance();
public void testMethod(){
//当前日期
Date date1=new Date();
Calendar cal1=Calendar.getInstance();
System.out.println(cal1.getTime());//Fri Jul 19 08:31:05 CST 2019
System.out.println(cal1.getTimeInMillis());//1563450087546
System.out.println(cal1.get(Calendar.YEAR));//2019
System.out.println(cal1.get(Calendar.MONTH)+1);//7
System.out.println(cal1.get(Calendar.DATE));//18
System.out.println(cal1.get(Calendar.HOUR));//7
System.out.println(cal1.get(Calendar.MINUTE));//41
System.out.println(cal1.get(Calendar.SECOND));/27
}
总结:
用date对象存储日期
用Calendar类的对象,来操作Date类中的日期数据
用SimpleDateFormat,来对Date对象和日期字符串之间的转换
●IO api:
input输入:往内存中传送数据
ouput输出:从内存中取数据
对文件内容操作分为两种方式:
字节流:对文件的内容用字节的方式操作
字符流:对文件内容读写用字符(ASCII)的方式操作
(本质还是用字节流操作)
File类:
-对文件和目录进行操作,与文件的内容无关
-不同操作系统上对目录间隔符的区分
Windows:
C:\aa\bb\cc.txt
Linux:
/home/aa/bb/cc.txt
在java中对路径的分隔符的表示
Windows:
c:\\aa\\b\cc.txt
c:/aa/bb/cc.txt
Linux:
c:/aa/bb/cc.txt
如果想兼容Windons和Linux
"aa"+File.separator+"bb"+File.separator+"cc.txt"
File的api:
-构建File类对象
File(String filepath);
File(File parent,String child);
File(String parentName,String child);
-isFile()
判断是否为文件
-isDirectory()
判断是否为目录
-length()
获取文件的长度
-exists()
判断文件或目录是否存在
-createNewFile()
创建一个空文件返回值是boolean
如果文件不存在,就创建文件夹并返回true
如果指定的文件存在,返回false;
public void testMethod() throws IOException{
String path="d:\\aa\\aa1.txt";
File file=new File(path);
if(!file.exists()){
boolean flag=file.createNewFile();
if(flag){
System.out.println("文件创建成功");
}else{
System.out.println("文件创建失败");
}
}else{
System.out.println("文件已存在");
}
}
--delete()
删除文件和文件夹
注意:如果File表示一个目录删除的时候,要保证目录必须是空的
删除文件:
public void testMethod() throws IOException{
String path="d:\\aa\\aa1.txt";
File file=new File(path);
if(file.exists()){
boolean flag=file.delete();
if(flag){
System.out.println("删除文件成功");
}else{
System.out.println("删除失败");
}
}else{
System.out.println("文件不存在");
}
}
删除目录:
public void testMethod() {
File file=new File("d:/aa");
try {
deleteFile(file);
} catch (Exception e) {
e.printStackTrace();
}
}
private void deleteFile(File file) throws Exception{
if(file==null){
throw new Exception("file不能为null,请指定具体的目录或文件");
}
if(file.isDirectory()){
//指定的file为目录
//返回指定目录中的所有的文件和目录
File[] files=file.listFiles();
//遍历files,可能是目录,也可能是文件
for(File f:files){
//递归调用自己
deleteFile(f);
}
}
//说明不是目录是文件,直接删除文件即可
file.delete();
}
--mkdir()
创建目录
-mkdirs()
创建多个文件夹
public void testMethod() {
File file1=new File("d:/aa/bb/cc/dd/ee");
if(!file1.exists()){
boolean flag=file1.mkdirs();
if(flag){
System.out.println("目录创建成功");
}else{
System.out.println("目录创建失败");
}
}else{
System.out.println("目录已经存在");
}
}
-listFiles()
返回指定目录中的所有文件和目录
public void testMethod12() {
File file=new File("d:/aa");
File[] fs=file.listFiles();
for(File f:fs){
System.out.println(f);
}
}
-listFiles(FileFilter)
返回指定目录中的部分文件和目录,用FileFilet设定筛选条件
public void testMethod13() {
File file=new File("d:/aa");
if(file.exists()){
File[] fs=file.listFiles(new FileFilter(){
//回调函数
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".txt");
}
});
for(File f:fs){
System.out.println(f);
}
}else{
System.out.println("目录不存在");
}
}
-listFiles(FileNameFileter)
返回指定目录中的部分文件和目录,用FileNameFileter设定筛选条件
public void testMethod14() {
File file=new File("d:/aa");
if(file.exists()){
File[] fs=file.listFiles(new FilenameFilter(){
//回调函数
@Override
public boolean accept(File dir, String name) {
return name.endsWith(".txt");
}
});
for(File f:fs){
System.out.println(f);
}
}else{
System.out.println("目录不存在");
}
}
总结:
1.只能操作文件或目录的信息
2.就是不能操作文件的内容
RandomAccessFile类
-可以操作文件的内容
-按照字节操作,字节流
-此类特殊,read读和write写都是此类中的api方法
-能够通过seek方法,随意改动/移动文件的指针
RandomAccessFile类
对文件的随机访问有两种模式:
1.只读模式
2.读写模式
-创建对象
RandomAccessFile(File file,String mode);
创建从中读取和向其中写入的随机访问流
文件通过File指定
模式通过String指定
RandomAccessFile(String file,String mode);
创建从中读取和向其中写入的随机访问流
文件通过String指定
模式通过String指定
RandomAccessFile raf1=new RandomAccessFile(new File("d:/aa/aa.txt"), "r");
String path="d:/aa/aa.txt";
RandomAccessFile raf2=new RandomAccessFile(path, "rw");
-写入操作
void write(int d);
此方法会根据当前指针所在的位置写入一个字节
只能使用整型的低8位
void write(byte[] d)
此方法会根据当前指针所在的位置写入一组字节
RandomAccessFile raf1=new RandomAccessFile(new File("d:/aa/aa.txt"), "rw");
raf1.write("hello world".getBytes());
void write(byte[] d,int offset,int len)
将len个字节从指定的byte数组写入文件,并从偏移量offset处开始写入
RandomAccessFile raf1=new RandomAccessFile(new File("d:/aa/aa.txt"), "rw");
raf1.write("hello world".getBytes(),0,7);
-读取操作
int read()
从文件中读取出一个byte字节,填充到整型的低8位
如果返回-1,表示读取到文件的末尾 EOF end of file
intread(byte[] b)
从指针指向的位置开始读取若干字节,存储到字节数组中
将读取到的字节按照顺序放在字节数组相对应的位置
返回值为读取到的字节数,也可以说成读取到的是长度
返回值为-1,则读取到文件的末尾
int read(byte[] d,int offset,int len)
将最多len的数据字节从文件中读入byte数组中,
并从偏移量offset开始存储
-void getFilePointer()
返回此文件的当前的偏移量
-void seek(long position)
设置到系文件开头0到文件指针的偏移量,在该位置发生下一个读取或写入操作
-int skipBytes(int n)
此方法可以调过一些少量的字节,只能跳正数