IO流写和读取文件

完美读取文件内容代码
FileReader fileReader = new FileReader("F:/logback.log");
        char[] chars = new char[1024];
        int len;//记录每次读取的字符长度
        while ((len = fileReader.read(chars)) != -1){
            String rs = new String(chars,0,len);
            System.out.println(rs);
        }

上述代码存在缺陷,当长度大于1024时会读不全

以下是改进措施

File file = new File("F://LOGBACK.log");
            FileReader fileReader = new FileReader(file);
            char[] chars = new char[(int) file.length()];//此处获取文件长度,一次读完
            int len;
            if ((len = fileReader.read(chars)) != -1){

                String rs = new String(chars,0,len);
                System.out.println(rs);
            }
            fileReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

 

 

完美写入代码

public static void writeCharToFile() throws IOException{
    String hello= new String( "hello word!");
    File file= new File( "d:/test.txt");
    //因为是用字符流来读媒介,所以对应的是Writer,又因为媒介对象是文件,所以用到子类是FileWriter
    Writer os= new FileWriter( file);
    os.write( hello);
    os.close();
}

Java IO类库的框架

 

字节流写文件

public static void writeByteToFile() throws IOException{
    String hello= new String( "hello word!");
     byte[] byteArray= hello.getBytes();
    File file= new File( "d:/test.txt");
     //因为是用字节流来写媒介,所以对应的是OutputStream 
     //又因为媒介对象是文件,所以用到子类是FileOutputStream
    OutputStream os= new FileOutputStream( file);
     os.write( byteArray);
     os.close();
}

字节流读文件

public static void readByteFromFile() throws IOException{
    File file= new File( "d:/test.txt");
     byte[] byteArray= new byte[( int) file.length()];
     //因为是用字节流来读媒介,所以对应的是InputStream
     //又因为媒介对象是文件,所以用到子类是FileInputStream
    InputStream is= new FileInputStream( file);
     int size= is.read( byteArray);
    System. out.println( "大小:"+size +";内容:" +new String(byteArray));
     is.close();
}

文件拷贝

package com.chenhao.io.byteIO;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author ChenHao
 *
 */
public class CopyFileDemo {

    /**
     * @param args
     * @throws FileNotFoundException 
     */
    public static void main(String[] args) {
        String src ="E:/xp/test";
        String dest="e:/xp/test/4.jpg";
        try {
            copyFile(src,dest);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("文件不存在");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("拷贝文件失败|关闭流失败");
        }
    }
    /**
     * 文件的拷贝
     * @param  源文件路径
     * @param  目录文件路径
     * @throws FileNotFoundException,IOException
     * @return 
     */
    public static void copyFile(String srcPath,String destPath) throws FileNotFoundException,IOException {
        //1、建立联系 源(存在且为文件) +目的地(文件可以不存在)  
        File src =new File(srcPath);
        File dest =new File(destPath);
        if(! src.isFile()){ //不是文件或者为null
            System.out.println("只能拷贝文件");
            throw new IOException("只能拷贝文件");
        }
        //2、选择流
        InputStream is =new FileInputStream(src);
        OutputStream os =new FileOutputStream(dest);
        //3、文件拷贝   循环+读取+写出
        byte[] flush =new byte[1024];
        int len =0;
        //读取
        while(-1!=(len=is.read(flush))){
            //写出
            os.write(flush, 0, len);
        }
        os.flush(); //强制刷出
        
        //关闭流
        os.close();
        is.close();
    }

}

字节流转换字符流

public static void convertByteToChar() throws IOException{
    File file= new File( "d:/test.txt");
    //获得一个字节流
    InputStream is= new FileInputStream( file);
    //把字节流转换为字符流,其实就是把字符流和字节流组合的结果。
    Reader reader= new InputStreamReader( is);
    char [] byteArray= new char[( int) file.length()];
    int size= reader.read( byteArray);
    System. out.println( "大小:"+size +";内容:" +new String(byteArray));
    is.close();
    reader.close();
}

参考

https://www.cnblogs.com/java-chen-hao/p/11083740.html

视频https://www.bilibili.com/video/BV1Cv411372m?p=160

 

posted @ 2022-05-22 22:32  kliziM  阅读(136)  评论(0)    收藏  举报