Main

字节流与字符流
package com.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.imageio.stream.FileImageInputStream;

public class Main {
public static void main(String[] args) {
// 流 按方向分为 输入流 ,输出流
// 流按数据分为 字节流 ,字符流
//IO分两种流
//字节流 InputStream OutputStream
//字符流 Reader Writer
//具体的实现方法
//字节流 FileInputStream FileOutputStream
//字符流 FileReader FileWriter
// 获得文件
// 输入文件
// File inFile = new File(pathIn);
String pathIn = “/Users/dllo/Desktop/Hello/”;
File inFile = new File(“/Users/dllo/Desktop/input/”);
// 输出文件
File outFile = new File(“/Users/dllo/Desktop/output/”);
// 输入流
try {
// 输入流 对象
FileInputStream fis = new FileInputStream(inFile);
// 输出流 对象
FileOutputStream fos = new FileOutputStream(outFile);
// 创建一个存储输入流数据的byte数组
// byte [] buffer = new byte[1024];
byte[] buffer = new byte[(int) inFile.length()];
// 读流
fis.read(buffer);
// 写流
fos.write(buffer);

        // 关流
        fis.close();
        fos.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // 字符流
    File playFile = new File("/Users/dllo/Desktop/info/");
    try {
        // 字符流对象
        FileReader fr = new FileReader(playFile);
        BufferedReader reader = new BufferedReader(fr);
        String line = "";// 每一行的数据
        // 一行一行加一起的最终数据
        String result = new String();
        // 读出一行数据 赋值给line,一直读取,直到其为空
        while ((line = reader.readLine()) != null) {
            // 把读出来的数据都加入result里
            result += line;
        }
        System.out.println(result);// 打印读取的数据
        // 关流
        reader.close();
        fr.close();

    } catch (FileNotFoundException e) {

        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

















}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值