IO:InputStream

InputStream类(java.io.InputStream)

public abstract class InputStream extends Object implements Closeable

构造方法:public InputStream()

普通方法:


public abstract int read()throws IOException


依次读取单个字节数据,如果没有内容则返回-1


public int read(byte[] b) throws IOException


读出输入流的数据,写入字节数组,返回实际读取到的长度,如果没有内容则返回-1


public int read(byte[] b, int off, int len) throws IOException


读出输入流指定长度的数据,写入字节数组的指定位置,返回实际读取到的长度,如果没有内容则返回-1。但是当指定长度为0时返回0;


public void close()throws IOException


关闭数据流

?

?

FileInputStream(java.io.FileInputStream)

public class FileInputStream extends InputStream

构造方法:


public FileInputStream(File file) throws FileNotFoundException


从文件创建输入流(File类)


public FileInputStream(String name) throws FileNotFoundException


从文件创建输入流(String类)

?

实例:


test1.txt的内容


0123456789abcdefghijklmn


Test2.txt的内容


01234

?


package wiki.jjcc.test.ips;

?

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

?

public
class Test1 {

????public
static
void main(String[] args) throws Exception {

????????String s = File.separator;

????????File file1 = new File("d:"+s+"temp"+s+"test1.txt");

????????File file2 = new File("d:"+s+"temp"+s+"test2.txt");

????????InputStream input = new FileInputStream(file1);

????????byte[] b1 = new
byte[10];

????????byte[] b2 = new
byte[10];

????????int
num1 = input.read(b1,3,6);

????????int
num2 = input.read(b2,3,6);

????????System.out.println(num1);

????????System.out.println("["+new String(b1)+"]");

????????System.out.println(num2);

????????System.out.println("["+new String(b2)+"]");

????????input.close();

????}

}

?


package wiki.jjcc.test.ips;

?

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

?

public
class Test1 {

????public
static
void main(String[] args) throws Exception {

????????String s = File.separator;

????????File file1 = new File("d:"+s+"temp"+s+"test1.txt");

????????File file2 = new File("d:"+s+"temp"+s+"test2.txt");

????????InputStream input = new FileInputStream(file2);

????????byte[] b1 = new
byte[10];

????????byte[] b2 = new
byte[10];

????????int
num1 = input.read(b1,3,6);

????????int
num2 = input.read(b2,3,6);

????????System.out.println(num1);

????????System.out.println("["+new String(b1)+"]");

????????System.out.println(num2);

????????System.out.println("["+new String(b2)+"]");

????????input.close();

????}

}


package wiki.jjcc.test.ips;

?

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStream;

?

public
class Test1 {

????public
static
void main(String[] args) throws Exception {

????????String s = File.separator;

????????File file = new File("d:"+s+"temp"+s+"test1.txt");

????????InputStream input = new FileInputStream(file);

????????byte[] b = new
byte[30];

????????int
foot = 0;

????????int
temp = 0;

????????while((temp=input.read())!=-1){

????????????b[foot++]=(byte)temp;

????????}

????????System.out.println(temp);

????????System.out.println("["+new String(b)+"]");

????????input.close();

????}

}

?

时间: 2025-01-04 17:09:07

IO:InputStream的相关文章

【java】io流之字节输入流:java.io.InputStream类及子类java.io.FileInputStream

1 package 文件操作; 2 3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.IOException; 6 import java.io.InputStream; 7 8 public class TestInputStream { 9 public static void main(String[] args) throws IOException { 10 File file=new F

Java IO: InputStream

原文链接 作者: Jakob Jenkov 译者: 李璟([email protected]) InputStream类是Java IO API中所有输入流的基类.InputStream子类包括FileInputStream,BufferedInputStream,PushbackInputStream等等.参考Java IO概述这一小节底部的表格,可以浏览完整的InputStream子类的列表. Java InputStream例子 InputStream用于读取基于字节的数据,一次读取一个字

关于Java IO InputStream 的一点整理!

程序的开发当中一直在用文件的读写,但是对于java当中输入流以及输出流只是会用不理解,一直以来想搞清楚其,但是一直没有执行(悲剧),今天早上抽出半个小时通过JDK API1.6.0中文版帮助逐步的了解下字节输入流读取字节的方法: 下面就说说InputStream当中read().read(byte[]  b).read(byte[] b.int off .int len)的使用以及区别 一.read()方法: public static void inputStreamRead1() { try

Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack.

1.错误描写叙述 八月 14, 2015 4:22:45 下午 com.opensymphony.xwork2.util.logging.jdk.JdkLogger error 严重: Exception occurred during processing request: Can not find a java.io.InputStream with the name [downloadFile] in the invocation stack. Check the <param name=

struts2文件下载出现Can not find a java.io.InputStream with the name的错误

今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [imageStream] in the invocation stack. Check the <param name="inputName">

Can not find a java.io.InputStream with the name [inputStream] in the invocation stack. Check the &lt;

最近项目需要用到上传下载,以前学jsp的时候直接用的是smartUpload,现在学的框架但是老师只是简单地教了框架的内容 对struts文件上传和下载没有涉及,没办法只能自己自学了!结果出现了上面的问题. 这个问题的根本原因网上都有说出来,但是没有给出的解决方案.原因是要返回的流为空,文件的路径有误导致文件的输入流为空! 所以最好在逻辑处理那块输出的你要下载文件的路径看是不是你要下载的路径!好了废话不多说哈! package com.iss.action; import java.io.Fil

struts2文件下载 出现Can not find a java.io.InputStream with the name的错误

成功代码: 前台界面jsp: <a style="text-decoration:none;" href="<%=path %>/main/frontNewsAction_downloadFile.action?fileName=<s:property value="fileTitle"/>">下载</a> Action文件:private String fileName;//get set方法 p

关于Mysql数据库longblob格式数据的插入com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V问题分析

当数据库字段为blob类型时 ,我们如果使用PreparedStatement中的setBinaryStream(int,InputStream,int)方法需要注意 在向blob字段类型中插入数据时,要使用javaio的inputstream,读入文件. 而相反从blob字段中读出数据时,同样使用javaio的inputstream,再用javaio的outputstream写入文件. 同clob的示例中的问题 如果在设置字节流的地方不加类型转换的话,如下: stat.setBinaryStr

Error: Default interface methods are only supported starting with Android N (--min-api 24): java.io.InputStream org.apache.poi.sl.usermodel.ObjectShape.readObjectData()

项目运行的时候,如果报错 Error: Default interface methods are only supported starting with Android N (--min-api 24): java.io.InputStream org.apache.poi.sl.usermodel.ObjectShape.readObjectData() 解决方案: 在app的build.gradle文件中添加以下代码 apply plugin: 'com.android.applicat