This article describes the Java file read and write and compression implementation method, the specific code is as follows:
Package Com.toone.iform.action.common;
Import Java.io.BufferedReader;
Import Java.io.BufferedWriter;
Import Java.io.File;
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.io.InputStream;
Import java.util.Enumeration;
Import Java.util.Random;
Import Java.util.zip.ZipEntry;
Import java.util.zip.ZipException;
Import Java.util.zip.ZipFile;
public class Testfileio {static String s = file.separator;
The private static void Testinput () {//D disk has a Welcome.java file and is now read in bytes: int a = 0;
int counter=0;
FileInputStream F11;
The input stream try {f11 = new FileInputStream ("D:" + S + "Welcome.java"); while ((A = F11.read ())!=-1) System.out.print ((char) a);
Here is the byte output, the Chinese character does not output normally, because one of the Chinese characters characters two bytes.
System.out.println ("\ n \--------------------------------------------------\ n"); FileReader f12 = new FileReader ("D:" + S + "Welcome.java"); while ((A = F12.read ())!=-1) System.out.print ((char) a);//Here is the character output, the Chinese characters can output normally System.out.println (
"\ n \--------------------------------------------------\ n"); F11.close ()///After reading, close the file F12.close ()//After reading, close file} catch (FileNotFoundException e) {//TODO
Auto-generated Catch block E.printstacktrace ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
There is a Welcome.java file under private static void Testoutput () {//D disk, now read in bytes: int a = 0;
Output stream File F21 = new file ("D:" + S + "testfile" + S + "test1.txt");
Define a new file F21, and then determine if it exists in this directory, and if not, create it.
if (!f21.exists ()) {F21.getparentfile (). Mkdirs ();
try {f21.createnewfile ();
Copy the contents of "Welcome.java" to f21 fileoutputstream fos = new FileOutputStream (F21); FileiNputstream fis = new FileInputStream ("D:" + S + "Welcome.java");/read "Welcome.java" file while (A = Fis.rea D ())!=-1) fos.write (a);//writes the read memory into the FOS, and now the test1.txt is the copy Welcome.java//writer class File
Writer f22 = new FileWriter ("D:" + S + "testfile" + S + "test2.txt"); for (int i = 0; i < 65535 i++) F22.write (i);//write to Test2.txt.
It can also be seen from here that the above 35-38 lines to determine whether a file exists or not.
Writes a string to the file FileWriter f23 = new FileWriter ("D:" + S + "testfile" + S + "test3.txt");
F23.write ("Hello, world!");
Fos.close ();
Fis.close ();
F22.close ();
F23.close ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
There is a Welcome.java file under private static void Testbufferring () {//D disk, now read in bytes: int a = 0, counter = 0; Buffer characters to enable efficient write//BufferedWriter f31=new BufferedWriter (Newfilewriter ("D" +s+ tes)Tfile "+s+" "Test4.txt"));
BufferedWriter f31;
try {f31 = new BufferedWriter (New FileWriter ("D:" + S + "testfile" + S + "test4.txt"));
for (int i = 1; I <= i++) {f31.write (string.valueof (New Random (). Nextint (100)) + "");
if (i% = 0) f31.newline ();
} f31.flush ();//Refresh buffer f31.close ();
BufferedReader F32 = new BufferedReader (New FileReader ("D:" + S + "testfile" + S + "test4.txt"));
String S32;
System.out.println ("Content of output file F32:");
while ((S32 = F32.readline ())!= null) System.out.println (S32);
F32.close ();
System.out.println ("\ n--------------------------------------------------\ n");
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
The private static void Testzip () {try {file F1 = new file ("D:/test.zip");
File F2 = new file ("D:/testfile/testzip"); ZipFile Zf = new ZipFile (F1);
Testziptounzip (ZF, F2);
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); ZipFile the compressed package to the public static void Testziptounzip in file (ZipFile zipfile, file file) {ZipEntry zentry =
Null
File zipout;
InputStream zis = null;
FileOutputStream fos = null; Enumeration E = Zipfile.entries ();//ZipFile directory while (E.hasmoreelements ()) {zentry = (zipentry) e.nextelement (
); System.out.println (Zentry.getname ());//What are the files under ZipFile?
But why not output in order??
Put the extracted files under the file folder: zipout = new File (file + S + zentry.getname ());
if (!zentry.isdirectory ()) {try {ZiS = Zipfile.getinputstream (zentry);
if (!zipout.exists ()) Zipout.getparentfile (). Mkdirs ();
FOS = new FileOutputStream (zipout);
Byte[] B = new byte[1024];
int length; while (length = Zis.read (b)) > 0) {fos. Write (b, 0, length);
} fos.close ();
Zis.close ();
catch (Zipexception E1) {//TODO auto-generated catch block E1.printstacktrace ();
catch (IOException E1) {//TODO auto-generated catch block E1.printstacktrace (); }}} public static void Main (string[] args) throws Zipexception {//TODO auto-generated method s
Tub Testinput ();
Testoutput ();
Testbufferring ();
Testzip (); }
}