java中你容易忘记的基础大盘点_File

字符流:文本文件

字节流:视频 音频 图片 

FileInputStream

package com.ruge.modules.business;

import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

/**
 * @author 爱丽丝、如歌
 * @Description: TODO
 * @date 2018/1/23 10:45
 */
public class IO {
    /**
     * 第一种读取方式 一次性读取
     */
    @Test
    public  void testFileInputStream1() throws IOException {
        String path = "D:/io/testFile.txt";
        /**
         * 创建file的对象
         */
        File file= new File(path);
        /**
         * 如果file不存在  新建
         */
        if(!file.exists()){
            file.createNewFile();
        }
        /**
         * 创建FileInputStream类的对象  读取file
         */
        FileInputStream fileInputStream = new FileInputStream(file);
        /**
         * 调用 FileInputStream 的方法进行读取
         */
        while (fileInputStream.read()!=-1){
            System.out.println((char) fileInputStream.read());
        }
        /**
         * 关闭流
         */
        fileInputStream.close();
    }

    /**
     * 第二次读取方式  高效
     * @throws IOException
     */
    @Test
    public  void testFileInputStream2() throws IOException {
        String path = "D:/io/testFile.txt";
        /**
         * 创建file的对象
         */
        File file= new File(path);
        /**
         * 如果file不存在  新建
         */
        if(!file.exists()){
            file.createNewFile();
        }
        /**
         * 创建FileInputStream类的对象  读取file
         */
        FileInputStream fileInputStream = new FileInputStream(file);
        /**
         * 读取到数据要写入的数组
         */
        byte [] bytes = new byte[1024];
        int length;
        /**
         * 这种方式效率更加高,因为不用频繁的操作硬盘,一次就读了1M
         *byte[] bytes = new byte[1024];//为了读取方便,一次读取1M,
         *将读取到的数据保存到bytes这个字节数组中
         */
        while ((length = fileInputStream.read(bytes))!=-1){
            /**
             * 将数据变为字符串输出
             * 下面两种效果相同
             */
            System.out.print(new String(bytes, 0, length));
            System.out.print(new String(bytes));
        }
        /**
         * 关闭流
         */
        fileInputStream.close();
    }
}
 
 

FileOutputStream

package com.ruge.modules.business;

import org.junit.Test;

import java.io.*;

/**
 * @author 爱丽丝、如歌
 * @Description: TODO
 * @date 2018/1/23 10:45
 */
public class IO {
    /**
     * 文件写入
     * @throws IOException
     */
    @Test
    public void testFileOutStream() throws IOException {
        /**
         * path可以不存在 如果不存在  即创建
         */
        String path = "d:/io/testOut.txt";
        /**
         * 创建一个file 对象
         * 文件内容会覆盖
         */
        File file = new File(path);
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        /**
         * 第一种写入方式   文本全部写入
         */
        fileOutputStream.write(new String("素胚勾勒出青花笔锋浓转淡").getBytes());
        /**
         * 第二种写入方式  可控制写入
         */
        String fileText = "素胚勾勒出青花笔锋浓转淡";
        int end = fileText.length();
        fileOutputStream.write(fileText.getBytes(),0,end);
        fileOutputStream.close();
  }
}

文件copy

package com.ruge.modules.business;

import org.junit.Test;

import java.io.*;

/**
 * @author 爱丽丝、如歌
 * @Description: 文件copy
 * @date 2018/1/23 10:45
 */
public class IO {
    /**
     * 文件copy
     * @throws IOException
     */
    @Test
    public void testFileCopy() throws IOException {
        String inputFile;
        String outputFile;
        inputFile = "D:/io/testFile.txt";
        outputFile = "D:/io/testFile1.txt";
        /**
         * 1 提供读入,写出的文件
         */
        File file1 = new File(inputFile);
        File file2 = new File(outputFile);
        /**
         * 2 创建相应的节点流 FileInputStream FileOutputStream
         */
        FileInputStream inputStream = new FileInputStream(file1);
        FileOutputStream outputStream = new FileOutputStream(file2);
        /**
         * 3 将创建的节点流的形参作为形参传递给缓冲流的构造器中
         */
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
        /**
         * 4 具体的文件复制的操作
         */
        byte[] bytes = new byte[10240];
        int len;
        while ((len = bufferedInputStream.read(bytes))!=-1){
            bufferedOutputStream.write(bytes);
        }
        bufferedOutputStream.close();
        bufferedInputStream.close();
  }
}


FileInputStream
import org.junit.Test;

import java.io.*;

public class Main {
    /**
     *因为InputStreamReader是字节输出(汉字会被分为两个字节),
     * 而BufferedReader是它的“包装”(整行读取),效率更高,所以配合使用更好。
     *可以通过BufferedReader 流的形式进行流缓存,之后通过readLine方法获取到缓存的内容。
     * @throws Exception
     */
    @Test
    public void testFileInputStream() throws Exception {
        File file = new File("D:/a.txt");
        FileInputStream inputStream = new FileInputStream(file);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"gbk");
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String lineText = "";
        while ((lineText = bufferedReader.readLine())!=null){
            lineText+=lineText;
            System.out.println(lineText);
        }
        bufferedReader.close();
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值