File获取方法总结

package com.yang;

import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        File file=new File("E:\\BaiduNetdiskDownload\\ss\\he.txt");
        System.out.println(file);
        //获取绝对路径
        System.out.println(file.getAbsolutePath());


        //获取路径
        System.out.println(file.getAbsolutePath());

        //获取名称
        System.out.println(file.getName());

        //获取上层文件目录路径
        System.out.println(file.getParent());
//        获取文件长度
        System.out.println(file.length());

//        获取最后一次修改的时间
        System.out.println(file.lastModified());
//        获取指定目录的所有文件
        String[]files=file.list();
        for (String s : files) {
            System.out.println(s);
        }

//        获取指定目录下的所有文件
        File[]filess=file.listFiles();
        for (File ss : filess) {
            System.out.println(ss);
        }


    }
}

File常用判断方法

package com.yang;

import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        File file=new File("E:\\BaiduNetdiskDownload\\ss\\he.txt");
//         判断方法
//         是否是文件目录
        System.out.println(file.isDirectory());
//    是否是文件
        System.out.println(file.isFile());
//    是否存在
        System.out.println(file.exists());
//    是否可读
        System.out.println(file.canRead());
//        是否可写
        System.out.println(file.canWrite());
//    是否隐藏
        System.out.println(file.isHidden());
    }
    
}




读入流的基本操作

package com.yang;

import org.junit.Test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderWriterTest {

    /**
     * 读入流
     * @throws IOException
     */
    @Test
    public void testFileReader()  {
        FileReader fr=null;

        try {

            //        实例化流
            File file=new File("file.txt");
//指定具体的流
            fr=new FileReader(file);
// 数据的读取
//        int data=reader.read();
//        while (data!=-1){
//            System.out.print((char)data);
//            data=reader.read();
//        }
            int data;
            while ((data=fr.read())!=-1){
                System.out.print((char)data);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

         
    }
}

reader(char[]arr)读入数据


    @Test
    public void testFileReader1() throws IOException {

        FileReader fr=null;
        try {
            //        实例化
            File file=new File("file.txt");
//            读入流的实例化
            fr=new FileReader(file);
//            读入操作
            char[]cbug=new char[10];
            int len;
            while ((len=fr.read(cbug))!=-1){
                //错误写法
//                for (int i=0;i<cbug.length;i++){
//                    System.out.print(cbug[i]);
//                }
//
//                for (int i = 0; i < len; i++) {
//                    System.out.print(cbug[i]);
//                }
//                方式二
            String str=new String(cbug,0,len);
                System.out.println(str);

            }
//            关闭资源
            fr.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (fr!=null){
                fr.close();
            }
        }
    }

从内存中写出数据到硬盘的文件里

 @Test
    public void testtFileWriter() throws IOException {
//        实例化File对象
        File  file=new File("file.txt");
//        true在原有的基础上做增加
    FileWriter fr=new FileWriter(file,true);
    fr.write("Hello  !!!");
    fr.close();
    }

实现文本文件复制

//    实现文件的复制
@Test
    public void testFileCopy()  {

    FileReader fr= null;
    FileWriter fw=null;
    try {
        //        创建File对象,指明读入和写出的文件
        File file=new File("file.txt");
        File file1=new File("file1.txt");
        fr = new FileReader(file);
        fw=new FileWriter(file1);
//数据的读入  写出操作
        char[]buf=new char[5];
        int len;
        while ((len=fr.read(buf))!=-1){
            //每次写出len个字符串
            fw.write(buf,0,len);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
       if (fw!=null){
           try {
               fw.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
      if (fr!=null){
          try {
              fr.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
    }
}

FileInputStream读取文件

  @Test
    public void  testFileInputStreamtest(){
        FileInputStream fis=null;

        try {
            //        造文件
            File file=new File("file.txt");
//    造流
           fis=new FileInputStream(file);
//           读取数据
            byte[]buffer=new byte[5];
            int len;
            while ((len=fis.read(buffer))!=-1){
            String str=new String(buffer,0,len);
                System.out.println(str);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fis!=null){
                //关闭资源
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值