Inputstream_java input

Inputstream_java inputimportjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.InputStream;publicclassInputStreamDemo{/**InputStream字节输入流*FileInputStream:文件字节输入…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo {
   /*
    * InputStream字节输入流
    * FileInputStream:文件字节输入流
    * --->构造方法
    * 1.new FileInputStream(File file):
    *   传入File对象,代表了文件源
    * 2.new FileInputStream(String name):
    *   传入String类型的文件路径,该路径代表的文件是文件源。
    */
   
   public static void main(String[] args)  {
      File file = new File("D:/test/a.txt");
      InputStream is = null;
      try {
         is=    new FileInputStream(file);
      
      /*
       * int|read():一次一个字节,返回int  Unicode编码表
       */
      /*int i = is.read();
      System.out.println(i);
   */
      /*
       * int | read(byte[] bytes):把读取到的内容存入byte数组中,
       * 返回值为每次读取到的字节的个数。如果不存在则返回-1
       */
   /* String str = "";
      int len = 0;
      byte[] bytes = new byte[3];
      while((len = is.read(bytes)) != -1){//还有剩余内容
         //System.out.println(Arrays.toString(bytes));
         String s = new String(bytes,0,len);
         str += s;
      }
      System.out.println(str);
      */
      /*
       * int | read(byte[] bytes , int off,int len):
       * 把读取到的内容存入bytes数组中,每次从下标为off开始
       * 最多存储len个,返回值为每次读取到的字节的个数。
       * 如果读取不到内容则返回-1
       */
      byte[] bytes = new byte[3];
      String str = "";
      int len = 0;
      while((len = is.read(bytes, 1, 2)) != -1){
         String s = new String(bytes,1,len);
         str += s;
      }
      System.out.println(str);
      
      } catch (IOException e) {
         e.printStackTrace();
      }finally{
         if(is != null){
            try {
               is.close();
            } catch (IOException e) {
               e.printStackTrace();
            }
         }
      }
   }
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/191044.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ImportError: No module named bz2问题解决方法「建议收藏」

    ImportError: No module named bz2问题解决方法「建议收藏」问题:File”out/host/Linux-x86/obj/EXECUTABLES/mksnapshot.arm_intermediates/js2c.py”,line36,in  importbz2ImportError:Nomodulenamedbz2Traceback(mostrecentcalllast): File”out

    2022年6月1日
    40
  • 【SpringCloud】微服务笔记

    【SpringCloud】微服务笔记集中什么是微服务架构:1597213385700SpringCloud是微服务一站式服务解决方案,微服务全家桶。它是微服务开发的主流技术栈。它采用了名称,而非数字版本号。s方法gCloud和springCloudAlibaba目前是最主流的微服务框架组合。版本选择:选用springboot和springCloud版本有约束,不按照它的约束会有冲突。版本问题本次学习的各种软件的版本:boot使用的是数字作为版本。官网强烈建议升级到2.0以上cloud使用的是字母作为版本,伦

    2022年5月22日
    68
  • Android名词解释_古代汉语名词解释汇总

    Android名词解释_古代汉语名词解释汇总什么叫刷机刷机,是手机方面的专业术语,是指通过一定的方法更改或替换了手机原版系统中固有的一些语言、图片、铃声和软件版本或者操作系统,可以使手机功能更加完善。刷机可以是官方的,也可以是非官方的。再简单点说,刷机就是一种改变你手机操作系统的一种行为,就相当于给电脑装上不同版本的windows或则电脑重装系统,也就是刷手机的操作系统吧。目前常说的刷机就是用“更改替换了一些图片、铃声或菜单后的软件

    2022年10月10日
    4
  • linux非root用户启动nginx,Linux 普通用户启动nginx

    linux非root用户启动nginx,Linux 普通用户启动nginx现象nginx配置的user为www用户,root启动之后主进程显示root,子进程显示是www,但是切换到www用户之后执行nginx-t等报错services@pts/0$/usr/local/nginx/sbin/nginx-tnginx:[warn]the”user”directivemakessenseonlyifthemasterprocess…

    2022年9月26日
    0
  • pycharm配置解释器显示unsupported python 3.4和pipenv executable is not found

    pycharm配置解释器显示unsupported python 3.4和pipenv executable is not foundunsupportedpython3.4pycharm配置Python3.4解释器显示unsupportedpython3.4的环境变量无效,需要重新添加

    2022年8月26日
    10
  • 【愚公系列】2022年03月 微信小程序-富文本编辑器

    【愚公系列】2022年03月 微信小程序-富文本编辑器<viewclass=”container”style=”height:{{editorHeight}}px;”><editorid=”editor”class=”ql-container”placeholder=”{{placeholder}}”bindstatuschange=”onStatusChange”bindready=”onEditorReady”></editor></view><scroll-views

    2022年6月10日
    30

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号