java 从文件中获取行号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17636157/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get line number from a file
提问by user2580264
How can I implement a method to return the line-number of the line currently being scanned from a file. I have two scanners, one for the file (fileScanner) and another for the line (lineScanner)
如何实现一种方法来返回当前从文件中扫描的行的行号。我有两台扫描仪,一台用于文件 (fileScanner),另一台用于线路 (lineScanner)
this is what I have, but I don't know if I need the linenumber in the constructor!
这就是我所拥有的,但我不知道是否需要构造函数中的行号!
public TextFileScanner(String fileName) throws FileNotFoundException
{
this.fileScanner = new Scanner(new File(fileName));
this.lineScanner = new Scanner(this.fileScanner.nextLine());
this.lineNumber = 1;
}
and I need this method:
我需要这个方法:
public int getLineNumber()
{
}
回答by hyde
You can use just one Scanner
object to read a file and reports the line numbers.
您可以仅使用一个Scanner
对象来读取文件并报告行号。
Here is a sample code:
这是一个示例代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LineNumber {
public static void main(String [] args) throws FileNotFoundException {
System.out.printf("Test!\n");
File f = new File("test.txt");
Scanner fileScanner = new Scanner(f);
int lineNumber = 0;
while(fileScanner.hasNextLine()){
System.out.println(fileScanner.nextLine());
lineNumber++;
}
fileScanner.close();
System.out.printf("%d lines\n", lineNumber);
}
}
Now, if you want do this using an Object-Oriented Programming approach then you can do something like this:
现在,如果您想使用面向对象的编程方法来做到这一点,那么您可以执行以下操作:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FileProcessor {
// Mark these field as private so the object won't get tainted from outside
private String fileName;
private File file;
/**
* Instantiates an object from the FileProcessor class
*
* @param fileName
*/
public FileProcessor(String fileName) {
this.fileName = fileName;
this.file = new File(fileName);
}
public int getLineNumbers() {
Scanner fileScanner = null;
try {
fileScanner = new Scanner(this.file);
} catch (FileNotFoundException e) {
System.out.printf("The file %s could not be found.\n",
this.file.getName());
}
int lines = 0;
while (fileScanner.hasNextLine()) {
lines++;
// Go to next line in file
fileScanner.nextLine();
}
fileScanner.close();
return lines;
}
/**
* Test our FileProcessor Class
*
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
FileProcessor fileProcessor = new FileProcessor("text.txt");
System.out.printf("%d lines\n", fileProcessor.getLineNumbers());
}
}
回答by manindra naresh
Prints the Current line number:
打印当前行号:
System.out.println("The line number is " + new Exception().getStackTrace()[0].getLineNumber());
System.out.println("行号为" + new Exception().getStackTrace()[0].getLineNumber());
Example:
例子:
public class LineNumberTest{
公共类 LineNumberTest{
public static void main(String []args){
System.out.println("The line number is " + new Exception().getStackTrace()[0].getLineNumber());
}
}
}