java 数据流



 

一.访问文件

FileInputStream

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

public class FileInputStreamTest {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\12894\\Downloads\\test.txt";
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(filePath);
            int data;
            // 读取文件直到文件结束(返回-1)
            while ((data = fis.read()) != -1) {
                // 将读取的字节转换为字符并打印
                char currentChar = (char) data;
                byte dataByte =(byte) data;
                System.out.println(currentChar+" "+data +" "+Integer.toBinaryString(data));
                Integer dataInteger = data;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

FileOutputStream
 

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

public class FileOutputStreamTest {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\**\\Downloads\\test.txt"; 
        FileOutputStream fos = null;
        String data = "Hello, World!"; // 要写入文件的数据

        try {
            fos = new FileOutputStream(filePath);
            //  new FileOutputStream(filePath);覆盖写入文件
            //  new FileOutputStream(filePath,true); 叠加写入文件
            byte[] byteData = data.getBytes();
            fos.write(byteData);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

 FileReader

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

public class FileReaderTest {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\**\\Downloads\\test.txt"; 
        Reader reader = null;

        try {
            reader = new FileReader(filePath);
            int data;
            while ((data = reader.read()) != -1) {
                // 将读取的字符直接转换为字符并打印
                char currentChar = (char) data;
                System.out.print(currentChar);
                System.out.println(data+" "+Integer.toBinaryString(data));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

 FileWriter 

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

public class FileWriterTest {
    public static void main(String[] args) {
        String filePath =  "C:\\Users\\**\\Downloads\\test.txt";
        Writer writer = null;
        String data = "Hello, World!"; // 要写入文件的数据

        try {
            writer = new FileWriter(filePath);
            // 将字符串写入文件
            writer.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭流
            try {
                if (writer != null) {
                    writer.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

二. 访问内存数组

ByteArrayInputStream

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;

public class ByteArrayInputStreamTest {
    public static void main(String[] args) throws UnsupportedEncodingException {
//        不需要try catch IOException 因为它不是外部资源相关的流
//        同时也不需要在关闭流,但是 ByteArrayInputStream 是 extends InputStream,所以也有close方法的实现只是什么都不做。
        byte [] byteArray = {10,20,30,50,60,70,80,90};
        ByteArrayInputStream bais = new ByteArrayInputStream(byteArray);
        int value;
//        返回 -1(表示流的末尾)
        while((value = bais.read()) != -1){
            System.out.println(value);
        }
        
        String str = "张三丰";
        byte[] bytesStr = str.getBytes("UTF-8");
        ByteArrayInputStream bais2 = new ByteArrayInputStream(bytesStr);
        while((value = bais2.read()) != -1){
            System.out.println(value);
        }
    }
}

ByteArrayOutputStream
 

public class ByteArrayOutputStreamTest {
    public static void main(String[] args) throws UnsupportedEncodingException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try{
            // FileOutputStream 是将字节写入文件,
            // ByteArrayOutputStream是将字节写入ByteArrayOutputStream的buf中
            baos.write("Hello, ".getBytes());
            baos.write("World!".getBytes());
        } catch (IOException e) {
            // ByteArrayOutputStream 的 write 方法不会抛出 IOException,
            // 但为了保持代码的通用性,这里还是保留了异常处理
            e.printStackTrace();
        }
        byte[] byteArray = baos.toByteArray();
        String str = new String(byteArray, "UTF-8");
        System.out.println(str);
    }
}

 CharArrayReader

import java.io.CharArrayReader;
import java.io.IOException;

public class CharArrayReaderTest {
    public static void main(String[] args) {
        char[] charArray = {'H','e','l','l','o'};
        CharArrayReader reader = new CharArrayReader(charArray);
        try{
            int charInt;
            while((charInt = reader.read()) != -1){
                System.out.print((char) charInt);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            // buf = null;
            reader.close();
        }
    }
}

CharArrayWriter
 

import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayWriterTest {
    public static void main(String[] args) {
        CharArrayWriter writer = new CharArrayWriter();
        try{
            writer.write("Hello,");
            writer.write("World!");
            char[] chars = {'T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 't', 'e', 's', 't'};
            writer.write(chars);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(writer.toString());
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值