如何在 Java 源代码中包含行号?

发布于 2024-10-27 12:12:36 字数 173 浏览 4 评论 0原文

我的最后一年编程项目即将达到高潮,该项目是用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

遮云壑 2024-11-03 12:12:36

您可以使用 cat 来执行此操作

cat -n input.java > output.java

You can use cat to do this

cat -n input.java > output.java
野心澎湃 2024-11-03 12:12:36

编写一个小型java程序来读取.java文件并将它们输出到带有行号的新文件中?

Write a litte java program to read your .java files and ouput them in new files with line numbers?

不知在何时 2024-11-03 12:12:36

对我来说,这听起来像是一个奇怪的要求,因为如果您在文件本身的每行开头插入行号,它将无法编译。您确定他们实际上并没有要求某种包含源代码的报告(代码本身中的行号仅在要打印代码时才有意义)。任何像样的文本编辑器都能够显示行号并打印它们,而行号不会成为文件的一部分,所以如果我是一名审查员,这就是我会使用的。但如果这确实是审查员想要的,您可以使用 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.

迷路的信 2024-11-03 12:12:36

例如,您可以提供(可能除了原始文本源代码之外)代码的 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.

┈┾☆殇 2024-11-03 12:12:36

使用 带有 -linksource 选项的 JavaDoc 工具。

创建每个源文件的 HTML 版本(带有行号)..

Use the JavaDoc tool with the -linksource option.

Creates an HTML version of each source file (with line numbers)..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文