Java file read/write operations

Source: Internet
Author: User

1. Obtain information entered by the console user

/*** // ** Obtain information entered by the console user <br/> * @ return <br/> * @ throws ioexception <br/> */<br/> Public string getinputmessage () throws ioexception... {<br/> system. out. println ("Enter your command:"); <br/> byte buffer [] = new byte [1024]; <br/> int COUNT = system. in. read (buffer); <br/> char [] CH = new char [count-2]; // the last two digits are the Terminator, delete not <br/> for (INT I = 0; I <count-2; I ++) <br/> CH = (char) buffer; <br/> string STR = new string (CH); <br/> return STR; <br/>}< br/> 

The user input information can be returned. The disadvantage is that Chinese input is not supported and further improvement is needed.

 

Ii. Copy an object

1. copy a file as a file stream

/***** // Copy the file as a file stream <br/> * @ Param SRC file source directory <br/> * @ Param DEST file destination directory <br/> * @ throws ioexception <br/> */<br/> Public void copyfile (string SRC, string DEST) throws ioexception... {<br/> fileinputstream in = new fileinputstream (SRC); <br/> file = new file (DEST); <br/> If (! File. exists () <br/> file. createnewfile (); <br/> fileoutputstream out = new fileoutputstream (File); <br/> int C; <br/> byte buffer [] = new byte [1024]; <br/> while (C = in. read (buffer ))! =-1 )... {<br/> for (INT I = 0; I <C; I ++) <br/> out. write (buffer); <br/>}< br/> in. close (); <br/> out. close (); <br/>}< br/> 

This method is tested and supports Chinese processing and can be copied to multiple formats, such as txt, XML, JPG, and Doc.

 

3. Write files
1. Use printstream to write files

/*** // *** <Br/> * file output example <br/> */<br/> Public void printstreamdemo ()... {<br/> try... {<br/> fileoutputstream out = new fileoutputstream ("D:/test.txt"); <br/> printstream P = new printstream (out ); <br/> for (INT I = 0; I <10; I ++) <br/> P. println ("this is" + I + "line"); <br/>} catch (filenotfoundexception e )... {<br/> E. printstacktrace (); <br/>}< br/> 2. use stringbuffer to write a file <br/> Public void stringbu Fferdemo () throws ioexception... {<br/> file = new file ("/root/SMS. log"); <br/> If (! File. exists () <br/> file. createnewfile (); <br/> fileoutputstream out = new fileoutputstream (file, true); <br/> for (INT I = 0; I <10000; I ++ )...... {<br/> stringbuffer sb = new stringbuffer (); <br/> Sb. append ("This is the first line" + I + ": the methods described earlier are not used. Why is it always a strange problem?"); <br/> out. write (sb. tostring (). getbytes ("UTF-8"); <br/>}< br/> out. close (); <br/>}< br/> 

This method can be used to set the encoding to effectively solve Chinese problems.

 

Iv. File rename

/***** Rename the file <br/> * @ Param path file directory <br/> * @ Param oldname original file name <br/> * @ Param newname new file name <br/> */<br/> Public void renamefile (string path, string oldname, string newname )... {<br/> If (! Oldname. equals (newname ))... {// It is necessary to rename the new and previous file names. <br/> file oldfile = new file (path + "/" + oldname ); <br/> file newfile = new file (path + "/" + newname); <br/> If (newfile. exists () // if there is already a file in the directory with the same name as the new file, renaming is not allowed <br/> system. out. println (newname + "already exists! "); <Br/> else... {<br/> oldfile. renameto (newfile); <br/>}< br/> 

 

5. Transfer file directory
The transfer file directory is not the same as the copy file. The copy file exists in both directories after the copy, but the transfer file directory exists only in the new directory after the transfer.

/*** // ** Transfer file directory <br/> * @ Param filename file name <br/> * @ Param oldpath old directory <br/> * @ Param newpath new directory <br/> * @ Param cover if a file with the same file name exists in the new directory and is transferred, whether to overwrite the files in the new directory. "cover = true" will overwrite the original file; otherwise, the operation is not performed. <br/> */<br/> Public void changedirectory (string filename, string oldpath, string newpath, Boolean cover )... {<br/> If (! Oldpath. equals (newpath ))... {<br/> file oldfile = new file (oldpath + "/" + filename); <br/> file newfile = new file (newpath + "/" + filename ); <br/> If (newfile. exists ())... {// if the file to be transferred exists in the directory to be transferred, <br/> If (cover) // overwrite <br/> oldfile. renameto (newfile); <br/> else <br/> system. out. println ("already exists in the new directory:" + filename); <br/>}< br/> else... {<br/> oldfile. renameto (newfile); <br/>}< br/> 

6. Read files
1. Use fileinputstream to read files

/*** // ** Read the file <br/> * @ Param path <br/> * @ return <br/> * @ throws ioexception <br/> */< br/> Public String fileinputstreamdemo (string path) throws ioexception... {<br/> file = new file (PATH); <br/> If (! File. exists () | file. isdirectory () <br/> throw new filenotfoundexception (); <br/> fileinputstream FCM = new fileinputstream (File ); <br/> byte [] Buf = new byte [1024]; <br/> stringbuffer sb = new stringbuffer (); <br/> while (FS. read (BUF ))! =-1 )... {<br/> Sb. append (new string (BUF); <br/> Buf = new byte [1024]; // regenerate, avoid duplication with the data read last time <br/>}< br/> return sb. tostring (); <br/>}< br/>

 

2. Read data using bufferedreader
In Io operations, bufferedreader and bufferedwriter are more efficient.

/*** // ** Read the file <br/> * @ Param path <br/> * @ return <br/> * @ throws ioexception <br/> */< br/> Public String bufferedreaderdemo (string path) throws ioexception... {<br/> file = new file (PATH); <br/> If (! File. exists () | file. isdirectory () <br/> throw new filenotfoundexception (); <br/> bufferedreader BR = new bufferedreader (New filereader (File); <br/> string temp = NULL; <br/> stringbuffer sb = new stringbuffer (); <br/> temp = BR. readline (); <br/> while (temp! = NULL )... {<br/> Sb. append (temp + ""); <br/> temp = BR. readline (); <br/>}< br/> return sb. tostring (); <br/>}< br/>

 

3. Use dom4j to read XML files
/*** // ** Read the XML file from the directory <br/> * @ Param path file directory <br/> * @ return <br/> * @ throws documentexception <br/> * @ throws ioexception <br/> */<br/> public document readxml (string path) throws disable entexception, ioexception... {<br/> file = new file (PATH); <br/> bufferedreader = new bufferedreader (New filereader (File )); <br/> saxreader = new saxreader (); <br/> document = (document) saxreader. read (bufferedreader); <br/> bufferedreader. close (); <br/> return document; <br/>}< br/>

 

7. Create a file (folder)

1. Create a folder/*** // ** create a folder
/** @ Param path directory <br/> */<br/> Public void createdir (string path )... {<br/> file dir = new file (PATH); <br/> If (! Dir. exists () <br/> dir. mkdir (); <br/>}< br/>

 

2. Create a new file/*** // ** create a new file
/** @ Param path directory <br/> * @ Param filename file name <br/> */<br/> Public void delfile (string path, string filename )... {<br/> file = new file (path + "/" + filename); <br/> If (file. exists () & file. isfile () <br/> file. delete (); <br/>}< br/>

8. delete files (directories)
1. delete the file/***** // ** delete the file

/** @ Param path directory <br/> * @ Param filename file name <br/> */<br/> Public void delfile (string path, string filename )... {<br/> file = new file (path + "/" + filename); <br/> If (file. exists () & file. isfile () <br/> file. delete (); <br/>}< br/>

 

2. delete a directory
To delete a directory using the delete () method of the file class, you must ensure that there are no files or subdirectories in the directory; otherwise, the deletion fails. Therefore, in actual applications, we need to delete the directory, you must recursively Delete All subdirectories and files in the directory before deleting the directory. /***** // Recursively delete a folder

/** @ Param path <br/> */<br/> Public void deldir (string path )... {<br/> file dir = new file (PATH); <br/> If (dir. exists ())... {<br/> file [] TMP = dir. listfiles (); <br/> for (INT I = 0; I <TMP. length; I ++ )... {<br/> If (TMP. isdirectory ())... {<br/> deldir (path + "/" + TMP. getname (); <br/>}< br/> else... {<br/> TMP. delete (); <br/>}< br/> dir. delete (); <br/>}< br/>

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.