Add line numbers for source code

Source: Internet
Author: User

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

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.