Java-IO-文件的创建及读取(一)

这里了解一下IO的用法。IO在网络传递数据,文件的复制,上传下载都会用到,就是以流为基础进行输入输出。根据官网API Java IO包中,流的类比较多,涉及到文件,字节流,字符流,对象流,管道流。因为知识有限,对理论不是很了解,稍微了解一点用法。这里就了解一下File类。

(一)创建一个文件夹

//File(String pathname) 根据参数创建一个新的File实例
public static File createDir(String pathName){
        File file  = new File(pathName);
        //Creates the directory named by this abstract pathname.用pathName名字创建一个目录
        file.mkdir();
        return file;
}

//File(File parent, String child) 根据parent文件夹的路径创建一个 在parent文件夹下的文件夹
public static File createDirInParent(File parent,String child) throws IOException{
        File file = new File(parent,child);
        file.mkdir();
        return file;
}

public static void main(String[] args) throws Exception {
        String pathname = "C://aaa";
        File parent  = FileUtil.createDir(pathname);
        System.out.println(parent.getAbsolutePath());
        File file = FileUtil.createDirInParent(parent, "bbbb");
        if(file.exists()){
            System.out.println("创建成功");
        }else{
            System.out.println("创建失败");
        }
    }

(二)创建一个文件

//File(String pathname) 根据参数创建一个新的File实例
public static File createFile(String pathName) throws Exception{
        File file = new File(pathName);
        file.createNewFile();
        return file;
}

(三)读取文件中的内容

//①FileInputStream
public static String readTxt(File file) throws Exception{
        FileInputStream fis = new FileInputStream(file);
        byte[] data = new byte[1024];
        int len = 0;
        String result = null;
        while((len=fis.read(data))!=-1){
            //data里存入了多少长度的字节文件,则创建String对象就读取多长。否则就读取1024的长度
            result = new String(data, 0, len);
        }
        fis.close();
        return result;
}

//②FileReader读取文件中的内容
public static String readTxtByReader(File file) throws Exception{
        FileReader reader = new FileReader(file);
        //char[]数组,字符型不是字节型数据
        char[] data = new char[1024];
        int len = 0;
        String result = null;
        while((len=reader.read(data))!=-1){
            result = new String(data, 0, len);
        }
        reader.close();
        return result;
}

(四)向文件中写入数据

//FileOutputStream 从一个文本文件读取数据到另一个文本文件
public static void writeTxt(File destination,File original) throws Exception{
        FileOutputStream fos = new FileOutputStream(destination);
        FileInputStream fis = new FileInputStream(original);
        byte[] data = new byte[1024];
        int len=0;
        while((len=fis.read(data))!=-1){
            fos.write(data, 0, len);
        }
        fis.close();
        fos.close();
}

//②FileWriter
public static void writeTxtByWriter(File destination,File original) throws Exception{
        FileWriter writer = new FileWriter(destination);
        FileReader reader = new FileReader(original);
        char[] data = new char[1024];
        while(reader.read(data)!=-1){
            writer.write(data);
        }
        reader.close();
        writer.close();
}

//用Writer中的append方法
public static void appendToTxt(File file,String msg) throws Exception{
        FileWriter writer = new FileWriter(file);
        writer.append(msg);
        writer.close();
}

上面这三种写入数据的方法都会覆盖掉原来文本文件中的数据。但是有时候会有直接添加到原数据的结尾的需求,这时候只需要
在创建流对象的时候,调用另一个构造函数。

//①java.io.FileWriter.FileWriter(File file, boolean append) throws IOException
//Constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning.
//调用该构造方法,将第二个参数设置为true就可以添加到末尾了
public static void appendToTxt(File file,String msg) throws Exception{
        FileWriter writer = new FileWriter(file,true);
        writer.append(msg);
        writer.close();
}

//②java.io.FileOutputStream.FileOutputStream(File file, boolean append) throws FileNotFoundException
public static void writeTxt(File destination,File original) throws Exception{
        FileOutputStream fos = new FileOutputStream(destination, true);
        FileInputStream fis = new FileInputStream(original);
        byte[] data = new byte[1024];
        int len=0;
        while((len=fis.read(data))!=-1){
            fos.write(data, 0, len);
        }
        fis.close();
        fos.close();
}

总结:在创建File对象时,如果给的路径下文件存在,则不会覆盖,如果不存在这个创建一个没有内容的文件。在new File(String pathname)之后,磁盘中不会显示,调用file.createNewFile()或者file.mkdir()指明file是文件夹还是其他文件类型之后,磁盘才会显示。使用FileWriter,需要关流,否则写入的文件不会显示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值