如何在 Java 源代码中包含行号?
我的最后一年编程项目即将达到高潮,该项目是用 Java 编写的(使用 Wicket Web 框架)。我必须向审查员提供源代码(在 CD 上),并且每一行都有清晰的编号。我知道在 Eclipse 中您可以对代码行进行编号,但显然这只有在查看代码的人使用 Eclipse 时才有效。有没有办法在源代码本身中包含行号?如果是这样,怎么办?
I'm reaching the climax of my final year programming project, which is written in Java (using the Wicket web framework). I have to provide the examiners with the source code (on CD) with each line clearly numbered. I know that in Eclipse you can number the lines of code, but obviously this will only work if the person viewing the code uses Eclipse. Is there any way to include line numbers in the source code itself? If so, how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用
cat
来执行此操作You can use
cat
to do this编写一个小型java程序来读取.java文件并将它们输出到带有行号的新文件中?
Write a litte java program to read your .java files and ouput them in new files with line numbers?
对我来说,这听起来像是一个奇怪的要求,因为如果您在文件本身的每行开头插入行号,它将无法编译。您确定他们实际上并没有要求某种包含源代码的报告(代码本身中的行号仅在要打印代码时才有意义)。任何像样的文本编辑器都能够显示行号并打印它们,而行号不会成为文件的一部分,所以如果我是一名审查员,这就是我会使用的。但如果这确实是审查员想要的,您可以使用 unix 命令
nl
,或者编写自己的程序来完成它。That sounds like a strange requirement to me, because if you insert a line number at the beginning of each line in the file itself, it won't compile. Are you sure they're not actually asking for some kind of report that includes the source code (line numbers in the code itself only makes sense if the code is to be printed). Any decent text editor is able to both display line numbers and print them, without the numbers being a part of the file, so if I were an examiner, that's what I would use. But if this is indeed what the examiners want, you could use the unix command
nl
, or write your own program that does it.例如,您可以提供(可能除了原始文本源代码之外)代码的 html 版本(带或不带语法突出显示),其中包括行号。我通过谷歌搜索找到了几个可以做到这一点的工具,例如 http://www.java2html.de/ 。我认为 JDK 实际上附带了这样的应用程序。
但是,如果您想要的只是纯文本,您当然可以简单地编写一个小例程来为您添加行号。只需逐行读取代码行,递增计数器并在该行前面添加当前计数器。
You could for example supply (probably in addition to the raw-text-sourcecode) an html version of your code (with or without syntax highlighting) which includes linenumbers. There are several tools I found by googling which do that, for example http://www.java2html.de/ . I think the JDK actually ships with such an application.
However, if it's just plain text you want, you could of course simply write a little routine to add linenumbers for you. Just read the lines of your code one by one, increment a counter and prepend the line with the current counter.
使用 带有 -linksource 选项的 JavaDoc 工具。
Use the JavaDoc tool with the -linksource option.