IO流总结

本文详细介绍了Java中不同IO流(FileInputStream, FileOutputStream, FileReader, FileWriter等)的使用,涉及字符流和字节流,以及BufferedReader和BufferedWriter的高级操作。重点展示了文本文件的读取、写入和内容处理,适合理解Java I/O基本概念和技术实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

		String fileName = "text.txt";   // 文件名
        FileOutputStream fos = new FileOutputStream(fileName);
        
        fos.write('a');  // 1
        fos.write("abc".getBytes());  // 2
        
        fos.close();
		String fileName = "text.txt";
        FileInputStream fis = new FileInputStream(fileName);

        int read;
        byte[] bytes = new byte[1024];
        while ((read = fis.read()) == -1) {  // 1
            System.out.println((char) read);
        }
        while ((read = fis.read()) == -1) {  // 2
            System.out.println(new String(bytes, 0, read));
        }

        fis.close();
		String fileName = "text.txt";
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(fileName));
        
        bos.write('a');  // 1
        bos.write("abcd".getBytes());  // 2
        bos.flush();
        
        bos.close();
		String fileName = "text.txt";
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));

        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = bis.read()) != -1) {  // 1
            System.out.println((char)read);
        }
        while ((read = bis.read(bytes)) != -1) {  // 2
            System.out.println(new String(bytes, 0, read));
        }
        
        bis.close();
		String fileName = "text.txt";
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(fileName));

        osw.write('a');  // 1
        osw.write("abc");  // 2
        osw.flush();

        osw.close();
		String fileName = "text.txt";
        InputStreamReader isr = new InputStreamReader(new FileInputStream(fileName));

        int read;
        char[] chars = new char[1024];
        while ((read = isr.read()) != -1) {  // 1
            System.out.println((char)read);
        }
        while ((read = isr.read(chars)) != -1) {  // 2
            System.out.println(new String(chars, 0, read));
        }
        
        isr.close();
		String fileName = "text.txt";
        FileWriter fw = new FileWriter(fileName);
        
        fw.write('a');  // 1
        fw.write("abc");  // 2
        fw.flush();
        
        fw.close();
		String fileName = "text.txt";
        FileReader fr = new FileReader(fileName);
        
        int read = 0;
        char[] chars = new char[1024];
        while ((read = fr.read()) != -1) {  // 1
            System.out.println((char) read);
        }
        while ((read = fr.read(chars)) != -1) {  // 2
            System.out.println(new String(chars, 0, read));
        }
        
        fr.close();
		String fileName = "text.txt";
        BufferedWriter bw = new BufferedWriter(new FileWriter(fileName));

        bw.write('a');  // 1
        bw.write("abc");  // 2
        bw.newLine();
        bw.flush();

        bw.close();
		String fileName = "text.txt";
        BufferedReader br = new BufferedReader(new FileReader(fileName));

        int read = 0;
        char[] chars = new char[1024];
        String line = "";
        while ((read = br.read()) != -1) {  // 1
            System.out.print(read);
        }
        while ((read = br.read(chars)) != -1) {  // 2
            System.out.print(new String(chars, 0, read));
        }
        while ((line = br.readLine()) != null) {  // 3,最常用
            System.out.println(line);
        }

        br.close();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值