package com.app.common; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileFilter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Reader; import java.io.Writer; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; import org.dom4j.Document; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; /** *@Author:jilongliang *@Date:2013-8-11 *@version:1.0 *@Email:jilongliang@sina.com *=============================================================================================================== *实际上字节流在操作时本身不会用到缓冲区(内存),是文件本身直接操作的,而字符流在操作时使用了缓冲区,通过缓冲区再操作文件。 * *字符流和字节流是根据处理数据的不同来区分的。字节流按照8位传输,字符流按照16位传输 *由于字符流使用Unicode字符集,支持多国文字,因此若流要跨越多种平台传输,应使用字符流。 *=============================================================================================================== *char型是字符型,占2个字节,默认数值'\u0000',取值范围'\u0000'~'\uffff' *byte是字节型,占1个字节,默认数值0,取值范围-128~127 *boolean 无符号 8 位 *byte 无符号,8 位 *char 无符号,16 位 *short 有符号,16 位 *int \ 有符号,32 位 *long 有符号,64 位 *float 32 位 *double 64 位 *=============================================================================================================== *字节流输出--->>程序-->>字节流---->文件(直接操作文件) * *字符流输出--->>程序-->>字符流---->>缓存--->>文件(数据线放在缓存,之后再从缓存写入文件) * *------------------------------------------------------------------------------------------------------------------ *@Description:字节流 *@InputStream * FileInputStream, FilterInputStream,BufferedInputStream, CheckedInputStream, CipherInputStream, * DataInputStream, DeflaterInputStream, DigestInputStream, InflaterInputStream, LineNumberInputStream, * ProgressMonitorInputStream, PushbackInputStream ,AudioInputStream, ByteArrayInputStream, FileInputStream, * ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream ------------------------------------------------------------------------------------------------------------------- *@OutputStream * FileOutputStream, FilterOutputStream,BufferedOutputStream, CheckedOutputStream, CipherOutputStream, * DataOutputStream, DeflaterOutputStream, DigestOutputStream, InflaterOutputStream, LineNumberOutputStream, * ProgressMonitorOutputStream, PushbackOutputStream ,AudioOutputStream, ByteArrayOutputStream, FileOutputStream, * ObjectOutputStream, PipedOutputStream, SequenceOutputStream, StringBufferOutputStream * -------------------------------------------------------------------------------------------------------------------- * *@Description字符流 *@Writer * BufferedWriter, CharArrayWriter, FilterWriter, OutputStreamWriter, PipedWriter, PrintWriter, StringWriter *------------------------------------------------------------------------------------------------------------------ *@Reader * *BufferedReader, CharArrayReader, FilterReader, InputStreamReader, PipedReader, StringReader *------------------------------------------------------------------------------------------------------------------ */ @SuppressWarnings("all") public class IOUtil { private static String content = "",line=System.getProperty("line.separator");//换行相当于\n private static List fileList = new ArrayList(); private static File tempFile = null; private static final long KB = 1024;// KB private static final long MB = KB * KB;// MB private static final long GB = KB * MB;// GB private static final int byt = 2048; private static File f=new File(""); private static BASE64Encoder encoder = new BASE64Encoder();// 加密 private static BASE64Decoder decoder = new BASE64Decoder();// 解密 private static String webRootPath; private static String rootClassPath; /** * 读文件流 * @param formPath从哪里读取的文件路径 * @return */ public static String reader1(String formPath) { FileReader read = null; BufferedReader reader = null; try { read = new FileReader(new File(formPath)); reader = new BufferedReader(read); StringBuffer buffer = new StringBuffer(""); content = reader.readLine(); while (content != null) { buffer.append(content).append(line); content = reader.readLine(); } return content = buffer.toString();//返回 } catch (Exception e) { e.printStackTrace(); } finally { try { if (reader != null)reader.close(); if (read != null)read.close(); } catch (Exception e) { e.printStackTrace(); } } return "";//没值就返回空 } /** * 读文件 * @param formPath * @return */ public static String reader2(String formPath){ try { InputStream is=new FileInputStream(new File(formPath)); InputStreamReader reader=new InputStreamReader(is); //不够大的时候读不出来全部内容来的,他不像available拿多所就读多少 char c[] = new char[1024*1024];//1M=1024*1024,根据需求定 int len = reader.read(c); content=new String(c, 0, len); if(reader!=null)reader.close(); //if(is!=null)is.close(); return content; } catch (Exception e) { e.printStackTrace(); } return ""; } /** * InputStreamReader * ByteArrayOutputStream * ByteArrayInputStream * @param formPath * @return */ public static String reader3(String formPath){ String newName=""; try { InputStream is=new FileInputStream(new File(formPath)); InputStreamReader reader=new InputStreamReader(is); //不够大的时候读不出来全部内容来的,他不像available拿多所就读多少 char c[] = new char[is.available()];//1M=1024*1024,根据需求定 int len = reader.read(c); content=new String(c, 0, len); ByteArrayOutputStream bout = new ByteArrayOutputStream(); DataOutputStream dout = new DataOutputStream(bout); dout.writeUTF(content); byte[] buff = bout.toByteArray(); ByteArrayInputStream bin = new ByteArrayInputStream(buff); DataInputStream dis = new DataInputStream(bin); newName = dis.readUTF(); bout.flush(); dout.flush(); bout.close(); dout.close(); dis.close(); } catch (Exception e) { e.printStackTrace(); } return newName; } /** * 写文件 * BufferedWriter * BufferedReader * @param formPath * @param toPath */ public static boolean writer1(String formPath,String toPath){ Reader reader=null; Writer writer=null; boolean flag=true; BufferedWriter buffWriter=null; BufferedReader buffReader=null; try { reader=new FileReader(new File(formPath)); writer = new FileWriter(new File(toPath));//writer不能关闭 buffWriter=new BufferedWriter(writer);//这个写完可以关闭 buffReader=new BufferedReader(reader); content=buffReader.readLine(); while(content!=null){ buffWriter.write(line+content); content=buffReader.readLine(); buffWriter.flush();//只要用到缓冲区就flush//其实关闭了缓冲区,就是关闭缓冲区中流的对象. //写一次flush是为了防止停电就挂了~ } reader.close(); buffReader.close(); buffWriter.close(); } catch (Exception e) { e.printStackTrace(); return false; } return flag; } /** * 写文件 * PrintWriter * Writer * Reader * BufferedReader * @param formPath * @param toPath */ public static boolean writer2(String formPath,String toPath){ Reader reader=null;//FileReader reader=null; PrintWriter writer=null;//PrintWriter boolean flag=true; BufferedWriter buffWriter=null;//此对象有换行newLine BufferedReader buffReader=null;//为了提高效率,使用字符缓冲流BufferedReader try { reader=new FileReader(new File(formPath)); writer = new PrintWriter(new File(toPath));//writer不能关闭 buffWriter=new BufferedWriter(writer);//这个写完可以关闭 buffReader=new BufferedReader(reader); content=buffReader.readLine(); while(content!=null){ buffWriter.write(line+content); content=buffReader.readLine(); } reader.close(); buffReader.close(); buffWriter.close(); } catch (Exception e) { e.printStackTrace(); return false; } return flag; } /** * 写文件 * InputStream * OutputStream * FileInputStream * FileOutputStream * @param from * @param to */ public static void writer3(String from, String to) { try { InputStream in = new FileInputStream(new File(from)); OutputStream out = new FileOutputStream(new File(to)); byte[] buff = new byte[in.available()]; //byte[] buff = new byte[1024 * 1024]; int len = 0; while ((len = in.read(buff)) != -1) { out.write(buff, 0, len); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 写文件 * @param from * @param to */ public static void writer4(String from, String to) { try { InputStream in = new FileInputStream(new File(from)); OutputStream out = new FileOutputStream(new File(to)); byte[] buff = new byte[in.available()]; in.read(buff); out.write(buff); in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } } /** * 写文件 * @param formPath * @param toPath * @return */ public static boolean writer5(String formPath,String toPath) { try { InputStream input=new FileInputStream(new File(formPath)); OutputStream output=new FileOutputStream(new File(toPath)); byte [] byf=new byte[1024*1024];//这里就不能填那么大啦 byte不同char,byte int len=0; while((len=input.read())!=-1) { //input.read(byf);//读 output.write(byf, 0, len); //output.write(byf);//写 } if(output!=null)output.close(); if(input!=null)input.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * 文件处理 * Writer * OutputStreamWriter * @param content * OutputStreamWriter * @param toPath写到那个路径下 * @return */ public static boolean writer6(String content, String toPath) { boolean flag = true; try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(toPath), "utf-8"));//设置编码 out.write(line + content); out.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return flag; } /** * BufferedOutputStream 套DataOutputStream套FileOutputStream * 据说管道套管道效率效率比较高,具体没测试过! * BufferedInputStream bis = new BufferedInputStream(new DataInputStream(new FileInputStream(new File(formPath)))); * * @param formPath * @param toPath写到那个路径下 * @return */ public static boolean writer7(String formPath, String toPath) { boolean flag = true; /** *这里就不能填那么大啦 byte不同char,byte */ byte[] byt = new byte[1]; try { //BufferedInputStream bis = new BufferedInputStream(new FileInputStream(formPath)); BufferedInputStream bis = new BufferedInputStream(new DataInputStream(new FileInputStream(new File(formPath)))); DataOutputStream dos=new DataOutputStream(new FileOutputStream(new File(toPath))); BufferedOutputStream bos=new BufferedOutputStream(dos); while(bis.read(byt)!=-1) { bos.write(byt); System.out.println("文件已经写完!"); } bos.flush(); bos.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return flag; } /** * * BufferedInputStream * ByteArrayOutputStream * @param formPath * @param toPath * @return */ public static boolean writer8(String formPath,String toPath){ try { BufferedInputStream bis=new BufferedInputStream(new FileInputStream(new File(formPath))); ByteArrayOutputStream baos=new ByteArrayOutputStream(); int c=bis.read();//读取bis流中的下一个字节 while(c!=-1){ baos.write(c); c=bis.read(); } bis.close(); byte byt[]=baos.toByteArray(); FileOutputStream out=new FileOutputStream(new File(toPath)); out.write(byt); out.close(); } catch (Exception e) { e.printStackTrace(); return false; } return true; } /** * 处理文件大小 */ public static String fileSize(long file) { if (file <= 0) { return ""; } else if (file < MB) { BigDecimal b = new BigDecimal((double) file / KB); return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "K"; } else if (file < GB) { BigDecimal b = new BigDecimal((double) file / MB); return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "M"; } else { BigDecimal b = new BigDecimal((double) file / GB); return b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + "G"; } } /** * 删除目录下的所有子目录和文件 * * @param filepath * @throws IOException */ protected static void deleteDirs(String filepath) throws IOException { File f = new File(filepath);// 定义文件路径 if (f.exists()) {// 判断是否存在 if (f.isDirectory()) {// 判断是文件还是目录 if (f.listFiles().length == 0) {// 若目录下没有文件则直接删除 f.delete();// 删除目录 } else {// 若有则把文件放进数组,并判断是否有下级目录 File delFile[] = f.listFiles(); int i = f.listFiles().length; for (int j = 0; j < i; j++) { if (delFile[j].isDirectory()) { deleteDirs(delFile[j].getAbsolutePath());// 递归调用delDirs方法并取得子目录路径 } else { delFile[j].delete();// 删除文件 } } f.delete();// 删除目录 } } else { f.delete();// 删除文件 } } } /** * 直接过滤图片所有格式的 * 重命名文件里面的名称 * @param fileDir * @param sequenceCode */ public static void fileRename(File fileDir, String sequenceCode) { File[] files = fileDir.listFiles(); for (int k = 0; k < files.length; k++) { StringBuffer sb = new StringBuffer(sequenceCode); if (files[k].isDirectory()) { fileRename(files[k], sequenceCode); } else { if (k < 10) sb.append("_000").append(k); else if (k >= 10 && k < 100) sb.append("_00").append(k); else if (k < 1000 && k >= 100) sb.append("_0").append(k); else sb.append("_").append(k); final int index = files[k].getName().lastIndexOf(".") + 1; final String fileType = files[k].getName().substring(index); sb.append(".").append(fileType); final String name = sb.toString(); final File dirFile = new File(fileDir, name); System.out.println("Rename File Path:" + files[k].getAbsolutePath()); files[k].renameTo(dirFile); } } } /** * 根据后缀条件过滤 * * @param fileDir * @param sequenceCode */ public static void fileFilterRenames(File fileDir, String sequenceCode) { File[] files = fileDir.listFiles(fileFilter); for (int k = 0; k < files.length; k++) { StringBuffer sb = new StringBuffer(sequenceCode); if (files[k].isDirectory()) { fileRename(files[k], sequenceCode); } else { if (k < 10) sb.append("_000").append(k); else if (k >= 10 && k < 100) sb.append("_00").append(k); else if (k < 1000 && k >= 100) sb.append("_0").append(k); else sb.append("_").append(k); final int index = files[k].getName().lastIndexOf(".") + 1; final String fileType = files[k].getName().substring(index); sb.append(".").append(fileType); final String name = sb.toString(); final File dirFile = new File(fileDir, name); System.out.println("重命名的文件: :" + files[k].getAbsolutePath()); files[k].renameTo(dirFile); } } } /** * 读取文件里面的所有文件 * * @param filePath */ public static void fileList(String filePath) { File srcFile = new File(filePath); boolean flag = srcFile.exists(); /** * @flag判断文件是否存在 * @isDirectory测试此抽象路径名表示的文件是否是一个目录 * @canWrite测试应用程序是否可以读取此抽象路径名表示的文件 */ if (!flag || !srcFile.isDirectory() || !srcFile.canRead()) { try { srcFile.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } else { File[] file = srcFile.listFiles(); for (int i = 0; i < file.length; i++) { System.out.println(file[i].getAbsolutePath()); } } } /** * 以数组形式列出所有文件包括子类的文件 * * @param path */ public static List findFiles(String path) { try { File file = new File(path); File[] files = file.listFiles(); String[] filenames = file.list(); if (filenames == null) return fileList; for (int i = 0; i < filenames.length; i++) { if (files[i].isFile()) { fileList.add(files[i].getPath()); } else if (files[i].isDirectory()) { findFiles(files[i].getPath()); } } } catch (Exception e) { e.printStackTrace(); } return fileList; } /** * 压缩文件 * @param path压缩文件的路径 * @param toDir指定压缩文件到那个目录磁盘下 */ public static void compressionZip(String path,String toDir) { File f = new File(path); try { InputStream fis = new FileInputStream(f); InputStream bis = new BufferedInputStream(fis); byte[] buf = new byte[1024*1024]; int index= f.getName().lastIndexOf("\\")+1; String fileName=f.getName().substring(index); if(fileName.matches(".*\\.(?i)gif")){ //匹配gif格式文件 fileName=fileName.replaceAll(".gif", ""); }else if(fileName.matches(".*\\.(?i)java")){//匹配.java文件 fileName=fileName.replaceAll(".java", ""); } int len; OutputStream fos = new FileOutputStream(toDir+ fileName + ".zip"); BufferedOutputStream bos = new BufferedOutputStream(fos); ZipOutputStream zos = new ZipOutputStream(bos);// 压缩包 ZipEntry ze = new ZipEntry(f.getName());// 这是压缩包名里的文件名 zos.putNextEntry(ze);// 写入新的 ZIP 文件条目并将流定位到条目数据的开始处 while ((len = bis.read(buf)) != -1) { zos.write(buf, 0, len); zos.flush(); } bis.close(); zos.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 解压zip文件 * @param path解压文件路径 * @param flag标识 */ public static void decompressZip(String path,boolean flag) { int count = -1; int index = -1; String savePath = ""; savePath = path.substring(0, path.lastIndexOf("\\")) + "\\"; try { BufferedOutputStream bos = null; ZipEntry entry = null; InputStream fis = new FileInputStream(path); ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis)); while((entry = zis.getNextEntry()) != null) { byte data[] = new byte[byt]; String temp = entry.getName(); flag = fileFilter(temp); if(!flag) continue; index = temp.lastIndexOf("/"); if(index > -1) temp = temp.substring(index+1); temp=savePath+temp; File f = new File(temp); if(!f.exists()){//不存在就创建一个新的文件 f.createNewFile(); //f.mkdirs(); } OutputStream fos = new FileOutputStream(f); bos = new BufferedOutputStream(fos, byt); while((count = zis.read(data, 0, byt)) != -1) { bos.write(data, 0, count); } bos.flush();//flush缓冲区 fos.flush(); bos.close(); fos.close(); fis.close(); } zis.close(); } catch (Exception e) { e.printStackTrace(); } } /** * * @param zip压缩文件文件路径 * @param zipName压缩文件里面的文件名称 * @param srcFiles多文件 * @throws IOException */ public static void ZipFiles(File zip, String zipName, File... srcFiles) throws IOException { ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zip)); ZipFiles(out, zipName, srcFiles); out.close(); System.out.println("*****************压缩完毕*******************"); } /** * 压缩文件-File * * @param zipFile * zip文件 * @param srcFiles * 被压缩源文件 * @author isea533 */ public static void ZipFiles(ZipOutputStream zis, String path, File... srcFiles) { path = path.replaceAll("\\*", "/"); if (!path.endsWith("/")) { path += "/"; } byte[] buf = new byte[1024]; try { for (int i = 0; i < srcFiles.length; i++) { if (srcFiles[i].isDirectory()) { File[] files = srcFiles[i].listFiles(); String srcPath = srcFiles[i].getName(); srcPath = srcPath.replaceAll("\\*", "/"); if (!srcPath.endsWith("/")) { srcPath += "/"; } zis.putNextEntry(new ZipEntry(path + srcPath)); ZipFiles(zis, path + srcPath, files); } else { FileInputStream in = new FileInputStream(srcFiles[i]); System.out.println(path + srcFiles[i].getName()); zis.putNextEntry(new ZipEntry(path + srcFiles[i].getName())); int len; while ((len = in.read(buf)) > 0) { zis.write(buf, 0, len); } zis.closeEntry(); in.close(); } } } catch (Exception e) { e.printStackTrace(); } } /** * * @param zipPath * @param descDir * @throws IOException */ public static void unZipFiles(String zipPath, String descDir) throws IOException { unZipFiles(new File(zipPath), descDir); } /** * 解压文件到指定目录 * @param zipFile * @param descDir * @author isea533 */ public static void unZipFiles(File zipFile, String descDir) throws IOException { File pathFile = new File(descDir); if (!pathFile.exists()) { pathFile.mkdirs(); } ZipFile zip = new ZipFile(zipFile); for (Enumeration entries = zip.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); String zipEntryName = entry.getName(); InputStream in = zip.getInputStream(entry); String outPath = (descDir + zipEntryName).replaceAll("\\*", "/"); // 判断路径是否存在,不存在则创建文件路径 File file = new File(outPath.substring(0, outPath.lastIndexOf('/'))); if (!file.exists()) { file.mkdirs(); } // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压 if (new File(outPath).isDirectory()) { continue; } // 输出文件路径信息 System.out.println(outPath); OutputStream out = new FileOutputStream(outPath); byte[] buf1 = new byte[1024]; int len; while ((len = in.read(buf1)) > 0) { out.write(buf1, 0, len); } in.close(); out.close(); } System.out.println("******************解压完毕********************"); } /** * 以数组形式存放图片,只拿jpg格式的 * * @param dirName * @return */ public static List readDirFiles(String dirName) {// dirName目录全路径 try { File file = new File(dirName); File[] files = file.listFiles(); for (int i = 0; i < files.length; i++) { File tempFile = files[i]; if (tempFile.isDirectory()) { String path = tempFile.getPath(); List list = readDirFiles(path);// 递归 for (int j = 0; j < list.size(); j++) { fileList.add(list.get(j)); } } else { String fileName = tempFile.getName(); if (!tempFile.isFile()) { continue; } // 判断是jpg格式的就添加进去 if (files[i].getName().endsWith(".gif")) { fileList.add(tempFile); } } if (i == (files.length - 1)) { return fileList; } } } catch (Exception e) { e.printStackTrace(); } return fileList; } /** * 创建文件并且拷贝文件 * * @param dirFrom从那个文件里面拷贝 * @param dirTo拷贝到那个文件里面 */ public static void copyFiles(File dirFrom, File dirTo) { File[] files = dirFrom.listFiles(); for (File f : files) { String tempFrom = f.getAbsolutePath(); String tempTo = tempFrom.replace(dirFrom.getAbsolutePath(), dirTo .getAbsolutePath()); // 后面的路径 替换前面的路径名 if (f.isDirectory()) { tempFile = new File(tempTo); if (!tempFile.exists()) { tempFile.mkdirs(); } else { copyFiles(dirFrom, dirTo); } } else { System.out.println("源文件:" + f.getAbsolutePath()); int index = tempTo.lastIndexOf("\\");// 找到"/"所在的位置 String mkdirPath = tempTo.substring(0, index); tempFile = new File(mkdirPath); if (!tempFile.exists()) { tempFile.mkdirs(); } else { System.out.println("目标点:" + tempTo); readFiles(tempFrom, tempTo); } } } } /** * 拷贝方法 * * @param from从那个路径 * @param to到那个路径 */ public static void readFiles(String from, String to) { try { InputStream in = new FileInputStream(from); OutputStream out = new FileOutputStream(to); byte[] buff = new byte[1024 * 1024]; int len = 0; while ((len = in.read(buff)) != -1) { out.write(buff, 0, len); } in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * xml格式化 * @param document * @param filePath * @return */ private static boolean format(Document document, String filePath) { boolean ret=true; OutputFormat format = OutputFormat.createPrettyPrint();// 格式 format.setEncoding("utf-8");// 设置格式编码 try { /** 将document中的内容写入文件中new XMLWriter(new FileWriter(new File(filename))); */ XMLWriter writer = new XMLWriter(new FileWriter(new File(filePath)),format); writer.write(document); writer.close(); } catch (IOException e) { e.printStackTrace(); ret=false; } return ret; } /** * 文件处理 * @param content * @param htmlPath * @return */ public static boolean write(String content, String htmlPath) { boolean flag = true; try { Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(htmlPath), "utf-8")); out.write("\n" + content); out.close(); } catch (Exception ex) { ex.printStackTrace(); return false; } return flag; } /** * 过滤器 */ private static FileFilter fileFilter = new FileFilter() { @Override public boolean accept(File fileName) { String f = fileName.getName(); if (f.endsWith(".png") || f.endsWith(".gif")) { return true; } return false; } }; /** * 文件过滤 * @param filName * @return */ public static boolean fileFilter(String filName) { if(filName.endsWith(".jpg") || filName.endsWith(".gif") || filName.endsWith(".bmp") || filName.endsWith(".png")) { return false; }else{ return true; } } /*** * ==============================================路径的封装begin========================================== */ /** * 获取当前工程路径 * * @return */ public static String getSysPath() { // String path = // Thread.currentThread().getContextClassLoader().getResource("").toString(); String path = Thread.currentThread().getContextClassLoader().getResource(".").toString(); String temp = path.replaceFirst("file:/", "").replaceFirst("WEB-INF/classes/", ""); String separator = System.getProperty("file.separator"); String resultPath = temp.replaceAll("/", separator + separator); return resultPath; } /** * Thread.currentThread().getContextClassLoader().getResource("") * 的getResource里面空串或者点或者是/输出的路径是一致 "" D:\Eclipse3.7\JTool\build\classes\ . * D:\Eclipse3.7\JTool\build\classes\ * -/ D:\Eclipse3.7\JTool\build\classes\ * * @return */ public static String getClassPath() { // String path = // Thread.currentThread().getContextClassLoader().getResource("").toString(); // String path // =Thread.currentThread().getContextClassLoader().getResource(".").toString(); String path = Thread.currentThread().getContextClassLoader().getResource(".").toString(); String temp = path.replaceFirst("file:/", ""); String separator = System.getProperty("file.separator"); String resultPath = temp.replaceAll("/", separator + separator); return resultPath; } /** * getClassLoader().getResource()方法参数空串和点都是输出相同的路径唯有 * /是报空指针 "" D:\Eclipse3.7\JTool\build\classes\ * . D:\Eclipse3.7\JTool\build\classes\ * -/ 报空指针 * * @return */ private String getClassesAbsolutePath() { // 得到的是 项目的绝对路径 String path = this.getClass().getClassLoader().getResource("") .getPath(); // String // path=this.getClass().getClassLoader().getResource(".").getPath(); // String // path=this.getClass().getClassLoader().getResource("/").getPath();//报空指针 String temp = path.replaceFirst("/", ""); String separator = System.getProperty("file.separator"); String resultPath = temp.replaceAll("/", separator + separator); return resultPath; } /** *得到的是当前类 文件的URI目录,不包括自己 * ""D:\Eclipse3.7\JTool\build\classes\javax\org\path\ . * D:\Eclipse3.7\JTool\build\classes\javax\org\path\ - / * D:\Eclipse3.7\JTool\build\classes\ * @return */ private String getCurrentClassPath() { // String path=this.getClass().getResource("").getPath(); // String path=this.getClass().getResource(".").getPath(); String path = this.getClass().getResource("/").getPath(); String temp = path.replaceFirst("/", ""); String separator = System.getProperty("file.separator"); String resultPath = temp.replaceAll("/", separator + separator); return resultPath; } public static String getPath(Class clazz) { String path = clazz.getResource("").getPath(); return new File(path).getAbsolutePath(); } public static String getPath(Object object) { String path = object.getClass().getResource("").getPath(); return new File(path).getAbsolutePath(); } public static String getRootClassPath() { if (rootClassPath == null) { try { String path = IOUtil.class.getClassLoader().getResource("").toURI().getPath(); rootClassPath = new File(path).getAbsolutePath(); } catch (Exception e) { String path = IOUtil.class.getClassLoader().getResource("").getPath(); rootClassPath = new File(path).getAbsolutePath(); } } return rootClassPath; } public static String getPackagePath(Object object) { Package p = object.getClass().getPackage(); return p != null ? p.getName().replaceAll("\\.", "/") : ""; } public static File getFileFromJar(String file) { throw new RuntimeException("Not finish. Do not use this method."); } public static String getWebRootPath() { if (webRootPath == null) webRootPath = detectWebRootPath(); return webRootPath; } public static void setWebRootPath(String webRootPath) { if (webRootPath == null) return ; if (webRootPath.endsWith(File.separator)) webRootPath = webRootPath.substring(0, webRootPath.length() - 1); IOUtil.webRootPath = webRootPath; } private static String detectWebRootPath() { try { String path = IOUtil.class.getResource("/").toURI().getPath(); return new File(path).getParentFile().getParentFile().getCanonicalPath(); } catch (Exception e) { throw new RuntimeException(e); } } /* private static String detectWebRootPath() { try { String path = IO.class.getResource("/").getFile(); return new File(path).getParentFile().getParentFile().getCanonicalPath(); } catch (IOException e) { throw new RuntimeException(e); } } /** * * ===========================================End============================================== */ /** * ==============================================begin============================ * 加密文件 * * @param f * @param path */ private static String encryptFile(File f, String path) { InputStream in = null; OutputStream out = null; String key = ""; try { f = new File(path); in = new FileInputStream(f); out = new ByteArrayOutputStream(); // System.out.println(f.getAbsolutePath()); // System.out.println(f.length()); encoder.encodeBuffer(in, out); key = out.toString(); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } return key; } /** *解密 * ByteArrayOutputStream * InputStream * OutputStream * FileInputStream * @param f * @param path */ private static String decryptFile(File f, String path) { InputStream in = null; OutputStream out = null; String key = ""; try { f = new File(path); in = new FileInputStream(f); out = new ByteArrayOutputStream(); decoder.decodeBuffer(in, out); key = out.toString(); in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } return key; } /** * 加密 * * @param key * @return * @throws Exception */ public static String encryptBASE64(String inputStr) { String value = ""; try { byte[] key = inputStr.getBytes(); value = encoder.encodeBuffer(key); } catch (Exception e) { e.printStackTrace(); } return value; } /** * 解密 * * @param key * @return * @throws Exception */ public static String decryptBASE64(String outputStr) { String value = ""; try { byte[] key = decoder.decodeBuffer(outputStr); value = new String(key); } catch (Exception e) { } return value; } /** * ================================end================================ */ public static void main(String[] args) throws Exception { String path1 = "D:/test/qq"; File fromFile = new File(path1);// 源文件夹 fileRename(fromFile, "I_"); /* String path1 = "C:\\QQ\\1030927759\\Image"; String path2 = "d:\\t\\b"; String path3 = "d:\\t\\c"; File fromFile = new File(path1);// 源文件夹 File toFile = new File(path2);// 目标 compressionZip(path2+"\\a.gif","D:\\t\\"); // copyFiles(fromFile, toFile); // fileFilterRenames(fromFile, "s"); //fileRename(fromFile, "f"); // deleteDirs(path1); // fileList(path2); * List list=readDirFiles(path2); for (Object object : list) { * System.out.println("image:"+object); } * * List list=findFiles(path2); for (Iterator iterator = list.iterator(); * iterator.hasNext();) { Object object = (Object) iterator.next(); * System.out.println(object); } // String key= encryptFile(fromFile, path1+"\\a.gif"); // System.err.println(key); // System.err.println(decryptBASE64(key)); // System.out.println("文件大小"+fileSize(fromFile.length())); String url = path1 + "\\AccessFile.java"; String inputStr = encryptBASE64(url); // System.err.println("BASE64加密后:\n" + inputStr); // System.err.println("BASE64解密后:\n" + decryptBASE64(inputStr)); boolean flag=false; //decompressZip("D:\\t\\e.zip",flag); *//** * 压缩文件 *//* File[] files = new File[]{new File("d:/t/a.txt"),new File("d:/t/a.xls")}; File zip = new File("d:/t/压缩.zip"); ZipFiles(zip,"ZipFiles",files); *//** * 解压文件 *//* File zipFile = new File("d:/t/压缩.zip"); String path = "d:/"; //unZipFiles(zipFile, path); */ //String separator=f.separator;//与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串 String path = "D:/test/a.txt"; //String text = reader1(path); //writer1(path, "D:/test/a1.txt"); //writer2(path, "D:/test/c.txt"); //writer3(path, "D:/test/d.txt"); //writer4(path, "D:/test/d.txt"); //char c=reader2(path); //System.out.print(c); //String text=reader2(path); //System.out.println(text); //writer5(path,"d:/test/f.txt"); //writer6(path,"d:/test/e.txt"); String s=reader3(path); System.out.println(s); } }
相关推荐
在Java中,输入输出流(简称IO流)是用来处理文件读写的核心技术之一。它主要包括两类:以字节为导向的流和以字符为导向的流。 ##### 1.1 以字节为导向的流 以字节为导向的流是指按照字节来进行数据读写的流,适用...
### Java IO流详解 #### 一、流的概述与分类 在Java中,流是一种抽象概念,用于描述数据从一个地方传输到另一个地方的过程。它主要用于处理数据输入和输出,例如从磁盘读取文件、向网络发送数据或从键盘接收用户...
### Java IO流学习笔记 #### 一、IO流的四大抽象类 - **InputStream/OutputStream**:字节的输入输出流的抽象类。 - 数据单位:字节(8比特,范围0-255) - **Reader/Writer**:字符的输入输出流的抽象类。 - ...
Java IO 流是Java平台中的核心概念,它用于在不同数据源之间进行数据传输,如文件、网络、内存等。Java IO系统提供了丰富的类库,使得开发者能够方便地处理输入和输出操作。在这个大总结中,我们将深入探讨Java IO流...
字节流包括InputStream和OutputStream,字符流包括Reader和Writer。字节流适用于处理所有类型的数据,包括文本和二进制数据,而字符流主要用于处理文本数据,能够更方便地处理字符编码。 3. 字符流的由来和使用场景...
InputStream和OutputStream是字节流,Reader和Writer是字符流。 字节流是最基本的流,文件的操作、网络数据的传输等等都依赖于字节流。字节流的API有InputStream和OutputStream两个类。InputStream的API包括read()...
在Java中,`java.io.File`类用于封装文件路径,可以用来创建文件对象,并且提供了许多与文件和目录交互的方法。 - 创建File对象: ```java File file = new File("a.txt"); ``` - 常用方法介绍: - `...
Java IO流按照数据传输的方向可以分为两类:输入流(InputStream)和输出流(OutputStream)。它们分别用于数据的读取和写入。进一步,根据处理的数据类型,流又可以分为字节流(Byte Stream)和字符流(Character ...
### 文档JAVA-IO流 #### 一、IO流概述 1. **什么是IO** - I代表**输入**(input),O代表**输出**(output)。在Java中,所有负责输入输出的类都位于`java.io`包内。这些类主要用于进行数据的输入输出操作。 2. **流...
- Java IO中的流类通常继承自四个基础抽象类:InputStream/Reader(输入流基类)、OutputStream/Writer(输出流基类)。这两个对分别处理字节和字符。它们提供了控制流的基本方法,比如读写数据、跳过指定数量的...
Java将所有IO操作封装为流对象,分为四大类:字节流(Byte Stream)和字符流(Character Stream),以及输入流(InputStream/Reader)和输出流(OutputStream/Writer)。字节流处理单字节数据,字符流处理Unicode...
### Java IO流技术详解 #### 一、IO流概述 在Java编程中,**IO流**(Input/Output Stream)主要用于实现数据的输入与输出操作。无论是文件读写、网络通信还是程序间的交互,几乎所有的数据交换都依赖于IO流。 #####...
### Java IO(下): 过滤流(处理流)、缓冲流详解 #### 一、过滤流(处理流)概述 **过滤流**(处理流)是在Java IO系统中的一个重要概念,它建立在已有的流对象之上,并为其添加了额外的功能或性能优化。与节点流...
在这个项目中,主要使用了字节流(InputStream和OutputStream)和字符流(Reader和Writer)来实现文件与内存之间的数据传输。字节流处理二进制数据,而字符流处理文本数据。在这里,我们可能会用到FileReader、...
字节流以字节为单位进行数据传输,主要由InputStream和OutputStream家族构成,例如FileInputStream和FileOutputStream用于文件操作,ByteArrayInputStream和ByteArrayOutputStream则用于内存中的字节数组。字符流以...
在这个“个人写的JavaIO工具”中,我们可以看到作者创建了一个自定义的Io工具集,包含了对文件读写、数据流操作、以及代码统计和注释处理的功能。下面将详细讨论这些知识点。 1. **Java IO基础**: - Java IO API...
- **Reader 与 Writer**:与 `InputStream` 和 `OutputStream` 相对应的是 `Reader` 和 `Writer`,它们处理的是字符流,而不是字节流。这对于处理文本文件非常有用,因为文本文件通常由字符组成。 #### 四、...
Java.io包提供了多种流类型来实现不同类型的输入和输出功能。 流可以按照其方向分为两类:输入流和输出流。输入流主要用于从源头读取数据,而输出流则用于向目标写入数据。例如,InputStream和OutputStream是处理...
这两类流又各自包含输入流和输出流,形成了四类抽象基类:`InputStream`、`OutputStream`、`Reader`和`Writer`。 #### 字节流和字符流的区别 - **字节流**:处理二进制数据,每次传输8位字节。 - **字符流**:处理...