Java I/O 文件加锁,压缩
文件加锁:
文件加锁机制允许我们同步访问某个作为共享资源的文件。
public class Test { public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("test.txt"); /* * 通过tryLock 或者 lock 获得整个文件的FileLock * tryLock(position, size, shared) 对文件进行部分加锁 */ FileLock fileLock = fos.getChannel().tryLock(); if (fileLock != null) { System.out.println("Locked file..."); try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } fileLock.release(); System.out.println("Realased Lock ..."); } } }
文件压缩:
GZIP进行简单的压缩解压:
public class Test { public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader(new FileReader("/home/estar/Test/a.java")); BufferedOutputStream bos = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream("test.gz",true))); System.out.println("GZIP 压缩写入文件 。。。"); String s; while ((s = in.readLine()) != null) { s += "\n"; bos.write(s.getBytes()); } in.close(); bos.close(); //InputStreamReader 起到了在字节流与字符流转换的桥梁。。。 System.out.println("GZIP 解压读取文件。。。。"); BufferedReader br = new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream("test.gz")))); while ((s = br.readLine()) != null) { System.out.println(s); } } } 输出: GZIP 压缩写入文件 。。。 GZIP 解压读取文件。。。。 AAHJKHJKAAHJKHJKAAHJKHJKnixingAAHJKHJKAAHJKHJKAAHJKHJKnixingAAHJKHJKAAHJKHJK AAHJKHJKnixing AAHJKHJKAAHJKHJK AAHJKHJKnixing AAHJKHJKAAHJKHJK AAHJKHJKnixing AAHJKHJKAAHJKHJK AAHJKHJKnixing
ZIP进行多文件保存:
public class Test { //这里保存要压缩的文件路径 public static String[] path = {"/home/estar/Test/a.java","/home/estar/Test/b.java","/home/estar/cbian/yyy/main.cpp"}; public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("test.zip"); CheckedOutputStream cos = new CheckedOutputStream(fos, new Adler32()); ZipOutputStream zos = new ZipOutputStream(cos); BufferedOutputStream bos = new BufferedOutputStream(zos); for (String p : path) { //通过ZipEntry 将要压缩的文件的写入压缩文件test.zip zos.putNextEntry(new ZipEntry(p)); //读出每个要压缩的文件的信息, BufferedReader br = new BufferedReader(new FileReader(p)); String s; while ((s = br.readLine()) != null) { System.out.println("from " + p + "content : " + s); s += "\n"; bos.write(s.getBytes()); } br.close(); bos.flush(); } bos.close(); System.out.println("Checksum : " + cos.getChecksum().getValue()); //读压缩文件 System.out.println("Reading file"); FileInputStream fis = new FileInputStream("test.zip"); CheckedInputStream cis = new CheckedInputStream(fis, new Adler32()); ZipInputStream zis = new ZipInputStream(cis); BufferedReader br = new BufferedReader(new InputStreamReader(zis)); ZipEntry ze; while ((ze = zis.getNextEntry()) != null) { System.out.println("reading ze : " + ze); String s; while ((s = br.readLine()) != null) { System.out.println(s); } } System.out.println("Checksum : " + cis.getChecksum().getValue()); ZipFile zf = new ZipFile("test.zip"); Enumeration e = zf.entries(); while (e.hasMoreElements()){ ZipEntry ze1 = (ZipEntry)e.nextElement(); System.out.println("ze1 : " + ze1); } } }
Java编程思想P566