0

我有一个包含 100 行的文件,
每行包含一个标签
我需要获取标签值,因为它的等级是 TagVertex 类的“id”

    public abstract class Vertex <T>{

         String vertexId ;// example Tag1

         T vertexValue ;

         public  abstract  T computeVertexValue();
}


   public class TagVertex extends Vertex<String> {

      @Override
      public String computeVertexValue() {
       // How to get the String from my file?
         return null;
}

T试试这个,但它不起作用

public static void main(String args[]) {
  File source //

  int i=90;

  int j=0;

  String s = null;

  try {
    Scanner scanner = new Scanner(source);

    while (scanner.hasNextLine()) {
        if (j==i) s= scanner.nextLine();

         else j++;
    }


} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

System.out.println(s);
  }}
4

2 回答 2

1

尽管有一种使用 BufferedReader 跳过字符的方法,但我认为没有一种内置方法可以跳过整行。

BufferedReader bf = new BufferedReader(new FileReader("MyFile.txt"));

for(int i = 1; i < myVertex.vertexId; i++){
    bf.readLine();
}

String n = bf.readLine();
if(n != null){
    System.out.println(n);
}

不过我认为可能有更好的主意。

于 2013-04-17T16:49:57.530 回答
0

This is command u can use to read from file:

BufferedReader bf = new BufferedReader(new FileReader("filename"));

This will read the file to the buffer. Now, for reading each line u should use a while loop and read each line into string. Like:

String str;
while((str = bf.readLine()) != null){
//handle each line untill the end of file which will be null and quit the loop
}
于 2013-04-17T16:28:20.727 回答