现在一txt文档内容如下:
#001#@aaa@
#002#@bbb@
#003#@ccc@
#004#@ddd@
如何读取指定行(不确定是第几行的情况)数据

解决方案 »

  1.   

    使用Scanner的nextLine吧,用一个循环控制读取哪一行
      

  2.   

    也可以用BufferedReader的readLineimport java.io.*;
    import java.util.*;public class Test
    {
    public static void main(String[] args) throws Exception
    {
    BufferedReader in = new BufferedReader(new FileReader("1.txt"));
    System.out.print("请输入行号:");
    Scanner scanner = new Scanner(System.in);
    int line = scanner.nextInt();
    String str = null;
    for (int i = 0; i < line; i++)
    {
    if ((str = in.readLine()) == null)
    {
    System.out.println("超出范围");
    }
    }
    System.out.println("第" + line + "行为:" + str);
    }
    }
      

  3.   

    给你写了个例子: public static String readLine(File file, int lineNum){
    BufferedReader reader = null;
    try {
    reader = new BufferedReader(new FileReader(file));
    int num = 1;
    while(true){
    String line = reader.readLine();
    if(line == null){
    break;
    }
    if(lineNum == num){
    return line;
    }
    num++;
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    return null;
    }
      

  4.   

    File file = new File("src/com/test/test.txt");
    InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file));
    BufferedReader br = new BufferedReader(streamReader);

    while(true){
    String line = (String)br.readLine();
    if(line.contains("#003#")){
    System.out.println(line);
    break;
    }
    }不知道能不能帮到你
      

  5.   

    你的方法是可以的,但我不知道为什么我这么写就不行,我从jt1中输入的是第二行的数据的开头部份,但总是输出第一行数据
      public class qx implements ActionListener{
    public void actionPerformed(ActionEvent e){
                try {
                    ATM_Longin atml = new ATM_Longin();
                    System.out.println(atml.getrw());
                } catch (FileNotFoundException ex) {
                    Logger.getLogger(ATM_Longin.class.getName()).log(Level.SEVERE, null, ex);
                }
    }
        }
        public String getrw() throws FileNotFoundException{
        String zh=jt1.getText();
        File file = new File("record.txt");
            InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file));
            BufferedReader br = new BufferedReader(streamReader);        while(true){
                try {
                    String line = br.readLine();
                    if (line.contains(zh)) {
                        return line;
                    }            } catch (IOException ex) {
                    Logger.getLogger(ATM_Longin.class.getName()).log(Level.SEVERE, null, ex);
                }
    }
        }
      

  6.   

    楼主你用Debug模式看看你有没有得到你输入的值
      

  7.   

          File file = new File("test.txt");
            InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file));
            BufferedReader br = new BufferedReader(streamReader);
            
            while(true){
                String line = (String)br.readLine();
                if(line.contains("#001#")){
                    System.out.println(line);
                    break;
                }
            }
      

  8.   


    这个是可以实现的,最后需要加in.close()关闭
      

  9.   

    我的想法是,读取这个txt文件,并放在一个 map中,
    然后你就可以通过 map的key找到对应的value了(也就是对应的行的数据)
    代码没有写,先看看这个想法,楼主能否明白java技术交流总群 161734506 欢迎喜欢Java,喜欢聊天的加入