`

java 读取输入流

    博客分类:
  • Java
阅读更多

java 中如何读取输入流呢?

方式一:

/***
	 * Has been tested ok
	 * 
	 * @param in
	 * @return
	 * @throws IOException
	 */
	public static byte[] readBytes3(InputStream in) throws IOException {
		BufferedInputStream bufin = new BufferedInputStream(in);
		int buffSize = 1024;
		ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);

		// System.out.println("Available bytes:" + in.available());

		byte[] temp = new byte[buffSize];
		int size = 0;
		while ((size = bufin.read(temp)) != -1) {
			out.write(temp, 0, size);
		}
		bufin.close();
		in.close();
		byte[] content = out.toByteArray();
		out.close();
		return content;
	}

 

方式二:

/***
	 * get byte[] from <code>InputStream</code> Low efficiency
	 * 
	 * @param in
	 * @return
	 * @throws IOException
	 */
	@Deprecated
	public static byte[] readBytes2(InputStream in) throws IOException {
		byte[] temp = new byte[1024];
		byte[] result = new byte[0];
		int size = 0;
		while ((size = in.read(temp)) != -1) {
			byte[] readBytes = new byte[size];
			System.arraycopy(temp, 0, readBytes, 0, size);
			result = mergeArray(result, readBytes);
		}
		return result;
	}
/***
	 * 合并字节数组
	 * 
	 * @param a
	 * @return
	 */
	public static byte[] mergeArray(byte[]... a) {
		// 合并完之后数组的总长度
		int index = 0;
		int sum = 0;
		for (int i = 0; i < a.length; i++) {
			sum = sum + a[i].length;
		}
		byte[] result = new byte[sum];
		for (int i = 0; i < a.length; i++) {
			int lengthOne = a[i].length;
			if (lengthOne == 0) {
				continue;
			}
			// 拷贝数组
			System.arraycopy(a[i], 0, result, index, lengthOne);
			index = index + lengthOne;
		}
		return result;
	}

 

方式三:

/***
	 * 指定字符编码,无损地读取文本文件.推荐!
	 * 
	 * @param in
	 *            : 输入流,会关闭
	 * @param charset
	 *            : 字符编码
	 * @return
	 * @throws IOException
	 */
	public static StringBuffer getFullContent3(InputStream in, String charset)
			throws IOException {
		StringBuffer sbuffer = new StringBuffer();
		InputStreamReader inReader;
		// 设置字符编码
		if (ValueWidget.isNullOrEmpty(charset)) {
			charset = SystemHWUtil.CURR_ENCODING;
		}
		inReader = new InputStreamReader(in, charset);
		char[] ch = new char[1024];
		int readCount = 0;
		while ((readCount = inReader.read(ch)) != -1) {
			sbuffer.append(ch, 0, readCount);
		}
		inReader.close();
		in.close();
		return sbuffer;
	}

 

方式四:

/***
	 * 先读取出来字节数组,然后在包装成为字符串;效率不高,因为有拷贝操作(System.arraycopy)
	 * 
	 * @param in
	 * @param charset
	 * @return
	 * @throws IOException
	 */
	public static String getFullContent2(InputStream in, String charset)
			throws IOException {
		//并不是要读取的字节的长度
		int step = BUFFSIZE_1024;
		BufferedInputStream bis = new BufferedInputStream(in);

		// Data's byte array
		byte[] receData = new byte[step];

		// data length read from the stream
		int readLength = 0;

		// data Array offset
		int offset = 0;

		// Data array length
		int byteLength = step;

		while ((readLength = bis.read(receData, offset, byteLength - offset)) != -1) {
			// Calculate the current length of the data
			offset += readLength;
			// Determine whether you need to copy data , when the remaining
			// space is less than step / 2, copy the data
			if (byteLength - offset <= step / 2) {
				byte[] tempData = new byte[receData.length + step];
				System.arraycopy(receData, 0, tempData, 0, offset);
				receData = tempData;
				byteLength = receData.length;
			}
		}

		return new String(receData, 0, offset, charset);
	}

 

方式五:

/***
	 * write inputstream into outputStream ,haven't close stream.
	 * 
	 * @param ins
	 * @param outs
	 */
	public static void writeIn2Output(InputStream ins, OutputStream outs,
			boolean isCloseOut, boolean isCloseInput) {
		try {
			int resultInt = -1;
			byte[] bytes = null;
			bytes = new byte[4096];

			try {
				while ((resultInt = ins.read(bytes)) != -1) {
					outs.write(bytes, 0, resultInt);
				}
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (isCloseOut) {
					try {
						outs.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				if (isCloseInput) {
					try {
						ins.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		} finally {

		}
	}

 

方式六:

/***
	 * write inputstream into file according to specified length.
	 * 
	 * @param file
	 * @param ins
	 * @param length2
	 * @throws IOException
	 */
	public static void writeInputStream2File(File file, InputStream ins,
			long length2, boolean isShutOutputStream) throws IOException {
		String parentDir = SystemHWUtil.getParentDir(file.getAbsolutePath());
		File fatherFile = new File(parentDir);
		if (!fatherFile.exists()) {
			fatherFile.mkdirs();
		}
		FileOutputStream outs = new FileOutputStream(file);
		int readSize;
		byte[] bytes = null;
		bytes = new byte[(int) length2];

		long length_tmp = length2;
		while ((readSize = ins.read(bytes)) != -1) {
			length_tmp -= readSize;

			outs.write(bytes, 0, readSize);
			if (length_tmp == 0) {
				break;
			}
			if (length_tmp < 4096) {
				bytes = new byte[(int) length_tmp];
			}
		}
		if (isShutOutputStream) {
			outs.close();
		}

	}

 

方式七:

/***
	 * 从输入流获取字节数组
	 * @param br_right
	 * @param length2
	 * @return
	 * @throws IOException
	 */
	public static byte[] readBytesFromInputStream(BufferedInputStream br_right,
			int length2) throws IOException {
		int readSize;
		byte[] bytes = null;
		bytes = new byte[length2];

		long length_tmp = length2;
		long index =0;//start from zero
		while ((readSize = br_right.read(bytes,(int)index,(int)length_tmp)) != -1) {
			length_tmp -= readSize;
			if (length_tmp == 0) {
				break;
			}
			index=index+readSize;
		}
		return bytes;
	}

 

0
0
分享到:
评论
1 楼 hw1287789687 2014-02-19  
参考:http://hw1287789687.iteye.com/blog/1991889

相关推荐

    Java输入输出流及文件读写详解

    Java输入输出流及文件读写详解;Java输入输出流及文件读写详解。

    Demo(数据输出输入流).java

    java由数据流处理输入/输出(I/O)模式,其中,输入流是指打开一个从某数据源到程序的流,并从这个流中读取数据;输出流是为了将程序中的数据传输到某个目的地,在传输过程中,需要将数据写入这个流中。

    java 读取字节流

    java 读取字节流的例子,非常好。本例用System.in.read(buffer)从键盘输入一行字符,存储在缓冲区buffer中,再以FileOutStream的write(buffer)方法,将buffer中内容写入文件WriteFile.txt中,程序如下(本例程序放在...

    Java流(文件读写操作)

    – 输入流:只能从中读取字节数据,而不能向其写出数据 – 输出流:只能向其写入字节数据,而不能从中读取数据 • 按照流所处理的数据类型 – 字节流:用于处理字节数据。 – 字符流:用于处理Unicode字符数据。 • ...

    Java_输入输出流及文件读写详解

    Java_输入输出流及文件读写详解

    java输入输出流学生成绩管理

    比较简单的学生成绩管理,采用java输入输出流。

    java 输入输出流 实例

    java 输入输出流 I/O 实例 从文件读取信息,将处理过的信息写回文档。

    读取本地图片获取输入流,读取表中图片获取输出流

    // 读取本地图片获取输入流 // 读取表中图片获取输出流

    java输入输出流

    1)生成Hello.txt文件; 2)设计读取Hello.txt文件的输入流对象和加入序号后保存的输出流对象;

    java IO流总结.md

    2.数据的流向:输入流、输出流 3.流的角色:节点流、处理流 节点流:直接从数据源或目的地读写数据 处理流:不直接连接到数据源或目的地,而是“连接”在已存在的流(节点流或处理流)之上,通过对数据的...

    java输入输出流 流式输入与输出

    要串行化一个对象,必须与一定的对象输入/输出流联系起来,通过对象输出流将对象状态保存下来,再通过对象输入流将对象状态恢复。 java.io包中,提供了ObjectInputStream和ObjectOutputStream将数据流功能扩展至可...

    java中的标准输入输出流

    本文档涵盖了java中标准输入输出流,包含InputStream、OutputStream字节流还有字符流以及对文件的读写操作等

    Java的输入与输出.ppt )

    大部分程序都需要数据输入/输出处理,比如从键盘读取数据、向屏幕中输出数据、从文件中读或者向文件中写数据、在网络...当从其中读取数据时,它就是一个输入流。当然,键盘只是一个输入流,而屏幕则只是一个输出流。

    JAVA IO流技术

    输入流:数据源到程序(InputStream、Reader读进来) 输出流:程序到目的地(OutPutStream、Writer写出去) 处理数据单元: 字节流:按照字节读取数据(InputStream、OutputStream) 字符流:按照字符读取数据(Reader...

    Java_输入输出流及文件读写详解.docx

    Java_输入输出流及文件读写详解

    java输入输出流实习题

    1. 撰写类MyLog,实现接口Loggable,完成在本地文件“MyLog.log”文件中的日志读写,要求记录的日志内容包括:当前时间,日志类型,日志内容。要求可以累加写入日志文件。

    java_io详解

    Java中IO是以流为基础进行输入输出的,所有数据被串行化写入输出流,或者从输入流读入。此外,Java也对块传输提供支持,在核心库java.nio中采用的便是块IO。关于NIO我们在后面还有专门的讨论。  流IO的好处是简单...

    java流实现文件拷贝

    今天复习了一下java流的知识,做了个小例子,实现文件的拷贝。(中文和空格都能很好的读取)(PrintWriter\OutputStreamWriter\FileOutputStream)

    Java流总结

    • 根据数据流向不同分为:输入流和输出流 字符流和字节流 字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流 读取时,去查了指定的码表。 字节流和字符流的区别: • ...

    Java IO输入输出流

    –输入流:读取外部数据导入程序中(将持久化文件数据加载到内存) –输出流:将程序中的数据输出到磁盘或保存到磁盘(将内存中的数据持久化到磁盘) ②,按照处理数据类型的单位不同:字节流和字符流 –字节流:以...

Global site tag (gtag.js) - Google Analytics