Java File 文件读取


点击查看代码
/**
 * 文件读取 (单文件读取和多文件读取 )
 */
public class FileRead {
    public static void main(String[] args) throws Exception {
        multiByteRead();
    }

    //单字节读取
    public static void singleByteRead() throws Exception {
        //读取该文件
        File file = new File("D://fileTest//zip//2022-07-18_20-10-52.jpg");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream("D://fileTest//zip//hjj.jpg");
        int index = 0;
        while ((index = fileInputStream.read()) != -1) {
            fileOutputStream.write(index);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }

    //多字节读取
    public static void multiByteRead() throws Exception {
        //读取该文件
        File file = new File("D://fileTest//zip//2022-07-18_20-10-52.jpg");
        FileInputStream fileInputStream = new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream("D://fileTest//zip//hjj1.jpg");
        int index = 0;
        byte[] bytes = new byte[1024];
        while ((index = fileInputStream.read(bytes)) != -1) {
            fileOutputStream.write(bytes, 0, index);
        }
        fileInputStream.close();
        fileOutputStream.close();
    }
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/279575.html

(0)
上一篇 2022年8月8日 19:27
下一篇 2022年8月8日 21:21

相关推荐

发表回复

登录后才能评论