8-API_文件操作
文件操作
package com.jking.a;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Test {
public static void main(String[] args) {
//a();//日期转换器
//b();//小数转换器
//c();//文件操作
d();//删除创建
//e("C:\\Program Files\\Java\\jdk1.8.0_221");
}
private static void e(String src) {//文件遍历
File f = new File(src);
//循环显示该目录下的所有文件
File[] files = f.listFiles();
for (File file : files) {
if(file.isFile())//是文件
System.out.println(file.getName()+"--"+file.length()/1024+"kb");
else if(file.isDirectory()) {
e(file.getPath());//递归查看所有文件
}
}
}
private static void d() {
File f = new File("D:/r.txt");
//判断文件是否存在
if(f.exists())
System.out.println(f.getName()+"-"+f.length());
if(f.delete())
System.out.println("删除成功");
else
try {
if(f.createNewFile())//在电脑内创建该文件
System.out.println("文件创建成功!");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void a() {
//格式转换器
/*
* yyyy 年
* MM 月
* dd 日
*/
/*
*
* Date d = new Date();
DateFormat df = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒");
String str = df.format(d);
*/
String str = new SimpleDateFormat("yyyy年MM月dd日 hh时mm分ss秒").format(new Date());
System.out.println(str);
}
public static void b() {
double d = 10.2165151;
String str =
new DecimalFormat("#.##")
.format(d);
System.out.println(str);
}
public static void c() {
//创建文件对象
File f = new File("C:\\Users\\差不多先生ZY\\Desktop\\猴王.html");
//File f = new File("C:/Users/差不多先生ZY/Desktop/猴王.html");
//获取文件相关信息
System.out.println(f.getName());//文件名
System.out.println(f.getPath());//文件路径
//System.out.println(f.getName().split("\\.")[1]);
System.out.println(f.getName().substring(f.getName().lastIndexOf(".")+1));//文件拓展名
System.out.println(f.length());
//System.out.println(f.length()/1024+"kb");
}
}
爱你爱你爱你爱你爱你昕宝贝

浙公网安备 33010602011771号