Created
September 11, 2014 09:34
-
-
Save winse/65db65f076b87885ff71 to your computer and use it in GitHub Desktop.
io read/write
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import static org.junit.Assert.assertEquals; | |
| import java.io.BufferedReader; | |
| import java.io.BufferedWriter; | |
| import java.io.ByteArrayInputStream; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.FileInputStream; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileOutputStream; | |
| import java.io.FileReader; | |
| import java.io.FileWriter; | |
| import java.io.IOException; | |
| import java.util.zip.GZIPInputStream; | |
| import java.util.zip.GZIPOutputStream; | |
| import org.apache.commons.io.IOUtils; | |
| import org.junit.Test; | |
| public class GZipFileWriteTest { | |
| @Test | |
| public void testWriteGZip() throws FileNotFoundException, IOException { | |
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
| FileInputStream in = new FileInputStream("e:/tmp/filter.io"); | |
| IOUtils.copy(in, out); | |
| in.close(); | |
| FileOutputStream fileOut = new FileOutputStream("e:/tmp/gzip.filter.io"); | |
| GZIPOutputStream gzipout = new GZIPOutputStream(fileOut); | |
| IOUtils.write(out.toByteArray(), gzipout); | |
| gzipout.finish(); // 可以不写,close会调用finish | |
| gzipout.flush(); | |
| gzipout.close();// 必须 http://snowolf.iteye.com/blog/643010 | |
| fileOut.close(); | |
| } | |
| @Test | |
| public void testReadGZip() throws FileNotFoundException, IOException { | |
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
| FileInputStream fileIn = new FileInputStream("e:/tmp/gzip.filter.io"); | |
| GZIPInputStream gzipIn = new GZIPInputStream(fileIn); | |
| IOUtils.copy(gzipIn, out); | |
| gzipIn.close(); | |
| fileIn.close(); | |
| FileOutputStream fileOut = new FileOutputStream("e:/tmp/filter.io2"); | |
| IOUtils.copy(new ByteArrayInputStream(out.toByteArray()), fileOut); | |
| fileOut.close(); | |
| } | |
| @Test | |
| public void testOpenFile() throws IOException{ | |
| String file = "e:/tmp/open-filter.io"; | |
| BufferedWriter writer = new BufferedWriter(new FileWriter(file)); | |
| writer.write("abc"); | |
| writer.close(); | |
| BufferedReader reader = new BufferedReader(new FileReader(file)); | |
| assertEquals("abc", reader.readLine()); | |
| reader.close(); | |
| FileOutputStream fileOut = new FileOutputStream(file); | |
| fileOut.close(); | |
| BufferedReader readerEmpty = new BufferedReader(new FileReader(file)); | |
| assertEquals("", readerEmpty.readLine()); | |
| readerEmpty.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment