Java文件处理

 

            所谓文件,是指记录在外部介质上的数据集合。存储在磁盘中的文件叫磁盘文件,简称文件。每个文件具有唯一的文件标识,即文件全称。

           Windows环境下的文件格式为: 文件路径\文件名

          (1)文件路径:  由磁盘盘符和文件夹名构成。

       2)文件名:      由主文件名和扩展名构成。           

        例如:d:\vb\cj.dat

                    表示文件cj.dat存放在D盘的\vb文件夹中,

                    文件名为:cj;文件的扩展名为.dat

      JAVA语言中,文件格式为:文件路径\\文件名

           例如:   d:\\vb\\cj.da

n File

       文件类是一个类,包含:属性、构造方法,一般方法。

 

File——构造方法(3种)

File( String pathname) 
File f=new File(“c:\data\temp.dat”);
File f=new File(“data\ temp.dat”);
File f=new File(“temp.dat”);

File( String parent, String child) 
File f=new File(“c:\data” ,“temp.dat”);
File f=new File(“data ” ,“ temp.dat”);

File( File parent, String child) 
File f=new File( new File(“c:\data”) ,“temp.dat”);
File f=new File(new File(“data ”) ,“ temp.dat”);

    File——文件的属性

n 文件名;
n 路径;
n 文件字节数(文件长度);
n 文件最后修改时间;
n 文件只读性;
n 文件隐藏性;
n 文件只写性;
 

创建方法

1.boolean createNewFile() 不存在返回true 存在返回false
2.boolean mkdir()
创建目录
3.boolean mkdirs() 创建多级目录

 

删除方法

1.boolean delete()

2.boolean deleteOnExit() 文件使用完成后删除

判断方法

1.boolean canExecute()判断文件是否可执行

2.boolean canRead()判断文件是否可读

3.boolean canWrite() 判断文件是否可写

4.boolean exists() 判断文件是否存在

5.boolean isDirectory()

6.boolean isFile()

7.boolean isHidden()

8.boolean isAbsolute()判断是否是绝对路径

 

创建一个新文件

public class Test1 {
    public static void main(String[] args) {
        File f=new File("d:"+File.separator+"test.txt");//为增加可移植性,建议使用File.separator
        try {
            f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class Test2 {
    public static void main(String[] args) {
        File f=new File("d:"+File.separator+"test.txt");
        if(f.exists()){//判断文件存不存在,如不存在就不用删除了
            f.delete();
        }
    }
}

综合创建,删除文件的操作

public static void main(String[] args) {
        File f=new File("d:"+File.separator+"test.txt");
        if(f.exists()){
            f.delete();
        }else{
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
public class Test4 {
    public static void main(String[] args) {
        File f=new File("d:"+File.separator+"test");
        f.mkdir();//创建文件夹
    }
}

创建一个文件夹

public class Test4 {
    public static void main(String[] args) {
        File f=new File("d:"+File.separator+"test");
        f.mkdir();//创建文件夹
    }
}

判断一个给定的路径是否目录

public class Test7 {
    public static void main(String[] args) {
        File f=new File("d:"+File.separator);
        if(f.isDirectory()){
            System.out.println(f.getPath()+"是目录");
        }else{
            System.out.println(f.getPath()+"不是目录");
        }
    }
}

判断所指定的文件是否存在

public class Test {
 public static void main(String[] args) {
		File fl = new File("d:\\test1.txt");
		File f2 = new File("c:\\biology.txt");
		System.out.println("文件" + fl.getName() + (fl.exists() ? " exists" : " not exist"));
		System.out.println("文件" + f2.getName() + (f2.exists() ? " exists" : " not exist"));
	}}

          对于文件的读(写)操作通常3个步骤进行:

n 使用 File 类打开一个文件
n 通过字节流或字符流的子类,指定输出的位置
n 进行读 / 写操作

                               关闭输入/输出 

                    向文本文件写数据

public static void main(String[] args) throws IOException {
        File f = new File("d:" + File.separator+"test.txt");
        OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建
        String str="Hello World";
        byte[] b=str.getBytes();
        out.write(b);//因为是字节流,所以要转化成字节数组进行输出
        out.close();
    }

                    以字节为单位文本文件写数据

public static void main(String[] args) throws IOException {
        File f = new File("d:" + File.separator+"test.txt");
        OutputStream out=new FileOutputStream(f);//如果文件不存在会自动创建
        String str="Hello World";
        byte[] b=str.getBytes();
        for(int i=0;i<b.length;i++){
            out.write(b[i]);
        }
        out.close();
    }

                 读取文本文件数据

public static void main(String[] args) throws IOException {
        File f = new File("d:" + File.separator+"test.txt");
        InputStream in=new FileInputStream(f);
        byte[] b=new byte[1024];
        int len=in.read(b);
        in.close();
        System.out.println(new String(b,0,len));
    }

            读取文本文件数据(逐个字节)

public static void main(String[] args) throws IOException {
        File f = new File("d:" + File.separator+"test.txt");
        InputStream in=new FileInputStream(f);
        byte[] b=new byte[(int) f.length()];
        for(int i=0;i<b.length;i++){
            b[i]=(byte) in.read();
        }
        in.close();
        System.out.println(new String(b));
    }

向文本文件写入数据

public static void main(String[] args) throws IOException {
        File f = new File("d:" + File.separator+"test.txt");
        Writer out=new FileWriter(f);
        String str="Hello World";
        out.write(str);
        out.close();
    }

从文本文件读取数据

 public static void main(String[] args) throws IOException {
        File f = new File("d:" + File.separator+"test.txt");
        Reader input=new FileReader(f);
        char[] c=new char[1024];
        int len=input.read(c);
        input.close();
        System.out.println(new String(c,0,len));
    }

   从文本文件读取数据(用循环方式,判断是否读到底)

public static void main(String[] args) throws IOException {
        File f = new File("d:" + File.separator+"test.txt");
        Reader input=new FileReader(f);
        char[] c=new char[1024];
        int temp=0;
        int len=0;
        while((temp=input.read())!=-1){
            c[len]=(char) temp;
            len++;
        }
        input.close();
        System.out.println(new String(c,0,len));
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值