如何在Java中读取文件时获取索引值

问题描述 投票:0回答:1

我写了一段代码来从文件中读取值,

public class Test 
{ 
  public static void main(String[] args) throws Exception 
  { 
    // pass the path to the file as a parameter 
    FileReader fr = 
      new FileReader("/home/workspace_ag7_tmv/Message Router/environments/wb/conf/subscriber_content_restriction.conf"); 

    int i; 
    while ((i=fr.read()) != -1) 
      System.out.print((char) i); 
      } 
} 

在我的文件中,我传递了这些值:PRE | 00000000110000200000049UPOS | 10000000110000200000049U

我能够使用上面的代码来获取这些值,现在我想获取第四个索引值。您能帮我怎么做吗?

java
1个回答
0
投票

您可以将字符添加到ArrayList,然后在第4个索引处获取元素。

int i;
List<Character> charList = new ArrayList<>();    
while ((i=fr.read()) != -1) 
  charList.add(char);
  System.out.print((char) i); 
  }
char a = charList.get(4);
© www.soinside.com 2019 - 2024. All rights reserved.