写入指定长度的字节到文件

使用java ,如何把指定长度的字节写入文件呢,或者说如何从inputStream 中读取指定长度的字节写入outputStream中呢?

/***
	 * write inputstream into file according to specified length.
	 * 
	 * @param file
	 * @param ins
	 *            : not closed
	 * @param length2
	 * @throws IOException
	 */
	public static FileOutputStream writeInputStream2File(File file,
			InputStream ins, long length2, boolean isCloseOutputStream)
			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)) != SystemHWUtil.NEGATIVE_ONE/*-1*/) {
			length_tmp -= readSize;

			outs.write(bytes, 0, readSize);
			if (length_tmp == 0) {
				break;
			}
			//非常重要,千万不能去掉!!!
			 if (length_tmp < SystemHWUtil.BUFF_SIZE/*4096*/) {
			 bytes = new byte[(int) length_tmp];
			 }
		}
		outs.flush();
		if (isCloseOutputStream) {
			outs.close();
		}
		return outs;
	}

/***
	 * Not responsible for closing the output and input stream 写入指定长度的字节到输出流
	 * 
	 * @param fin
	 * @param fout
	 *            : The divided file
	 * @param length2
	 * @throws IOException
	 */
	public static void writeFromFile2File(InputStream fin, OutputStream fout,
			long length2) throws IOException {
		
		if (length2 == 0) {// want to write zero bytes
			// if (fout != null) {
			// fout.close();
			// }
			return;
		}
		int readSize;
		byte[] bytes = null;
		if (length2 >= SystemHWUtil.BUFF_SIZE) {
			bytes = new byte[SystemHWUtil.BUFF_SIZE];
		} else {
			bytes = new byte[(int) length2];
		}

		long length_tmp = length2;
		while ((readSize = fin.read(bytes)) != SystemHWUtil.NEGATIVE_ONE) {
			
			length_tmp -= readSize;
			fout.write(bytes, 0, readSize);
			if (length_tmp == 0) {
				break;
			}
			//非常重要,千万不能删除
			 if (length_tmp < SystemHWUtil.BUFF_SIZE) {
			 bytes = new byte[(int) length_tmp];
			 }
		}

	}

	/***
	 * Responsible for closing the output stream
	 * 
	 * @param fin
	 * @param outPutFile
	 * @param length2
	 *            :The number of bytes to be written
	 * @param append
	 *            : Whether additional
	 * @throws IOException
	 */
	public static void writeFromFile2File(FileInputStream fin, File outPutFile,
			long length2, boolean append) throws IOException {
		
		if (length2 == 0) {// want to write zero bytes
			return;
		}
		FileOutputStream fout = null;
		try {
			fout = new FileOutputStream(outPutFile, append/* 追加 */);
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		try {
			writeFromFile2File(fin, fout, length2);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			fout.flush();
			fout.close();// Close the stream
		}
	}

 上述代码见附件中io0007-find_progess\src\main\java\com\io\hw\file\util\FileUtils.java

依赖的包:is_chinese-0.0.2-SNAPSHOT.jar

参考:http://hw1287789687.iteye.com/blog/2023095

写入文件:

/***
	 * 写入文件
	 * @param content
	 * @param charset
	 * @param readAndWriteResult
	 * @param file
	 * @throws IOException
	 */
	private static void writeStubFile(String content, String charset,  File file) throws IOException {
		FileWriterWithEncoding fileW = new FileWriterWithEncoding(file, charset);
		fileW.write(content);
		fileW.close();
		
	}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值