System类、StringBuilder、File

一、System

获取时间戳

 

二、StringBuilder和StringBuffer

因为String类定义的字符串具有不变性,因此在做字符串值变化的时候非常浪费内存,即耗时又浪费时间。一般不做经常变化字符串的使用,经常需要变化的推荐使用StringBuffer和StringBuilder

 StringBuilder的常用操作:

1. append() 对字符串后面进行追加

public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb1=new StringBuilder("hello");
        sb1.append("world");
        System.out.println(sb1);

}
}

//输出结果是 hello World

 2. insert() 在字符串指定索引位置插入一个新的字符串

public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder sb1=new StringBuilder("hello");
        sb1.insert(2,"x");
        System.out.println(sb1);

}
}
//输出结果是hexllo

3.reverse() 字符串的反转

public class StringBuilderTest {
    public static void main(String[] args) {

        StringBuilder sb1=new StringBuilder("hello");

        StringBuilder sb2=  sb1.reverse();
        System.out.println(sb2);//获取字符串的反转

}

}
//输出结果是olleh

4.delete() 移除字符串中从索引为2(包含2)的位置到索引为4(不包含4)之间的字符

public class StringBuilderTest {
    public static void main(String[] args) {
    StringBuilder sb1=new StringBuilder("hello");
    StringBuilder sb3= sb1.delete(2,4);
        System.out.println(sb3);

}

}
//输出结果是heo

其他操作:

StringBuffer和StringBuilder的区别:

* stringBuffer 线程安全的可变字符序列

* StringBuilder线程不安全的可变字符序列

三、File

3.1构造方法:

第一种

File f1=new File("D:/aa/hello.txt");//f1代表了这个文件

第二种

File f2=new File("d:/aa","hello.txt");//前面路径名,后面文件名

第三种

File file=new File("d:/aa"); //父级file对象和子路径
File f3=new File(file,"hello.txt");

注:

  • 1.一个File对象代表硬盘中实际存在的一个文件或者目录
  • 2.无论该路径下是否存在文件或者目录,都不影响File对象的创建 

3.2功能

1. getAbsolutePath();返回File的绝对路径名字的字符串

public class FileTest2 {
    public static void main(String[] args) {
    File f1=new File("D:/aa/hello.txt");//创建了 文件 映射对象
    String absolutePath1 = f1.getAbsolutePath();
    System.out.println("文件的绝对路径"+absolutePath1);//绝对磁盘路径 


}
}

2.getPath()  将此File转化为路径名字符串

public class FileTest2 {
    public static void main(String[] args) {
    File f1=new File("D:/aa/hello.txt");//创建了 文件 映射对象
    String path1 = f1.getPath();
    System.out.println("文件的路径"+path1);


}
}

3.getName(),返回由此File表示的文件或者目录的名称

public class FileTest2 {
    public static void main(String[] args) {
    File f1=new File("D:/aa/hello.txt");//创建了 文件 映射对象
    String name1 = f1.getName();//获取文件名,即你定位的元素
    System.out.println("文件名称:"+name1);


}
}

4.length()由此File表示的文件的长度

public class FileTest2 {
    public static void main(String[] args) {
    File f1=new File("D:/aa/hello.txt");//创建了 文件 映射对象
    long length = f1.length();
    System.out.println("文件的大小:"+length);


}
}

5. exist 判断该文件 或者 文件夹 是否存在


public class FileTest2 {
    public static void main(String[] args) {
    File f1=new File("D:/aa/hello.txt");//创建了 文件 映射对象
     System.out.println(f1.exists());


}
}

6.  isDirectory()判断是否为文件夹 

public class FileTest2 {
    public static void main(String[] args) {
    File f1=new File("D:/aa/hello.txt");//创建了 文件 映射对象
    System.out.println(f1.isDirectory());


}
}

7.  isFile()判断是否为文件 

public class FileTest2 {
    public static void main(String[] args) {
    File f1=new File("D:/aa/hello.txt");//创建了 文件 映射对象
    System.out.println(f1.isFile());


}
}

8.createNewFile() 当且仅当具有该名称的文件不存在时,创建一个新的空文件。

9.delete() 删除由此File表示的文件文件或目录

10.mkdir():创建由此File表示的目录

11.mkdirs():创建由此File表示的目录,包括任何必需但不存在的父目录。


public class FileTest3 {
        public static void main(String[] args) throws IOException {

        File f1=new File("d:/aa/bb/bb.txt");
        File f2=new File("d:/aa/aa.txt");

        //1.目录结构不存在 程序异常;2.目录结构存在且文件不存在则创建空文件夹;3.目录结构存在且文件在不做任何操作
        f1.createNewFile();//创建文件
        f2.createNewFile();


        File f3=new File("d:/xx");
        File f4=new File("d:/xx/yy");
        File f5=new File("d:/xx/yy/zz");

        //mkdir创建单个文件夹,mkdirs自动补全所有的目录结构,推荐使用mkdirs
        f4.mkdir();
        f5.mkdirs();

        //删除映射的文件或者目录对象,父目录不会删除
        f2.delete();
        f5.delete();
}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值