首页编程java编程java bytearrayinputstream java中输入stream的用途

java bytearrayinputstream java中输入stream的用途

编程之家2026-06-03816次浏览

大家好,关于java bytearrayinputstream很多朋友都还不太明白,今天小编就来为大家分享关于java中输入stream的用途的知识,希望对各位有所帮助!

java bytearrayinputstream java中输入stream的用途

Java InputStream流转换读取成byte[]字节数组方法及示例代码

Java中InputStream流处理是一个常见的操作,当需要将输入数据转换为byte[]数组时,有多种方法可供选择。本文将为您详细介绍这些转换方法,并提供相应的示例代码,帮助您更直观地理解和应用。

首先,最直接的方法是使用InputStream.read(byte[] b, int off, int len),这个方法会读取指定数量的字节到指定的byte数组中。例如:

byte[] bytes= new byte[1024];

int bytesRead= in.read(bytes);

if(bytesRead!=-1){

// bytesRead now holds the number of bytes read

java bytearrayinputstream java中输入stream的用途

}

另一种方式是使用InputStream.getChannel().read(ByteBuffer dst),通过NIO(New I/O)API,可以更高效地读取大量数据:

ByteBuffer buffer= ByteBuffer.allocateDirect(1024);

while(in.getChannel().read(buffer)!=-1){

buffer.flip();

byte[] bytes= new byte[buffer.remaining()];

java bytearrayinputstream java中输入stream的用途

buffer.get(bytes);

// process bytes...

buffer.clear();

}

最后,可以使用InputStream.toByteArray()方法,该方法会一次性读取所有数据并返回一个byte数组:

byte[] bytes= new byte[in.available()];

in.read(bytes);

以上就是Java InputStream流转换为byte[]字节数组的几种常见方法及其示例,希望对您的编程实践有所帮助。

关于BufferedInputStream和FileInputStream的区别

区别:FileInputStream是字节流,BufferedInputStream是字节缓冲流,使用BufferedInputStream读资源比FileInputStream读取资源的效率高(BufferedInputStream的read方法会读取尽可能多的字节),且FileInputStream对象的read方法会出现阻塞。

1、FileInputStream,直接操作本机I/O,把持着一个文件的句柄,说白了它是面向文件的。

2、BufferedInputStream,它只是面向字节流的,你可以不使用它,自己创建数组,将字节放在里面,也就缓存在jvm内存里,之后操作内存数据。

3、BufferedInputStream是套在某个其他的InputStream外,起着缓存的功能,用来改善里面那个InputStream的性能(如果可能的话),它自己不能脱离里面那个单独存在。

4、FileInputStream是读取一个文件来做InputStream。所以你可以把BufferedInputStream套在FileInputStream外,来改善FileInputStream的性能。

扩展资料

FileInputStream是Java语言中抽象类InputStream用来具体实现类的创建对象。FileInputStream可以从文件系统中的某个文件中获得输入字节,获取的文件可用性取决于主机环境。

FileInputStream的构造方法需要指定文件的来源,通过打开一个到实际文件的连接来创建一个FileInputStream,该文件通过文件系统中的 File对象 file指定。

参考资料:百度百科-FileInputStream

java 输入输出流 (被采纳为答案者加100分)

其中BufferedInputStream是FileInputStream的子类,你可以理解成同样处理一个文件,BufferedInputStream效率更高,原因是BufferedInputStream采用了更高效的字节流处理方式,

BufferedInputStream才用缓冲流把内在的缓冲器连接到I/O流,允许java程序对多个字节同时操作,这样就提高了效率。

inputstreamreader的构造函数带两个参数,一是关联到的文件,二是字符解码方式.所以实际上通过inputstreamreader实例读出来的东西已经不是磁盘上原始的字节数据了,而是根据你指定的解码方式(如果你没有指定,则使用系统缺省的,win2000下是gbk/gb2312)把字节流转换成了字符流,注意字节流和字符流的区别,一个字节就是8比特位(32位机器上),而一个字符含多少字节则与不同的编码/解码方式有关了,如gbk是一字节,utf-8是1-3的变长字节,utf-16是2个定长字节.

