io读取的输入输出,InputStream ,OutputStream 的read(),write()方法的详细介绍

今天复习io 流 遇到一些问题,分享一下,有关字节的读取,输入操作。

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class Demo {
	
	public static void main(String[] args) {
		
		//字节的输出输出流
		
		InputStream is = System.in;//标准的输入流对象   --读取操作
		OutputStream os = System.out;//标准的输出流对象---写的操作
		
		
		try {
			byte[] buffer = new byte[10]; //缓冲区  // 0 1 2 3 4 5 6 7 8 9 
			
			int len = 0;//读取之后的实际长度 //在UTF8编码下,回车\r   换行\n 也各占1个字节
			
			/*
			 * read方法参数:
			 *  b - 读入数据的缓冲区。
			 *  off - 数组 b 中将写入数据的初始偏移量。
			 *  len - 要读取的最大字节数。 
			 */
			while((len=is.read(buffer,0,4))!=-1){ //buffer缓冲区读入进去 2位置开始  0 1 2
				System.out.println("读取的实际长度--------------------------"+len);
				os.write(buffer, 0, 4); //buffer缓冲区写进去 2位置开始
				System.out.println("--------------------------");
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.pri
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
zip压缩DLL 可以使用程序自己来控制 压缩,解压. c#使用代码如下: /// /// 压缩和解压文件 /// public class ZipClass { /// /// 所有文件缓存 /// List files = new List(); /// /// 所有空目录缓存 /// List paths = new List(); /// /// 压缩单个文件 /// /// 要压缩的文件 /// 压缩后的文件全名 /// 压缩程度,范围0-9,数值越大,压缩程序越高 /// 分块大小 public void ZipFile(string fileToZip, string zipedFile, int compressionLevel, int blockSize) { if (!System.IO.File.Exists(fileToZip))//如果文件没有找到,则报错 { throw new FileNotFoundException("The specified file " + fileToZip + " could not be found. Zipping aborderd"); } FileStream streamToZip = new FileStream(fileToZip, FileMode.Open, FileAccess.Read); FileStream zipFile = File.Create(zipedFile); ZipOutputStream zipStream = new ZipOutputStream(zipFile); ZipEntry zipEntry = new ZipEntry(fileToZip); zipStream.PutNextEntry(zipEntry); zipStream.SetLevel(compressionLevel); byte[] buffer = new byte[blockSize]; int size = streamToZip.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, size); try { while (size < streamToZip.Length) { int sizeRead = streamToZip.Read(buffer, 0, buffer.Length); zipStream.Write(buffer, 0, sizeRead); size += sizeRead; } } catch (Exception ex) { GC.Collect(); throw ex; } zipStream.Finish(); zipStream.Close(); streamToZip.Close(); GC.Collect(); } /// /// 压缩目录(包括子目录及所有文件) /// /// 要压缩的根目录 /// 保存路径 /// 压缩程度,范围0-9,数值越大,压缩程序越高 public void ZipFileFromDirectory(string rootPath, string destinationPath, int compressLevel) { GetAllDirectories(rootPath); /* while (rootPath.LastIndexOf("\\") + 1 == rootPath.Length)//检查路径是否以"\"结尾 { rootPath = rootPath.Substring(0, rootPath.Length - 1);//如果是则去掉末尾的"\" } */ //string rootMark = rootPath.Substring(0, rootPath.LastIndexOf("\\") + 1);//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。 string rootMark = rootPath + "\\";//得到当前路径的位置,以备压缩时将所压缩内容转变成相对路径。 Crc32 crc = new Crc32(); ZipOutputStream outPutStream = new ZipOutputStream(File.Create(destinationPath)); outPutStream.SetLevel(compressLevel); // 0 - store only to 9 - means best compression foreach (string file in files) { FileStream fileStream = File.OpenRead(file);//打开压缩文件 byte[] buffer = new byte[fileStream.Length]; fileStream.Read(buffer, 0, buffer.Length); ZipEntry entry = new ZipEntry(file.Replace(rootMark, string.Empty)); entry.DateTime = DateTime.Now; // set Size and the crc, because the information // about the size and crc should be stored in the header // if it is not set it is automatically written in the footer. // (in this case size == crc == -1 in the header) // Some ZIP programs have problems with zip files that don't store // the size and crc in the header. entry.Size = fileStream.Length; fileStream.Close(); crc.Reset(); crc.Update(buffer); entry.Crc = crc.Value; outPutStream.PutNextEntry(entry); outPutStream.Write(buffer, 0, buffer.Length); } this.files.Clear(); foreach (string emptyPath in paths) { ZipEntry entry = new ZipEntry(emptyPath.Replace(rootMark, string.Empty) + "/"); outPutStream.PutNextEntry(entry); } this.paths.Clear(); outPutStream.Finish(); outPutStream.Close(); GC.Collect(); } /// /// 取得目录下所有文件及文件夹,分别存入files及paths /// /// 根目录 private void GetAllDirectories(string rootPath) { string[] subPaths = Directory.GetDirectories(rootPath);//得到所有子目录 foreach (string path in subPaths) { GetAllDirectories(path);//对每一个字目录做与根目录相同的操作:即找到子目录并将当前目录的文件名存入List } string[] files = Directory.GetFiles(rootPath); foreach (string file in files) { this.files.Add(file);//将当前目录中的所有文件全名存入文件List } if (subPaths.Length == files.Length && files.Length == 0)//如果是空目录 { this.paths.Add(rootPath);//记录空目录 } } /// /// 解压缩文件(压缩文件中含有子目录) /// /// 待解压缩的文件路径 /// 解压缩到指定目录 /// 解压后的文件列表 public List UnZip(string zipfilepath, string unzippath) { //解压出来的文件列表 List unzipFiles = new List(); //检查输出目录是否以“\\”结尾 if (unzippath.EndsWith("\\") == false || unzippath.EndsWith(":\\") == false) { unzippath += "\\"; } ZipInputStream s = new ZipInputStream(File.OpenRead(zipfilepath)); ZipEntry theEntry; while ((theEntry = s.GetNextEntry()) != null) { string directoryName = Path.GetDirectoryName(unzippath); string fileName = Path.GetFileName(theEntry.Name); //生成解压目录【用户解压到硬盘根目录时,不需要创建】 if (!string.IsNullOrEmpty(directoryName)) { Directory.CreateDirectory(directoryName); } if (fileName != String.Empty) { //如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入 if (theEntry.CompressedSize == 0) break; //解压文件到指定的目录 directoryName = Path.GetDirectoryName(unzippath + theEntry.Name); //建立下面的目录和子目录 Directory.CreateDirectory(directoryName); //记录导出的文件 unzipFiles.Add(unzippath + theEntry.Name); FileStream streamWriter = File.Create(unzippath + theEntry.Name); int size = 2048; byte[] data = new byte[2048]; while (true) { size = s.Read(data, 0, data.Length); if (size > 0) { streamWriter.Write(data, 0, size); } else { break; } } streamWriter.Close(); } } s.Close(); GC.Collect(); return unzipFiles; } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值