Chenyuyan123

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

文件的创建与查询

1、什么是文件?

文件是我们保存数据的地方

2、文件流文件在程序中是以流的形式来操作的。

流:数据在数据源(文件)和程序(内存)之间经历的路径

输入流:数据从数据源(文件)到程序(内存)的路径

输出流:数据从程序(内存)到数据源(文件)的路径

3、常用的文件操作

1、创建文件对象相关构造器和方法

New File(String pathname)//根据路径构建一个File对象

 

1.1  第一种构建文件的方法

地址与文件名,统一性规定好,具体代码如下:

import org.testng.annotations.Test;

import java.io.File;

import java.io.IOException;

public class testC {

    public static void main(String[] args){

    }

    @Test

    public void createFile(){

        String filePath ="d:/file1.txt";

        File file = new File(filePath);

        try{

            file.createNewFile();

            System.out.println("创建文件1成功");

        } catch(IOException e){

            e.printStackTrace();

        }

    }

}

利用IDEA运行代码得到结果截图如下:

存创建成功,储在D盘中并查看文件file1.txt存在,因为没有任何数据,为一个空文件,那可以查看它的属性,结果如下:

 

 

1.2  第二种构建方法

地址与文件名分开两部进行书写,具体代码如下:

import org.testng.annotations.Test;

import java.io.File;

import java.io.IOException;

public class testC2 {

    public static void main(String[] args){

    }

    @Test

    public void create20(){

        File parentFile = new File("D:\\");

        String fileNane = "file2.txt";

        File file = new File(parentFile,fileNane);

        try{

            file.createNewFile();

            System.out.println("创建文件2成功");

        } catch(IOException e){

           throw new RuntimeException(e);

        }

    }

}

利用IDEA运行代码得到结果截图如下:

存创建成功,储在D盘中并查看文件file2.txt存在,因为没有任何数据,为一个空文件,那可以查看它的属性,结果如下:

 

1.3  第三种构建方法

地址与文件名分开两部进行书写,同时地址无论大小写并不会对于储存的位置影响,具体代码如下:

import org.testng.annotations.Test;

import java.io.File;

import java.io.IOException;

public class testC3 {

    public static void main(String[] args){

}

    @Test

    public void create30(){

        String parentPath = "d:\\";

        String filePath = "file3.txt";

        File file = new File(parentPath,filePath);

        try{

            file.createNewFile();

            System.out.println("创建文件3成功");

        } catch(IOException e){

            throw new RuntimeException(e);

        }

    }

}

利用IDEA运行代码得到结果截图如下:

存创建成功,储在D盘中并查看文件file1.txt,file2.txt,file3.txt都存在,说明改变大小写并不会影响储存的位置,结果如下:

 

 

 

 

2、获取文件的相关信息(查询信息) 

常见的File相关方法

getName()/getAbsolutePath/getParent/length/exists/isFile/isDirectory

获取文件名、获取绝对路径,在UTF-8中一个英文一个字节,一个汉字三个字节,具体实现代码如下:

import org.testng.annotations.Test;

import java.io.File;

public class FileInformation {

    public static void main(String[] args){

    }

    @Test

    public void Info(){

        //先创建文件对象

        File file = new File("D:\\file1.txt");

        //调用相应的方法,得到相应的信息

        System.out.println("文件名称:"+file.getName());

        System.out.println("文件绝对路径:"+file.getAbsolutePath());

        System.out.println("文件父目录:"+file.getParent());

        System.out.println("文件大小:"+file.length());

        System.out.println("是否是文件:"+file.isFile());

        System.out.println("是否是目录:"+file.isDirectory());

    }

}

利用IDEA运行代码得到结果截图如下:

  

3、Scanner与Println的练习

3.1  基本键盘输入,代码如下:

import org.testng.annotations.Test;

import java.util.Scanner;

public class scanPrintTest {

    public static void main(String[] args){

        //创建Scanner对象,接受从控制台输入

        Scanner input = new Scanner(System.in);

        //接受Sting类型

        String str = input.next();

        //输出结果

        System.out.println(str);

        System.out.println("hello world");

    }

}

输入相应的string类型的数得到结果截图如下:

   

3.2  常见键盘输入类型,代码如下:

import java.util.Scanner;

public class scanTest {

    public static void main(String[] args){

        //创建Scanner对象,接受从控制台输入

        Scanner input = new Scanner(System.in);

        //double类型的数据

        System.out.print("请输入一个double类型的数");

        double d = input.nextDouble();

        System.out.println(d);

        //int类型的数据

        System.out.println("请输入一个int类型的数");

        int i = input.nextInt();

        System.out.println(i);

        //字符串类型的数据

        System.out.println("请输入一个string类型的数");

        String s=input.next();

        System.out.println(s);

    }

}

利用IDEA运行代码,输入相应的double类型的数,int类型的数,string类型的数得到结果截图如下:

 

 

 

posted on   陈于晏  阅读(48)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
 
点击右上角即可分享
微信分享提示