java基础-IO流

本文详细介绍了字节流和字符流的概念及其在Java中的应用。对比了字节流和字符流的区别,包括它们各自的操作单位。文章还提供了具体的输入输出流示例代码,展示了如何使用FileInputStream和FileReader进行文件读取,以及使用FileOutputStream和FileWriter进行文件复制和写入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述

字节流和字符流

1.区别
  • 字节流:操作的数据单元是8位的字节
  • 字符流:操作的数据单元是16位的字节
2.输入流
  • 字节输入流(InputStream)
int read():从输入流中读取单个字节,返回所读取的字节数据;
int read(byte[] b):从输入流中读取b.length个字节数据,并存储在字节数组b中,返回实际             读取的字节数;
int read(byte[] b , int off , int len):从输入流中最多读取len个字节数据,并将存放在数组b从off位置开始存储数据,返回实际读取到的字节数
  • 字符输入流(Reader)
int read()
int read(char[] cbuf)
int read(char[] cbuf , int off , int len)

3.FileInputStream和Reader

package com.gdy.file;

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

/**
 * Created by RS on 2017/10/16.
 */
public class FileInputStreamDemo {
    public static void main(String[] args) throws IOException {

        //创建字符输入流
        FileInputStream fis = new FileInputStream("test.txt");

        byte[] read1 = new byte[1024];
        //用于保存实际读取的字节数
        int readlength = 0 ;

        while((readlength = fis.read(read1)) > 0 ){
            System.out.println(new String(read1 , 0 , readlength));
        }

        fis.close();

    }
}
package com.gdy.file;

import java.io.FileReader;
import java.io.IOException;

/**
 * @author rs
 * Created by RS on 2017/10/17.
 */
public class FileReadDemo {
    public static void main(String[] args) throws IOException {

        FileReader fileRead = new FileReader("test.txt");
        char[] chbf = new char[32];
        int hasRead = 0 ;
        while ((hasRead = fileRead.read(chbf)) >0){
            System.out.println(new String(chbf,0,hasRead));
        }
    }
}
3.输出流

1.OutputStream和Writer

void write(int c)将指定的字节/字符数组中的数据输出到指定输出流中
void writer(byte[]/char[] buf):将字节数组/字符数组中的数据输出到指定输出流中
void wirter(byte[]/char[],int off.int len):将字节数组/字符数组中从off位置开始,长度为len的数据出处到输出流中
void witer(String str):将str字符串中包含的字符输出到指定的输出流中
void witer(String str,int off,int len):将sr字符串中从off位置开始,长度为len的字符输出到指定输出流中

2.FileInputStream复制文件

package com.gdy.file;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo {
    public static void main(String[] args) {
        try {
            //创建输入字节流
            FileInputStream fileInputStream = new FileInputStream("data/FileTest.txt");
            //创建输出字节流
            FileOutputStream fileOutputStream = new FileOutputStream("data/FileTest222.txt");
            byte[] bytes = new byte[1024];
            int readLength = 0 ;

            while( (readLength = fileInputStream.read(bytes) ) > 0){
                fileOutputStream.write(bytes , 0 , readLength);
            }


        }  catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3.FileWriter写文件

package com.gdy.file;

import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by RS on 2017/10/18.
 * 会覆盖之前的文件
 */
public class FIleWriterDemo {
    public static void main(String[] args) {
        FileWriter fileWriter = null;
        try {
             fileWriter = new FileWriter("newtest.txt");
             fileWriter.write("123");
            fileWriter.write("123");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值