从文本文件中提取值并将其赋值给变量 - Java

时间:2014-10-07 10:46:54

标签: java file-io

我无法将文本文件中的值分配给4个变量。该文件在每行中有四个值,用空格分隔。我想将每个值分配给不同的变量。

items.txt中的数据:

  344443 toothbrush WW WQ 

  243434 ToothPaste WE WQ

  349343 DentalFloss WS QA 

这是我的代码,它不起作用。

 File itemsSold = new File("items.txt");             

 if (itemsSold.exists()){                               
  Scanner inputFile = new Scanner(itemsSold);  

   while (inputFile.hasNextLine()) {

    Items = inputFile.nextLine();    

    ItemId = inputFile.nextInt();
    ItemName = inputFile.next();

    String itemShlve = inputFile.next();
    String itemcode= inputFile.next();

     System.out.print(ItemId + "\n");
     System.out.print(ItemName);
     System.out.print(itemitemShlve);

  }

   inputFile.close();                         

}

需要的输出就是这个,我得到的是:     344443     牙刷     WW

243434
ToothPaste 
WE

349343
DentalFloss
WS

Java的初学者,提前致谢。

4 个答案:

答案 0 :(得分:1)

你可以这样做:

      Scanner fScn = new Scanner(new File(“items.txt”));
      String data;

      while( fScn.hasNextLine() ){
           data = fScn.nextLine();

           String[] token = data.split(" ");
           itemId = Integer.parseInt(token[0]);
           itemName= token[1];
           itemShelve = token[2];
           itemCode = token[3];
      }
      fScn.close();

1)使用文件扫描程序逐行捕获数据

2)用白色空格将数据行拆分为令牌

3)相应地将令牌分配到相应的变量

4)关闭文件扫描程序

答案 1 :(得分:0)

不要混淆nextIntnextLine的来电。在这种情况下,只需使用nextLine即可。

答案 2 :(得分:0)

为每一行使用另一台扫描仪。

if (itemsSold.exists()){                               
      Scanner inputFile = new Scanner(itemsSold);  

      while (inputFile.hasNextLine()) { // check for next line
           Items = inputFile.nextLine(); // read next line

           Scanner scanner = new Scanner(Items);//create new Scanner for new line 
           ItemId = scanner.nextInt(); // read next int
           ItemName = scanner.next();
           String itemShlve = scanner.next();
           String itemcode= scanner.next();  
           scanner.close();  // don't forget to close the Scanner
           ...
      }
      inputFile.close();
}

注意:遵循Java命名约定。

答案 3 :(得分:0)

我希望它会有所帮助。如果你也想要第四个值。在SysOut循环中设置值4。

File file = new File("C:\\items.Txt");
        String[] variable = new String[4];
        try {
            FileReader fr = new FileReader(file);
            BufferedReader br = new BufferedReader(fr);
            String line;
            int count;
            String temp = new String();
            while((line = br.readLine())!=null) {
                count = 0;
                for(char c : line.toCharArray()) {
                    if(c ==' ') {
                        variable[count] = temp;
                        temp = new String();
                        count++;
                    }
                    else {
                        temp += c;
                    }
                }
                for(int i=0; i<3; i++) {
                    System.out.println(variable[i]);
                }
                System.out.println();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
br.close()
fr.close();