从文件中读取特定行并从一行中分离数据

时间:2017-08-18 05:51:36

标签: java file

我对java很新,所以任何帮助都会受到赞赏。

我试图编写一个程序,询问用户要读取哪个文件,然后询问ID,在文件中搜索该ID,然后从该行输出信息。 它将从中读取的文本文件的格式如下:

苹果^ 201 ^ 3

香蕉^ 202 ^ 4

橙^ 205 ^ 5

2nd列用于ID。

另外我需要将信息分开一些如何输出它:

项目:Apple

ID:201:

价格:3美元

import java.util.*;
import java.io.*;
public class fruit
{
   public static void main(String[] args) throws IOException
{
  Scanner keyboard = new Scanner(System.in);

  System.out.println("Enter a filename >> ");
  String filename = keyboard.nextLine();

  File f = new File(filename);
  Scanner fin = new Scanner(f);

  System.out.println("Enter item ID: ");
  int fruitID = keyboard.nextInt();

  while(fin.hasNextLine())
  {
      String line = fin.nextLine();

      if(fin.hasNextInt(fruitID))
      {
          System.out.println(line);
      }
      else
      {
          System.out.println("ERROR");
      }
   }
      fin.close();
  }
}

3 个答案:

答案 0 :(得分:1)

由于字符串拆分需要正则表达式而^具有正则表达式含义,因此需要进行反斜杠

    String input = fin.nextLine();  // e.g. Banana^202^4
    System.out.println(input.split("\\^")[2]);

答案 1 :(得分:1)

public static void main(String[] args) throws IOException {
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a filename >> ");
    String filename = keyboard.nextLine();
    System.out.println("Enter item ID: ");
    String fruitID = Integer.toString(keyboard.nextInt());

    try (BufferedReader buffer = new BufferedReader(new FileReader(filename))) {
        String line;
        while ((line = buffer.readLine()) != null) {
            String[] tokens = line.split("\\^");
            if (tokens.length < 3 && !tokens[1].equals(fruitID))
                continue;
            System.out.println("Item: " + tokens[0]);
            System.out.println("ID: " + tokens[1]);
            System.out.println("Price: $" + tokens[2]);
            break;
        }
    }
    catch (Exception ex){
        ex.printStackTrace();
    }
}
  • Split&#34; ^&#34;,检查您是否至少有3个令牌,然后在第二个令牌等于输入ID时打印它们。
  • Starting Java 7,您可以使用try资源来确保在语句结束时关闭每个资源。
  • 找到特定行后,可以使用break来避免解析文件的其余部分。

答案 2 :(得分:1)

Hope this will help


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class FindGivenStringFromFile {

    public static void main(String args[]) throws FileNotFoundException {

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Enter a filename >> ");
        String filename = keyboard.nextLine();

        File f = new File(filename);
        Scanner fin = new Scanner(f);

        System.out.println("Enter item ID: ");
        int fruitID = keyboard.nextInt();

        //Reading each line of file using Scanner class
        int lineNumber = 1;
        while (fin.hasNextLine()) {
            String line = fin.nextLine();
            String[] lineDataArray = line.split("\\^");
            if(lineDataArray != null && lineDataArray.length >2){
                if(Integer.parseInt(lineDataArray[1]) == fruitID){
                    System.out.println("Item: " + lineDataArray[0]);
                    System.out.println("Id: " + lineDataArray[1]);
                    System.out.println("price: $" + lineDataArray[2]);
                }

            lineNumber++;
        }       

    }   
}
}