When writing a book, you need to add a row number before the code in the book. It is troublesome to write it manually. In order to facilitate the compilation of a piece of code for processing. The file is as follows:
Import java. Io. file;
Import java. Io. fileinputstream;
Import java. Io. fileoutputstream;
Import java. Io. printwriter;
Import java. util. collections;
Public class addlinenumber {
Public static void main (string [] ARGs ){
Running in = New Processing (system. In );
System. Out. println ("Enter the original file (full path, for example, F: // source.txt ):");
String filename = in. nextline ();
Try {
Addlinenumber. addlinenumber (filename );
System. out. println ("processed, file:" + filename. substring (0, filename. lastindexof (". ") +" new "+ filename. substring (filename. lastindexof (". ")));
} Catch (exception e ){
System. Out. println (E. tostring ());
}
}
/*
* Add a row number for the source file
*/
Public static void addlinenumber (string filename) throws exception {
File originalfile = new file (filename );
Bytes in = new bytes (New fileinputstream (originalfile ));
File newfile = new file (filename. substring (0, filename. lastindexof (". ") +" new "+ filename. substring (filename. lastindexof (". ")));
Printwriter out = new printwriter (New fileoutputstream (newfile ));
Int Index = 1;
While (in. hasnextline ()){
String temp = in. nextline ();
Out. println (index + "/t" + temp );
Index ++;
}
Out. Close ();
In. Close ();
}
}
Running effect:
Source file content:
Entitymanagerfactory EMF = persistence. createentitymanagerfactory ("default ");
Entitymanager em = emf. createentitymanager ();
Em. gettransaction (). Begin ();
Em. persist (User );
Em. gettransaction (). Commit ();
Code after adding a row number:
1 entitymanagerfactory EMF = persistence. createentitymanagerfactory ("default ");
2 entitymanager em = emf. createentitymanager ();
3 em. gettransaction (). Begin ();
4 em. persist (User );
5 em. gettransaction (). Commit ();
No comments added. Let's take a look.
Li xucheng csdn blog: http://blog.csdn.net/javaeeteacher
Csdn student base camp: http://student.csdn.net/space.php? Uid = 1, 124362
If you like my article, add me as a friend: http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5