Home>

Directly on the code,There are detailed notes,There are illustrations, I believe you understand!

package day14;
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;
private static void testinput () {
//There is a welcome.java file under disk d, now read in byte:
int a=0;
//int counter=0;
fileinputstream f11;
//input stream
try {
f11=new fileinputstream ("d:" + s + "welcome.java");
while ((a=f11.read ())!=-1)
system.out.print ((char) a);//Here is the output in bytes,Chinese characters cannot be output normally,Because a Chinese character is two bytes.
system.out
.println ("\ n \ n ------------------------------------------ -------- \ n ");
filereader f12=new filereader ("d:" + s + "welcome.java");
while ((a=f12.read ())!=-1)
system.out.print ((char) a);//Here is the output by character,Chinese characters can be output normally
system.out
.println ("\ n \ n ------------------------------------------ -------- \ n ");
f11.close ();//Close the file after reading
f12.close ();//Close the file after reading
} catch (filenotfoundexception e) {
//todo auto-generated catch block
e.printstacktrace ();
} catch (ioexception e) {
//todo auto-generated catch block
e.printstacktrace ();
}
}
private static void testoutput () {
//There is a welcome.java file under disk d, now read in byte:
int a=0;
//output stream
file f21=new file ("d:" + s + "testfile" + s + "test1.txt");//define a new file f21, and then determine whether it exists in this directory,If it doesn't exist,Create it.
if (! f21.exists ()) {
f21.getparentfile (). mkdirs ();
try {
f21.createnewfile ();
//Copy the content of "welcome.java" to f21
fileoutputstream fos=new fileoutputstream (f21);
fileinputstream fis=new fileinputstream ("d:" + s
+ "welcome.java");//read in the "welcome.java" file
while ((a=fis.read ())!=-1)
fos.write (a);//Write the read memory to fos, and now get test1. txt is copy welcome. java
//writer class
filewriter f22=new filewriter ("d:" + s + "testfile" + s
+ "test2.txt");
for (int i=0;i<65535;i ++)
f22.write (i);//will
//Write to test2.txt. It can also be seen from here,The statement on line 35-38 above to judge the existence of the file is also unnecessary.
//write string to 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 ();
}
}
}
private static void testbufferring () {
//There is a welcome.java file under disk d, now read in byte:
int a=0, counter=0;
//buffer characters,Achieve efficient writing
//bufferedwriter f31=new bufferedwriter (new
//filewriter ("d" + s + "testfile" + s + "test4.txt"));
bufferedwriter f31;
try {
f31=new bufferedwriter (new filewriter ("d:" + s + "testfile" + s
+ "test4.txt"));
for (int i=1;i<= 100;i ++) {
f31.write (string.valueof (new random (). nextint (100)) + "");
if (i%10 == 0)
f31.newline ();
}
f31.flush ();//flush the buffer
f31.close ();
bufferedreader f32=new bufferedreader (new filereader ("d:" + s
+ "testfile" + s + "test4.txt"));
string s32;
system.out.println ("Contents 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 ();
}
}
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 ();
}
}
//Extract the compressed zipfile into file
public static void testziptounzip (zipfile zipfile, file file) {
zipentry zentry=null;
file zipout;
inputstream zis=null;
fileoutputstream fos=null;
enumeration e=zipfile.entries ();//directory of zipfile
while (e.hasmoreelements ()) {
zentry=(zipentry) e.nextelement ();
system.out.println (zentry.getname ());//What are the files under zipfile?But why not output in order??
//Place the decompressed files in 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 stub
testinput ();
testoutput ();
testbufferring ();
testzip ();
}
}
  • Previous Go's method receiver type is value type or pointer type?
  • Next Detailed java NIO
  • Trends