java-io流

架构图:

 

架构图2:

 

代码1:

复制代码
package com.wkcto.chapter06.fileinputstream;

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

/**
 * 异常处理
 *
 */
public class Test05 {

    public static void main(String[] args) {
        
//        readData();         //以字节为单位读取文件内容, 异常处理, 手动关闭流
        readData2();         //以字节数组为单位读取文件内容, 异常处理, 自动 关闭流, 从JDK7开始
    }

    private static void readData2() {        
        try (
                FileInputStream fis = new FileInputStream("d:/abc.txt");
                ) {
            byte [] bytes = new byte[8];      //字符数组一般情况下是1024的偶数倍
            int len = fis.read(bytes);
            while( len != -1){
                System.out.print( new String(bytes, 0 ,len));
                len = fis.read(bytes);
            }
            
        } catch (Exception e) {
        }
    }

    private static void readData() {
        
        FileInputStream fis = null;
        try {
            fis = new FileInputStream("d:/abc.txt");
            int cc = fis.read();
            while( cc != -1 ){
                System.out.print( (char)cc );
                cc = fis.read();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if ( fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }                
            }            
        }
        
    }

}
复制代码

示例代码2:

复制代码
package com.wkcto.chapter06.fileinputstream;

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

/**
 * 演示FileOutputStream, 把程序中的数据保存到文件中
 * 
 *
 */
public class Test06 {

    public static void main(String[] args) throws IOException {
        //1)建立当前程序与文件之间的流通道, 如果文件不存在,会创建一个新的文件,如果文件已存在,会覆盖原来的内容
//        FileOutputStream fos = new FileOutputStream("d:/def.txt");
        //1)建立当前程序与文件之间的流通道, 如果文件不存在,会创建一个新的文件,如果文件已存在,原文件后面追加新的内容
        FileOutputStream fos = new FileOutputStream("d:/def.txt", true);     //以追加的方式打开文件
        //2)把数据保存到文件中
        //2.1 可以一次保存一个字节
        fos.write(97);
        fos.write(98);
        fos.write(99);
        //2.2 可以一次保存一个字节数组
        byte[]bytes = "wkcto is a NB Website".getBytes();
        fos.write(bytes);
        //2.3 换行 , 在Windows操作系统中,换行需要\r\n两个 字符
        fos.write('\r');
        fos.write('\n');
        //2.4 保存字节数组中部分字节
        fos.write(bytes, 0, 5);
        
        //3)关闭流通道
        fos.close();
    }

}
复制代码

 

posted on   荆棘人  阅读(128)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
历史上的今天:
2016-07-08 javascript方法扩展

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示