Java for file read and write operation detailed _java

Source: Internet
Author: User

Directly on the code, there are detailed comments, there are illustrations, I believe you understand!

Copy Code code as follows:

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 D disk, now read by 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 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 be output normally
System.out
. println ("\ n \--------------------------------------------------\ n");

F11.close () to close the file after you finish reading
F12.close () to close the file after you finish 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 D disk, now read by byte:
int a = 0;
Output stream
The file F21 = new file ("D:" + S + "testfile" + S + "Test1.txt"),//define the F21 of the document, 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.read ())!=-1)
Fos.write (a);//writes the read memory into the FOS, the test1 is now obtained. TXT is copy welcome. of Java

Writer class
FileWriter f22 = new FileWriter ("D:" + S + "testfile" + S
+ "Test2.txt");
for (int i = 0; i < 65535; i++)
F22.write (i)//Will
Written to the Test2.txt. It can also be seen from here that the above 35-38 lines to determine whether a file exists or not.
Writing a string to a 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 D disk, now read by byte:
int a = 0, counter = 0;
Buffer characters for 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 <= i++) {
F31.write (string.valueof (New Random (). Nextint (100)) + "");
if (i% 10 = 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 ();
}
}

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 package zipfile into file
public static void Testziptounzip (ZipFile zipfile, file file) {
ZipEntry zentry = null;
File zipout;
InputStream zis = null;
FileOutputStream fos = null;
Directory of Enumeration E = Zipfile.entries ();//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 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 Stub

Testinput ();
Testoutput ();
Testbufferring ();
Testzip ();
}
}






Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.