唐祖庭 2021-10-23 15:04 采纳率: 95.9%
浏览 9
已结题

java 求帮助 我这个怎么改变一下?

img


public class Demo4 {
    public static void main(String[] args) {
        //创建输入流对象
        FileInputStream inputStream=new FileInputStream(new File("a.txt"));
        FileInputStream in=new FileInputStream("a.txt");
        // read
        // read单个字节
        //readSinngle(in);
        // read字节数组
        //创建一个容器杯子
        byte[] bytes = new byte[1024];
        // readMulti(in , bytes);
        // read字节数组 的部分
        int readCount =0;
        try {
            readCount=in.read(bytes,0,bytes.length);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(new String(bytes,0,readCount));
        // close
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void readMUlti(FileInputStream in, byte[] bytes) throws IOException{
        int readCount =in.read();
        System.out.println(readCount);
        // 把字节数据转化为字符串利用String的构造方法
        String s = new String(bytes , 0 , readCount);
        System.out.println(s);
    }

    private static void readSingle(FileInputStream in) throws IOException{
        int readData =in.read();
        System.out.println(readData);
        int readData2 = in.read();
        System.out.println(readData2);
        int readData3 = in.read();
        System.out.println(readData3);
        int readData4 =in.read();
        System.out.println(readData4);
    }
}

  • 写回答

1条回答 默认 最新

  • 两分与月 2021-10-23 15:21
    关注
            FileInputStream inputStream = null;
            FileInputStream in = null;
            //创建输入流对象
            try {
                inputStream = new FileInputStream(new File("a.txt"));
                in = new FileInputStream("a.txt");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月31日
  • 已采纳回答 10月23日
  • 创建了问题 10月23日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器