Java字符流,字节流,音频流,流媒体等控制

/**
*将指定目录下的所有文件和其子目录下所有文件压缩陈zip文件。
*@throws java.lang.Exception
*将其指定目录下的所有文件和
*@param path  文件相对路径
*@param files File对象数组,保存路径下的文件
*@throws java.lang.Exception
*/
public static void zip(ZipOutputStream out,String path,File[] files)throws Exception
{
	ZipEnty entry=null;
	InputStream in;
	String filename;

	for(int i=0; i<files.length; i++){
		filename = path+files[i].getName();
		
		if(!files[i].exists())
		   continue;
		if(files[i].isDirectory()){
			File[] tempFiles=files[i].listFiles();
			zip(out,filename+File.separator,tempFiles);
			continue;
		}		
		
		entry=new ZipEntry(filename);
		out.putNextEntry(entry);
		in =new BufferedInputStream(new FileInputStream(files[i]));
		copyStream(in,out);
		
		in.close();
		out.flush();
	}
}

/**
*将系统指定的压缩文件,解压到系统指定的路径下。
*@param zipFilename zip文件
*@param destPath 解压缩路径
*@throws java.lang.Exception
*@throws java.lang.Exception
*/
public static void unzip(String zipFilename,String destPath)throws Exception
{	
	File file=new File(zipFilename);
	if(!file.exists())
		return;
	File path=new File(destPath);
		mkdirs(path);
	ZipFile zf=new ZipFile(file);
	Enumeration enu=zf.getEntries();
	ZipEntry entry=null;
	File temp,parent;
	
	OutputStream out;
	while(enu.hasMoreElements){
		entry=(ZipEntry)enu.nextElement();
			if(entry.isDirecotry())
				continue;
			
			temp=new File(destPath+File.separator+entry.getName());
			parent=new File(temp.getParent());
			
			mkdirs(parent);

			InputStream in=zf.getInputStream(entry);
			out=new FileOutputStream(temp);

			copyStream(in,out);
			in.close();
			out.close();
	}
		zf.close();
}

/**
*将文件从源路径拷贝到目的路径下。
*@param src 源文件路径字符串(由文件路径和文件名组成)
*@param dest 目标文件路径
*/
public static void copyFile(String src,String dest)
{
	try
	(
		File srcFile=new File(src);	
		if(!srcFile.exists())
		{return;}
		File destFile=new File(dest);
		mkdirs(destFile);

		InputStream in =new FileInputStream(src);
		OutputStream out=new FileOutputStream(dest+File.separator+srcFile.getName());
		
		copyStream(in,out);
		in.close();
		out.close();
	)
	catch(IOException e)
	{e.printStackTrace();}
}

/**
*拷贝数据流
*@param in 输入流
*@param out 输出流
*@throws IOException
*@throws java.io.Exception
*/
public static void copyStream(InputStream in,OutputStream out)throws IOException
{
	byte[] buff=new byte[4096];
	int bytesRead;

	while(-1!=(bytesRead=in.read(buff,0,buff.length)))
	{
		out.write(buff,0,bytesRead);
		out.flush();
	}
}
public static void copyStream(byte[] buff, OutputStream out)throws IOException
{
	out.write(buff);
	out.flush();
}

/**
*创建文件目录
*@param name 目录名
*/
public static void mkdirs(String name)
{
	if(!Validators.isNull(name))
	{
		File file=new File(name);
		mkdirs(file);
	}
}

/**
*创建文件目录
*@param dir File对象
*/
public static void mkdirs(File dir)
{
	if(!dir.exists()){dir.mkdirs();}
}

/**
*读取系统指定的文件,并且以字符串的形式返回文件内容。
*@param 文件名
*@return 文件内容
*/
public static String readString(String name)
{
	return readString(new File(name));
}

/**
*读取系统指定的文件,并且以字符串的形式返回文件内容。
*@param file File 对象
*@return  文件内容
*/
public static String readString(File file)
{
	try
	{
		return readString(new FileInputStream(file));
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return null;
	}
}

/**
*读取输入流,并且以字符串的形式返回输入流内容。
*@param in 输入流
*@return 输入流内容 
*/
public static String readString(InputStream in)
{
	try
	{
		StringBuffer contents=new StringBuffer();
		BufferedReader brRead=new BufferedReader(new InputStreamReader(in,Encoding.ISO));
		String line="";
		while((line=brRead.reabLine())!=null)
		{
			contents.append(GenericUtils.isoToDefault(line));
			contents.append("\r\n");
		}
		brRead.colse();
		return contents.toString();
	}
	catch(Exception ex)
	{
		ex.printStackTrace();
		return null;
	}
}

/**
*将输入流读取到缓存流中
*@param in 输入流
*@return 输入流内容
*/
public static byte[] readBytes(InputStream in)
{
	try
	{
		ByteArrayOutputStream bout=new ByteArrayOutputStream();
		copyStream(in,bout);
		byte[] buff=bout.boByArray();
		in.close();
		bout.close();
		return buff;
	}
	catch(Exception ex)
	{
		ex.printStackTrace();
		return null;
	}
}

