The package of file read and write operation is realized by reference to the previous file writing function of the project.
Only need to pass in the path in the read and write method (can be absolute or relative path)
Later use, you can improve on this basis, such as:
Write operation:
1. Capture the contents of the Java GUI in the text box and place it in a TXT document
2, each type of data is written in the form of a string line
3, writes the contents of the global array
Read operation:
Gets the number of file rows
Type conversion of string-by-row data into a two-dimensional array
For the subsequent algorithm processing to provide a portal, but be careful : The number of rows can not be used as a global variable counter, or every time the read-in is a global variable accumulation error, should start reading again
1 ImportJava.io.BufferedReader; 2 ImportJava.io.BufferedWriter; 3 ImportJava.io.File; 4 ImportJava.io.FileReader; 5 ImportJava.io.FileWriter; 6 Importjava.io.IOException;7 8 Public classReadTextFile {9 PublicBufferedReader Bufread; Ten PublicBufferedWriter Bufwriter; One File WriteFile; A String filepath, filecontent, read; -String readcontent = "" ; - //read content from a text file the Publicstring ReadFile (string path) - { - Try { -filepath = path;//get the path to the text file +File File =NewFile (filepath); -FileReader Fileread =Newfilereader (file); +Bufread =NewBufferedReader (Fileread); A while(read = Bufread.readline ())! =NULL ) { atRead = Read + "\ r \ n";//Line breaks are added to each line read -ReadContent = ReadContent +Read; - } -}Catch(Exception d) { - System.out.println (D.getmessage ()); - } in returnReadContent;//returns the content read from a text file - } to Public voidWriteFile (String path) { + - Try { theBufferedWriter writer =NewBufferedWriter (NewFileWriter (path));//If the text file does not exist, create it * for(inti=0;i<256;i++){ $ for(intj=0;j<7;j++){Panax Notoginseng -Writer.write (Math.random () + ""); the } + writer.newline (); A } the //use the methods in the buffer to flush the data to the destination file. + Writer.flush (); - //closes the buffer while closing the writer stream object $ writer.close (); $}Catch(IOException e) { - //TODO auto-generated Catch block - e.printstacktrace (); the } - }Wuyi the - Wu Public Static voidMain (string[] args)throwsException { -ReadTextFile parse =NewReadTextFile (); About //Write a file $Parse.writefile ("D:/radar.txt"); - //Read the file (the primary implementation reads the entire text, or the Read file function returns a string type, or reads each row into a global string array) -String filecontent = Parse.readfile ("D:/radar.txt" ); - A System.out.println (filecontent); + the - } $}
Of course, the read-write path can also be stored as a global static variable string, but it is not conducive to separation, and therefore omitted.
Java file read and write operations class