于是值得你注意的就是当你用inputstreamreader读文件时,你应该知道该文件被存储时是用什么方式编码的,否则你指定错了解码方式,读出来的就是乱码.但是退一步来说,在全英文环境下,问题也没这严重.因为所有的字符集在前七位上都是与ascii兼容的(我猜的,也许有的不是),然而当你的程序涉及中文字符时,肯定是会出错了.

那么fileinputstream的特点呢?它的构造函数就一个,即关联到的文件,既然没有指定解码方式,那它所做的就是只以字节流的方式读出文件而不做任何处理,你应该用一个字节数组来接受它,对该数组你以后还可以做任何想做的操作。

给你个例子,自己去测试

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.OutputStream;

import java.util.*;

public class test{

/* public static void main(String[] args){

String str=new String("Face recognition in the thermal infrared domain has received relatively little attention in the literature in comparison with recognition in visible-spectrum imagery");

StringTokenizer token=new StringTokenizer(str);

Hashtable ht=new Hashtable();

while(token.hasMoreTokens()){

String temp=new String(token.nextToken());

ht.put(temp,temp);

}

Enumeration en=ht.keys();

while(en.hasMoreElements()){

Object obj=en.nextElement();

System.out.print("KEY_NO:"+obj);

System.out.println("="+ht.get(obj));

}

}

*/

public static void main(String[] args){

try{

String file1="d:\\1.doc";

String file2="d:\\2.doc";

copyFile(file1,file2);

readFile(file2);

//fileCheck("d:\\test1.txt");

// readFile("D:\\test1.txt");

// readFileByte("D:\\test1.txt");

// readFileByFile("D:\\test1.txt");

} catch(IOException e){

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void testFile() throws IOException{

copyFile("D:\\test1.txt","D:\\test2.txt");

}

public static void copyFile(String inName,String outName) throws IOException

{

File tmp= new File(outName);

if(!tmp.canRead())tmp.createNewFile();

BufferedInputStream in= new BufferedInputStream(new FileInputStream(inName));

BufferedOutputStream out= new BufferedOutputStream(new FileOutputStream(outName));

copyFile(in,out,true);

}

public static void readFile(String inName) throws IOException

{

BufferedReader read= new BufferedReader(new InputStreamReader(new FileInputStream(inName)));

String b;

while((b=read.readLine())!=null)

print( b);

}

public static void readFileByte(String inName) throws IOException

{

BufferedInputStream read= new BufferedInputStream(new FileInputStream(inName));

int b= 0;

while((b=read.read())!=-1)

System.out.print((char)b);

}

public static void readFileByFile(String name) throws IOException

{

File tmp= new File(name);

FileReader fr= new FileReader(tmp);

BufferedReader br= new BufferedReader(fr);

String b;

while((b=br.readLine())!=null)

print(b);

}

public static void copyFile(InputStream in,OutputStream out, boolean close) throws IOException{

int b;

while((b=in.read())!=-1)

{

out.write(b);

}

in.close();

if(close)

out.close();

}

public static void print(Object o)

{

System.out.println(o);

}

public static void fileCheck(String name) throws IOException

{

print("---"+name+"---");

File f= new File(name);

if(!f.exists())

{

print("fle not exist!");

return;

}

print("Canonical name:"+f.getCanonicalPath());

String p= f.getParent();

if(p!=null)

print("Parent directory:"+p);

if(f.canRead())print("file can be read!");

if(f.canWrite())print("file can be writable!");

Date d= new Date();

d.setTime(f.lastModified());

print("last modified time:"+d);

if(f.isFile())

{

print("file size is:"+f.length()+" bytes");

}else if(f.isDirectory()){print("is a directry!");}

else{

print("neither a directory or a file!");

}

print("");

}

}

OK,关于java bytearrayinputstream和java中输入stream的用途的内容到此结束了,希望对大家有所帮助。

好用的ai智能助手(哪个ai智能助手最好用ai智能助手app前十名盘点)洛克王国暗夜之光?梦幻天使洛克王国