栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习之路(五十五)| IO流(二)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习之路(五十五)| IO流(二)

各自努力,最高处见!加油!

IO流(二)
  • 一、InputStream:字节输入流
  • 二、OutputStream:字节输出流

一、InputStream:字节输入流

InputStream抽象类是所有类字节输入流的超类。

InputStream常用的子类:

  1. FileInputStream:文件输入流
  2. BufferedInputStream:缓冲字节输入流
  3. ObjectInputStream:对象字节输入流

示例代码:

import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStream_ {
    public static void main(String[] args) {

    }

    @Test
    public void readFile01() {
        String filePath = "D:\Java_code\LearnPlus\news2.txt";
        int readData=0;
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(filePath);
            //从输入流读取一个字节数据,如果没有输入可用,此方法将被阻止。如果返回-1,表示读取完毕
            while ((readData=fileInputStream.read())!=-1){
                System.out.println((char)readData);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                fileInputStream.close();//关闭文件流,释放资源。
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    @Test
    public void readFile02() {
        String filePath = "D:\Java_code\LearnPlus\news2.txt";
        byte[] buf=new byte[8];//一次读取8个字节
        int readlen=0;
        FileInputStream fileInputStream = null;

        try {
            fileInputStream = new FileInputStream(filePath);
            //从输入流读取流中的数据到字节中,如果返回-1,表示读取完毕,如果读取正常,返回实际读取的字节数
            //new String(buf,0,readlen)表示将readlen个byte转成字符串
            while ((readlen=fileInputStream.read(buf))!=-1){
                System.out.print(new String(buf,0,readlen));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            try {
                fileInputStream.close();//关闭文件流,释放资源。
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

该类的read方法一次读取一个字节,最好不用该方式来读取中文汉字,一个UTF8格式的汉字占三个字节,如果按字节读取会造成乱码。

二、OutputStream:字节输出流


示例代码:

import org.junit.Test;

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

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

    }
    @Test
    public void writeFile(){
        String filepath="D:\Java_code\LearnPlus\news2.txt";
        FileOutputStream fileOutputStream=null;
        //使用write方法,程序执行后会将本次写入的内容覆盖文件原有的内容(默认方式)
        //注意:这里的覆盖不是指每一次write方法的执行都覆盖上一次的内容。

        try {
//            fileOutputStream=new FileOutputStream(filepath);//默认为覆盖原文件的内容
            fileOutputStream=new FileOutputStream(filepath,true);
            //后面第二个参数为true表示为write方法写入的时候再原文件的基础上追加内容
            fileOutputStream.write('h');//写入一个字节
            //写入一个字符串
            String str="HelloWorld";
            fileOutputStream.write(str.getBytes());//把一个字符串转成byte编码

            fileOutputStream.write(str.getBytes(),0,3);//指定byte的长度
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/345318.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号