Java file operations are very common in system development, such as file upload and download, Excel file data import and export, and so on.
There are two types of I/O operations: byte stream operations and batch stream operations.
The byte stream includes the input stream and the output stream, and the input stream and the output stream.
Input stream (read data from screen, hard disk, or other data sources into memory) and output stream (used to output content to screen, hard disk, and other destinations)
A byte stream is the reading or output of a byte. (The byte is an integer between 0 and 255)
The character stream reads and outputs text characters, that is, directly reads numbers, letters, or Chinese characters that can be directly recognized.
About the buffer stream:
A buffer stream refers to a stream with a buffer function (bufferoutputstream) when output. A stream without a buffer function is directly saved to the destination when output. If there is a buffer function, the output content will be placed in the memory first. When there is a certain amount of data, or the stream is closed, or the flush () method is called, will Save the corresponding content to the hard disk or other destinations.
Fileinputstream and fileoutputstream are usually used for byte stream operations. While in stream operations, I usually use filereader and filewriter. Used with the buffer stream: bufferedreader and bufferedwriter. Bufferedinputstream and bufferedoutputstream.
The following code is a basic example of file operations:
Package COM. zyujie. io; import Java. io. bufferedinputstream; import Java. io. bufferedoutputstream; import Java. io. bufferedreader; import Java. io. bufferedwriter; import Java. io. file; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. filereader; import Java. io. filewriter; import Java. io. ioexception; import Java. io. inputstreamreader; public class fileop {/** determines whether the file is a directory based on the path */Public boolea N isdirectory (string path) {file dirname = new file (PATH); If (dirname. isdirectory () {return true;} else {return false;}/** create directory */Public Boolean createdirectory (string path) {file dirname = new file (PATH ); return dirname. mkdir ();}/** determine whether the file exists */Public Boolean isfile (string path) {file filename = new file (PATH); If (filename. isfile () {return true;} else {return false;}/** Create File */Public Boolean createfile (String path) throws ioexception {file filename = new file (PATH); Return filename. createnewfile ();}/** write content to the file. If you want to write content to a text file, we recommend that you use the filewriter of the ghost stream. It is not easy to garbled. * if you want to generate an image file, we recommend that you use the fileoutputstream of the byte stream. Or buffer stream */Public void writefile (string path) {try {// read the console input character, into the buffer inputstreamreader ISR = new inputstreamreader (system. in); bufferedreader BR = new bufferedreader (ISR); // specify the target file to be written. filewriter fw = new filewriter (PATH ); // buffer the output bufferedwriter BW = new bufferedwriter (FW); While (true) {// read a row string tempstr = BR. readline (); // write a row BW. write (tempstr); // write a line separator BW. newline (); // if there is an end in the written into the begin stream, exit the write operation if (tempstr. equals (" End ") {break;} BR. close (); ISR. close (); BW. flush (); BW. close (); FW. close ();} catch (exception e) {}}/** read the file. Similarly, if you write a file, you can use the prepare stream to operate */Public void readfile (string path) {try {// read the file and specify the target file filereader Fr = new filereader (PATH); // write the input stream file to the buffer zone bufferedreader BR = new bufferedreader (FR ); // define the read value of string variables. By default, the read value does not occupy the memory string tempstr = ""; // If the stream is ready to be read while (BR. ready () {tempstr + = BR. readline ();} BR. close (); Fr. close (); system. O Ut. println (tempstr);} catch (exception e) {}}/** specifies the directory for generating image and Excel files. byte streams are used, file Replication function */Public void copyimage (string path, string newpath) {try {// read the target image file to be copied file = new file (PATH ); // read the target image into a word-based throttling fileinputstream FCM = new fileinputstream (File); // buffer the byte stream to the buffer bufferedinputstream Bis = new bufferedinputstream (FS ); // specify the location of the generated Target Image File newfile = new file (newpath); // prepare the output stream fileoutputstream Fos = new fileoutputst Ream (newfile); // buffer bufferedoutputstream Bos = new bufferedoutputstream (FOS); // byte stream length int Len = 0; // keep repeating until the end of the byte stream length // no buffer stream is used // while (LEN = Fi. read ())! =-1) {// FOS. Write (LEN); // use the buffer stream while (LEN = bis. Read ())! =-1) {Bos. write (LEN);} Bos. flush (); Bos. close (); FOS. close (); bis. close (); FCM. close ();} catch (exception e) {}} public static void main (string [] ARGs) throws ioexception {fileop op = new fileop (); string Path = "D: // zhouyujie // log.txt "; string imgpath =" D: // zhouyujie // edu.png "; string imgnewpath =" D: // zhouyujie // eduattachment .png "; string xlspath = "D: // zhouyujie // tcms.xlsx"; string xlsnewpath = "D: // zhouyujie/TC Msattachment .xlsx "; Boolean flag = op. isfile (PATH); If (FLAG) {system. out. println ("file exists"); // read the file op. readfile (PATH); // copy the image op. copyimage (imgpath, imgnewpath); // copy excelop. copyimage (xlspath, xlsnewpath);} else {Boolean F = op. createfile (PATH); If (f) {system. out. println ("file generation, please write a string in the console. "); // Specify the file write content op. writefile (PATH) ;}else {system. Out. println (" file generation failed ");}}}}