IO流的读写

什么是io?

简单的来说,io是一种计算机用语。主要是用于处理数据的传输。
在这里主要写一下关于FileInputStream和OutputStream的一个简单使用。
  1. FileInputStream

    FileInputStream是Java语言中抽象类InputStream用来具体实现类的创建对象。FileInputStream可以从文件系统中的某个文件中获得输入字节
    

    1.1:常用的一些简单方法

    int available()
    返回下一次对此输入流调用的方法可以不受阻塞地从此输入流读取(或跳			过)的估计剩余字节数。
    void close()
    关闭此文件输入流并释放与此流有关的所有系统资源。
    protected void finalize()
    确保在不再引用文件输入流时调用其 close 方法。
    FileChannel getChannel()
    返回与此文件输入流有关的唯一 FileChannel 对象。
    FileDescriptor getFD()
    返回表示到文件系统中实际文件的连接的 FileDescriptor 对象,该文件系统		正被此 FileInputStream 使用。
    int read()
    从此输入流中读取一个数据字节。
    int read(byte[] b)
    从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
    int read(byte[] b, int off, int len)
    从此输入流中将最多 len 个字节的数据读入一个 byte 数组中。
    long skip(long n)
    从输入流中跳过并丢弃 n 个字节的数据。
    

    1.2:相关操作

public class FileInputStreamTest {
    public static void main(String[] args) throws IOException {
        FileInputStreamTest test = new FileInputStreamTest();
        test.FileInputStreamDemo();
    }

    /**
     * 读取文件   字节流方式
     * @throws IOException
     */
    public void FileInputStreamDemo() throws IOException {
        File file  = new File("D://io.txt");
        InputStream is  =  new FileInputStream(file);
        try {
            if(file.exists()){
                byte[] data  = new byte[1024];
                int foot  = 0 ; //字节数组的操作脚标
                int temp  = 0 ;  //接收每次读取的字节数据
                while ((temp=is.read())!=-1){
                    data[foot++] = (byte) temp;
                }
                is.close();
                System.out.println("foot:"+foot);
                // 加入GBK主要是处理一下乱码的中文
                System.out.println(new String(data,0,foot,"GBK"));
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            is.close();
        }
    }


    public void InputStreamReaderDemo() throws IOException{
        //创建字节输入流,传递文本文件
        FileInputStream fis=new FileInputStream("d:\\io.txt");
        //创建转换流对象,构造方法,包装字节输入流
        InputStreamReader isr=new InputStreamReader(fis,"GBK");
        char[] ch=new char[2014];
        int len=isr.read(ch);
        System.out.println(new String(ch,0,len));
        isr.close();
    }
}

io.txt文件
在这里插入图片描述
在这里插入图片描述结果
1.2 OutputStream

	1.2.1相关操作
	void write( int b );   //往流中写一个字节b 
	void write( byte b[ ] ); //往流中写一个字节数组b 
	void write( byte b[ ], int index, int len ); //把字节数组b中从下标index开始,长度为len的字节写入流中 
	flush( )  //刷空输出流,并输出所有被缓存的字节,由于某些流支持缓存功能,该方法将把缓存中所有内容输出到流中。 
	关闭流: 
	close( );       //关闭 

1.2.2:相关操作

public class OutputStreamTest {
    public static void main(String[] args) throws IOException {
        OutputStreamTest outputStreamTest = new OutputStreamTest();
        outputStreamTest.OutputStreamDemo();
        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println("localDateTime = " + localDateTime);
    }

    /**
     * 写入相关的数据进入文本
     * @throws IOException
     */
    public void OutputStreamDemo() throws IOException {
        // 写入的位置
        String url = "D://outio.txt";
        File file = new File(url);
        if(file.exists()){
            List<String> list = new ArrayList();
            list.add("J");
            list.add("A");
            list.add("V");
            list.add("A");
            list.add("I");
            list.add("O");
            list.add("测试");
            OutputStream outputStream = new FileOutputStream(file);
            // 循环list集合添加数据
            list.stream().forEach(i->{
                // 这里要实现换行操做
                String s = i+"\n";
                // 将获取到的一个字符串转换为byte
                byte[] str = s.getBytes();
                try {
                    outputStream.write(str);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
            // 关闭流
            outputStream.close();
        }
    }
}

操作前的文件操作后的结果

以上就是关于io的一个简单操作demo,不要忘记,操作了流要关闭流。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值