java输入输出流


/**
 *
 */
package com.XXX.stream;

import java.io.BufferedInputStream;
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.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Writer;

import org.apache.hadoop.io.ArrayFile.Reader;

/**
 * @author 
 *所有的文件在硬盘或在传输时都是以字节的方式进行的,
 *包括图片等都是按字节的方式存储的,而字符是只有在内存中才会形成,
 *所以在开发中,字节流使用较为广泛。
 */
public class StringByteStream {

    private static String mFilePath;

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        initialize();
//        OutPutStream();
//        InPutStream();
//        FileWriter();
//        FileReader();
        FileOutputStream();
//        FileInputStream();
//        ByteToStringInNoBuff();
//        ByteToStringInByBuff();
    }

    /**
     * 需要通过InputStreamReader转换字节流,否则输出乱码
     * */
    private static void FileInputStream() {
        // TODO Auto-generated method stub
        File mFile = new File(mFilePath);
        StringBuilder mBuilder = new StringBuilder();
        try {
            FileInputStream mFInputStream = new FileInputStream(mFile);
            InputStreamReader mInputStreamReader = new InputStreamReader(mFInputStream);
            BufferedReader mReader = new BufferedReader(mInputStreamReader);
            String str = null;
            while ((str = mReader.readLine()) != null) {
                mBuilder.append(str);
            }
            mFInputStream.close();
            mInputStreamReader.close();
//            byte[] b = new byte[2048];
//            int len = mFInputStream.read();
//            mFInputStream.read(b, 0, len);
//            String str = new String(b);
//            mFInputStream.close();
            System.out.println("mBuilder = " + mBuilder);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * FileOutputStream写入文件字节流,需要将其转换为字符才能写入
     * */
    private static void FileOutputStream() {
        // TODO Auto-generated method stub
        File mFile = new File(mFilePath);
        try {
            FileOutputStream mFileOutputStream = new FileOutputStream(mFile);
            String str = "测试文件流输出";
            byte [] b = new byte[2048];
            b = str.getBytes();
            mFileOutputStream.flush();
            mFileOutputStream.write(b);
            mFileOutputStream.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 字节流转化成字符流读取,通过缓冲区
     * */
    private static void ByteToStringInByBuff() {
        // TODO Auto-generated method stub
        File mFile = new File(mFilePath);
        try {
            InputStream mInputStream = new FileInputStream(mFile);
            InputStreamReader mInputStreamReader = new InputStreamReader(mInputStream);
            BufferedReader mBufferedReader = new BufferedReader(mInputStreamReader);
            String outputByBuffer = null;
//            outputByBuffer = mBufferedReader.read();返回int
            while ((outputByBuffer = mBufferedReader.readLine()) != null) {
                System.out.println("测试通过缓冲区字节流读取转换为字符流 = " + outputByBuffer);

            }
            mInputStreamReader.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    /**
     * 字节流转化成字符流读取,无缓冲区,则通过字符数组输出
     * */
    private static void ByteToStringInNoBuff() {
        // TODO Auto-generated method stub
        File mFile = new File(mFilePath);
        try {
            InputStream mInputStream = new FileInputStream(mFile);
            InputStreamReader mInputStreamReader = new InputStreamReader(mInputStream);
            char [] cha = new char [1024];
            int length = mInputStreamReader.read(cha);
            String srt = new String(cha);
            mInputStreamReader.close();
            System.out.println("测试无缓冲区读取字节流转换为字符流 = " + srt);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    /**
     * 字节流输出
     * */
    private static void OutPutStream() {
        // TODO Auto-generated method stub
        File mFile = new File(mFilePath);//1.通过file类找到文件
        OutputStream mOutputStream = null;
        try {//2.创建一个字节输出对象
            mOutputStream = new FileOutputStream(mFile, false);//false或不带该参数表示不追加输出
            String str = "测试字节输出流";
            byte b[] = str.getBytes();//3.需要将文本转换为字节流
            mOutputStream.flush();
            mOutputStream.write(b);//4.输出字节流
            mOutputStream.close();//5.关闭输出流
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 字节流读取*
     * */
    private static void InPutStream() {
        // TODO Auto-generated method stub
        File mFile = new File(mFilePath);
        InputStream mInputStream = null;
        try {
            mInputStream = new FileInputStream(mFile);
            byte[] cha = new byte[1024];
            int length = mInputStream.read(cha);
            mInputStream.read(cha, 0, length);
            String str = new String(cha);
            mInputStream.close();
            System.out.println("str = " + str);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    /**
     * 文件流输出,使用了缓冲区,在关闭输出流时会强制从缓冲区输出内容
     * FileWriter可直接写入字符
     * */
    private static void FileWriter() {
        // TODO Auto-generated method stub
        File mFile = new File(mFilePath);
        FileWriter mFileWriter = null;
        try {//创建字符输出对象,会自动创建所需文件
            mFileWriter = new FileWriter(mFile, false);
            String str = "测试字符输出流";
            mFileWriter.write(str);
            mFileWriter.flush();//输出
            mFileWriter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    /**
     * 字符流读取
     * */
    private static void FileReader() {
        // TODO Auto-generated method stub

        File mFile = new File(mFilePath);
        try {
            FileReader mFileReader = new FileReader(mFile);
            char [] cha = new char[1024];
            int length = mFileReader.read(cha);
            mFileReader.read(cha, 0, length);
            String str = new String(cha);
            mFileReader.close();
            System.out.println("str = " + str);

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


    }


    private static void initialize() {
        // TODO Auto-generated method stub
        mFilePath = "D:\\usersDate\\testStream.txt";
    }
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值