InputStream与OutPutStream简单使用

1、InputStream输入流:

        InputStream作为一个抽象类不能被单独使用,使用的都是继承InputStream的子类

        使用简单的几个步骤对文件进行操作

        先声明文件所在位置,再在程序中生成这一对象,当方法返回值为-1时证明结束

package liu;

import org.junit.Test;

import java.io.*;

public class InputStreamTest {
    public static void main(String[] args) {


    }

    @Test
    public void readFile(){
        String filePath = "d:\\new2.txt";
        FileInputStream fileInputStream = null;
        int read = 0;
//        创见FileInputStream 对象,用于读取文件
        try {
             fileInputStream = new FileInputStream(filePath);
//            从输入流读取一个字节数据。如果没有输入可用,此方法将阻止
//            如果返回-1,表示读取完毕
            //read = fileInputStream.read();
            while ((read = fileInputStream.read()) != -1){
                System.out.print((char) read);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void readFile01(){
        String filePath = "d:\\new2.txt";
        FileInputStream fileInputStream = null;
        int read = 0;
        byte[] buf = new byte[1024];
//        创见FileInputStream 对象,用于读取文件
        try {
            fileInputStream = new FileInputStream(filePath);
//          从该输入流读取最多b.length字节的数据字节组,此方法将阻塞,直到某些输入可
//            如果读取正常,返回实际读取的字节数
            //read = fileInputStream.read();
            while ((read=fileInputStream.read(buf)) != -1){
                System.err.print(new String(buf,0,read));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

2、OutputStream输出流:

 将内存中数据写入硬盘中。

FileOutPutStream中构造器中添加true

 FileOutputStream(filePath,true),后面不加true则表示覆盖文件,加了表示append沿最后添加

将字符串转换成byte
fileOutputStream.write(a.getBytes());
按照指定范围添加进文件中
fileOutputStream.write(a.getBytes(),0,3);
package liu;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * outputStream
 * 从内存-->硬盘
 */
public class OutputStreamTest {
    public static void main(String[] args) {

    }

    @Test
    public void Writer01(){
        //文件输出位置
        String filePath = "d:\\newFile.txt";
        FileOutputStream fileOutputStream = null;
        String a = "a";
        try {
            //FileOutputStream(filePath,true),后面不加true则表示覆盖文件,加了表示append沿最后添加
            fileOutputStream = new FileOutputStream(filePath,true);
            //将字符串转换成byte
            fileOutputStream.write(a.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭流
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void Writer02(){
        //文件输出位置
        String filePath = "d:\\newFile.txt";
        FileOutputStream fileOutputStream = null;
        String a = "a覅里 该垃圾费";
        try {
            //FileOutputStream(filePath,true),后面不加true则表示覆盖文件,加了表示append沿最后添加
            fileOutputStream = new FileOutputStream(filePath);
            //将字符串转换成byte
            //fileOutputStream.write(a.getBytes());
            //fileOutputStream.write(a.getBytes());
            //fileOutputStream.write(a.getBytes());
            //按照指定范围添加进文件中
            fileOutputStream.write(a.getBytes(),0,3);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                //关闭流
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

总结:

通过使用两个类对文件进行读与写的操作,步骤并无太大区别。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值