One: The use of file processing in C + +, see blog C + + file operations
Two: The convenience of Java and C + +:
(1) Java when reading a file, the character stream can be processed and re-encoded, such as inputstreamreader reader = new InputStreamReader (new FileInputStream (file), encoding);
(2) Bufferreader bufferreader = new Bufferreader (file) for buffer processing
(3) The very good thing about Java is that you reach out, such as int to string (such as integer.tostring (Intnumber)), String separation (Stringstr.split (exp); ), the string is re-synthesized, directly add can, very useful;
(4) with the help of the integrated development environment, the IDE lacks the dependency class, the automatic introduction function, is even more powerful.
Three: Examples of processing txt ask files
Package Edu.tju.cs;import Java.io.bufferedreader;import Java.io.bufferedwriter;import java.io.File;import Java.io.fileinputstream;import Java.io.filewriter;import Java.io.inputstreamreader;public Class Originaldevidebytime {String times[] = {"6:00:00", "6:30:00", "7:00:00", "7:30:00", "8:00:00", "8:30:00", "9:00:00", " 9:30:00 "," 10:00:00 "," 10:30:00 "," 11:00:00 "," 11:30:00 "," 12:00:00 "," 12:30:00 "," 13:00:00 "," 13:30:00 "," 14:00:00 "," 14:30:00 "," 15:00:00 "," 15:30:00 "," 16:00:00 "," 16:30:00 "," 17:00:00 "," 17:30:00 "," 18:00:00 "," 18:30:00 "," 19:00:00 "," 19:30:00 "," 20:00:00 "," 20:30:00 "," 21:00:00 "," 21:30:00 "," 22:00:00 "," 22:30:00 "," 23:00:00 "," 23:30:00 "," 24:00:00 " };//Composite output file name public String getfilename (int cp) {string filename = "d:\\tjdata_metro\\results\\"; filename = filename + integ Er.tostring (CP) + ". csv"; return fileName;} Reads the file and separates the data public void Readtxtfile (string filePath) {try {string encoding= "GBK"; File File=new file (FilePath); int cp = 1; if (File.isfile () &&File.exists ()) {//Determine if the file exists inputstreamreader read = new InputStreamReader (New Fileinputstre AM (file), encoding);//considering the encoding format BufferedReader BufferedReader = new BufferedReader (read); String originalline = null; String newLine = null; while ((Originalline = Bufferedreader.readline ()) = null) {Bufferedreader.mark ((int) file.length () +1);//Setting Piece flag bit//write file name to handle if (CP >= times.length) return; String fileName = GetFileName (CP); BufferedWriter writer = new BufferedWriter (new FileWriter (FileName)); while ((Originalline = Bufferedreader.readline ()) = null) {//string-delimited string tmp[] = Originall Ine.split (","); If the tmp.length>3 is eligible for re-compositing if (Tmp[3].compareto) if (Times[Cp-1]) >=0&&tmp[3].compareto (TIMES[CP]) <0) {newLine = Tmp[0] + "," + tmp[1] + "," + tmp[2] + "," + t MP[3] + "," + tmp[4] + "," + tmp[5 "+", "+ tmp[6] +", "+ tmp[7"; System.out.println (NewLine); Writer.write (newLine + "\ n"); } else System.out.println ("not between times"); }//Close Write file Writer.close (); Reset to file start position, CP Self-bufferedreader.reset (); CP + +; } bufferedreader.close (); Read.close (); } else {System.out.println ("Originaldevidebytime ... The specified file could not be found "); }} catch (Exception e) {System.out.println ("Originaldevidebytime ... Error reading file contents "); E.printstacktrace (); }system.out.println ("Originaldevidebytime ... Devide is OVER!!! ");} Main function entry public static void main (string argv[]) {string FilePath= "D:\\tjdata_metro\\token_enex_201404_20w.csv"; Originaldevidebytime devide = new Originaldevidebytime (); Devide.readtxtfile (FilePath); }}
Four: Learning experience:
(1) file pointer in Java reset to the beginning of the file
File File = new file ("Temp.txt");
BufferedReader reader = null;
reader = new BufferedReader (new FileReader (file));
Reader.mark ((int) file.length () +1);
To do something
Reader.reset ();
Note: If the last character of the file string is Chinese, use the length in Mark () to File.length ()
If the last string of the file is English or numeric, then Java.io.IOException:Mark invalid, with the length in Mark () set to File.length () +1
Mark is used to mark the current position; Use reset to return to the mark Mark's position after reading a certain amount of data (less than readlimit); FileInputStream does not support mark/reset operations ; Bufferedinputstream supports this operation;
Mark (readlimit) means to make a mark at the current position, the maximum number of bytes that can be re-read, that is, if you read the number of bytes after the tag is greater than readlimit, you will no longer return to the position; usually InputStream read () Returns 1, indicating that the end of the file is reached and cannot be read again. Unless the mark/reset is used
(2) There are three ways to type int to string in Java
1 "string.valueof (i)
2 "integer.tostring (i)
3 "i+" "
The ValueOf () and ToString () methods can be understood, but why is it possible to add a "" quote in 3? Who has the information to see?
(3) Stringstr.compareto (otherstring), equal return 0, if STRINGSTR is greater than otherstring return positive, if STRINGSTR is less than otherstring return negative
Java Basic TXT file processing (feel more useful than C + +)