/**
*从zip文件中读取文件内容
*@param zipFile zip文件完整路径
*@param entryFile zip文件内的文件名称
*@return
*/
public static byte[] readZipBytes(String zipFile,String entryFile)
{
	try
	{
		byte[] buff=null;
		ZipFile zf=new ZipFile(zipFile);
		
		ZipEntry entry=null;
		Enumeration enu=zf.getEntries();
		while(enu.hasMoreElements)
		{
			entry=(ZiEntry)enu.nextElement();
			if(entry.isDirectory())
				continue;
			if(entryFile.equals(entry.getName().trim()))
			{
				InputStream in=zf.getInputStream(entry);
				buff=readBytes(in);
				break;
			}
		}
		zf.close();
		return buff;
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return null;
	}
}

/**
*将指定的文件内容写入到指定的文件对象中。
*@return true 成功 false 失败
*@param file File对象
*@param contents 文件内容
*@return boolean布尔型
*/
public static boolean writeString(File file,String contents)
{
	return writeString(file,contents,false);
}

/**
*将指定的文件内容写入到指定的文件对象中。
*@return true 成功 false 失败
*@param file File对象
*@param contents 文件内容
*@param append 是否追加
*@return boolean
*/
public static boolean writeString(File file,String contents,boolean append)
{
	try
	{
		DataOutputStream dos=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file,append)));
		dos.writeByte(contents);
		dos.flush();
		dos.close();
		return true;
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return false;
	}
}

/**
*将指定的文件内容写入到指定的文件对象中。
*@param name 文件名
*@param contents 文件内容
*@param true 成功 false 失败
*@param append 是否追加
*@return boolean
*/
public static boolean writeString(String name, String contents,boolean append)
{
	return writeString(new File(name),contents,append);
}

/**
*将指定的文件内容写入到指定的文件对象中。
*@param name 文件名
*@param contents 文件内容
*@return true 成功 false 失败 
*/
public static boolean writeString(String name,String contents)
{
	return writeString (new File(name),contents);
}

/**
*将byte数组中的内容写入到File对象中。
*@param file 文件
*@param buff 文件内容
*@return true写文件成功 false写文件失败
*/
public static boolean writeBytes(File file,byte[] buff)
{
	try
	{
		OutputStream out=new FileOutputStream(file);
		out.write(buff);
		out.flush();
		out.close();
		return true;
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return false;
	}
}

/**
*将byte数组中的内容输出到文件中。
*@param name 文件名
*@param buff 文件内容
*@return true写文件成功 false写文件失败
*/
public static boolean writeBytes(String name,byte[] buff)
{
	return writeBytes(new File(name),buff);
}

/**
*将输入流写入文件中。
*@param name 文件名
*@param in 输入流
*@return true写文件成功 false写文件失败
*/
public static boolean write(String name,InputStream in)
{
	return write(new File(name),in);
}

/**
*将输入流写入文件中。
*@param file 文件
*@param in 输入流
*@return true写文件成功 false写文件失败
*/
public static boolean write(File file,InputStream in)
{
	try
	{
		OutputStream out=new FileOutputStream(file);
		copyStream(in,out);
		return true;
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return false;
	}
}

/**
*将文件内容写入输入流中。
*@return true写文件成功 false写文件失败
*@param out 文件输出流
*@param content 文件内容
*@return boolen
*/
public static boolean write(OutputStream out,String content)
{
	try
	{
		out.write(content.getBytes(Encoding.DEFAULT));
		return true;
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return false;
	}
}

/**
*将文件对象的内容写到输出流中。
*@return true写文件成功 false写文件失败
*@param out 文件输出流
*@param file File对象
*@return boolean
*/
public static boolean write(OutputStream out,File file)
{
	try
	{
		InputStream in=new FileInputStream(file);
		copyStream(in,out);
		return true;
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return false;
	}
}

/**
*将GZIP文件写到输出流中。
*@return true写文件成功 false写文件失败
*@param out 文件输出流
*@param file File对象
*@return boolean
*/
public static boolean write(OutputStream out,File file)
{
	try
	{
		InputStream in=new GZIPInputStream(new(FileInputStream(file)));
		copyStream(in,out);
		return true;
	}
	catch(Exception e)
	{
		e.printStackTrace();
		return false;
	}
}

/**
*获取CRC32效验码。
*@param buf 缓存流
*@return CRC32效验码
*/
public static String getCRC32(byte[] buf)
{
	CRC32 crc32=new CRC32();
	crc32.update(buf);
	long value=crc32.getValue();
	String hexValue=Long.toHexString(value).toUpperCase();
	for(int i=hexValue.length();i<8;i++)
		hexValue="0"+hexValue;
	return hexValue;
}

/**
*获取文件的扩展名。
*@return 文件的扩展名
*@param fileName 文件名
*@return java.lang.String
*/
public static String getExtendName(String fileName)
{
	if(Validators.isNull(fileName))
	{
		return null;
	}
	int index=fileName.lastIndexOf('.');
	if(index==-1)
	{	return null;}
	return fileName.substring(index+1).toLowerCase();
}

/**
*判断系统指定的文件是否是GZIP文件。
*@return true表示指定文件是GZIP格式文件 false则不是
*@param fileName 文件名
*@param file File对象
*@return boolean
*/
public static boolean isGZIPFile(String fileName)
{
	return Validators.equals(getExtendName(fileName),"gz");
}

/**
*移动文件。
*@return src 源路径
*@param dest 目录路径
*@return 
*/
public static boolean move(String src,String dest)
{
	File srcFile=new File(src);
        File destFile=new File(dest);
	if(destFile.exists())
	{
		destFile.delete();
	}
	if(!destFile.getParentFile().exists())
	{
		destFile.getParentFile().mkdirs();
	}
	return srcFile.renameTo(destFile);
}

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值