加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

File类

发布时间:2021-05-20 09:44:56 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:import java.io.File; import java.io.IOException; import text6.Staticvariaty; //File类的构造方法??? 三种重载形式 public class FileDemo01 { ?? ?public static void main(String[] args) throws IOException { ?? ??? ?function_13(); ?? ?} ?? ? ?? ?
import java.io.File;
import java.io.IOException;
import text6.Staticvariaty;

//File类的构造方法??? 三种重载形式
public class FileDemo01 {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?function_13();
?? ?}
?? ?
?? ?//传递路径名:可以写到文件夹,可以写到一个文件
?? ?//将路径封装为File类型对象
?? ?public static void function() throws IOException{
?? ??? ?File file=new File("d:eclipse");
?? ??? ?//File创建文件的功能? boolean createNewFile()? (文件已经存在了,不再创建)
?? ??? ?boolean b=file.createNewFile();
?? ??? ?System.out.println(b);
?? ?}
?? ?
?? ?public static void function_1(){
?? ??? ?File file=new File("c:","windows");
?? ??? ?System.out.println(file);
?? ?}
?? ?
?? ?public static void function_2(){
?? ??? ?File parent=new File("d:");
?? ??? ?File file=new File(parent,"eclipse");
?? ??? ?System.out.println(file);
?? ?}
?? ?
?? ?//File创建文件夹功能 boolean mkdir()? (文件夹已经存在了,不再创建)
?? ?public static void function_3(){
?? ??? ?File file=new File("d:abc");
?? ??? ?boolean b=file.mkdir();
?? ??? ?System.out.println(b);
?? ?}
?? ?//File创建多级目录的文件夹
?? ?public static void function_4(){
?? ??? ?File file=new File("d:abcc");
?? ??? ?boolean b=file.mkdirs();
?? ??? ?System.out.println(b);
?? ?}
?? ?
?? ?//File类的删除功能 boolean delete()? (删除的文件或文件夹,在File构造方法中给出;删除成功返回true,失败返回false)
?? ?//删除方法,不走回收站,直接从硬盘中删除
?? ?public static void function_5(){
?? ??? ?File file=new File("d:abc");
?? ??? ?boolean b=file.delete();
?? ??? ?System.out.println(b);
?? ?}
?? ?
?? ?//File类的获取功能---String getName():返回由此抽象路径名表示的文件或目录的名称
?? ?//获取路径中的最后部分的名字
?? ?public static void function_6(){
?? ??? ?File file=new File("e:WechatWeChat.exe");
?? ??? ?String nameString=file.getName();
?? ??? ?System.out.println(nameString);
?? ?}
?? ?
?? ?//File类的获取功能---long length():返回路径中表示的文件大小的字节数
?? ?public static void function_7(){
?? ??? ?File file=new File("e:WechatWeChat.exe");
?? ??? ?long length=file.length();
?? ??? ?System.out.println(length);
?? ?}
?? ?
?? ?//File类的获取功能---String getAbsolutePath():获取绝对路径,返回String对象
?? ?//??????????? ---String getAbsoluteFile():获取绝对路径,返回File对象? (常用此)
?? ?public static void function_8(){
?? ??? ?File file=new File("src");
?? ??? ?File absolute=file.getAbsoluteFile();
?? ??? ?System.out.println(absolute);
?? ?}
?? ?
?? ?//File类的获取功能---String getParent():获取父路径,返回String对象
?? ?//?? ??? ??? ??? ? String getParentFile():获取父路径,返回File对象
?? ?public static void function_9(){
?? ??? ?File file=new File("e:WechatWechat.exe");
?? ??? ?File parent=file.getParentFile().getParentFile();
?? ??? ?System.out.println(parent);
?? ?}
?? ?
?? ?//File判断功能---boolean exists():判断File构造方法中封装路径是否存在
?? ?public static void function_10(){
?? ??? ?File file=new File("src");
?? ??? ?boolean b=file.exists();
?? ??? ?System.out.println(b);
?? ?}
?? ?
?? ?//File判断功能---boolean isDirectory():判断是不是文件夹
?? ?public static void function_11(){
?? ??? ?File file=new File("src");
?? ??? ?boolean b=file.isDirectory();
?? ??? ?System.out.println(b);
?? ?}
?? ?
?? ?//File类的获取功能---String[] list():获取到File构造方法中封装的路径中的文件和文件夹名(遍历一个目录)
?? ?public static void function_12(){
?? ??? ?File file=new File("e:Wechat");
?? ??? ?String[] strArr=file.list();
?? ??? ?for(String s:strArr){
?? ??? ??? ?System.out.println(s);
?? ??? ?}
?? ?}
?? ?
?? ?//File类的获取功能---String[] listFiles():获取到File构造方法中封装的路径中的文件和文件夹名(遍历一个目录)
?? ?//返回的是文件或目录的全路径
?? ?public static void function_13(){
?? ??? ?File file=new File("e:Wechat");
?? ??? ?File[] fileArr=file.listFiles();
?? ??? ?for(File f:fileArr){
?? ??? ??? ?System.out.println(f);
?? ??? ?}
?? ?}
}

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读