如何在给定的搜索中加载相应的数据

时间:2012-08-14 09:37:24

标签: java arrays string search java-io

我已经创建了一个程序,用于在文本字段中保存输入的数据。它的保存技术就是这样

data0 = student number
data1 = name
data2 = section
data3 = cp
data4 = email
data5 = address

在保存的文件中:

data0 | data1 | data2 | data3 | data4 | data5
data0 | data1 | data2 | data3 | data4 | data5
data0 | data1 | data2 | data3 | data4 | data5
data0 | data1 | data2 | data3 | data4 | data5
data0 | data1 | data2 | data3 | data4 | data5

data0具有独特性, 这是我用来搜索学生编号“data0”的代码 如果我搜索12293例如

data0 | data1 | data2 | data3 | data4 | data5
data0 | data1 | data2 | data3 | data4 | data5
12293 | blahh | blehh | blihh | blohh | bluhh
data0 | data1 | data2 | data3 | data4 | data5

并且在第3行有一场比赛,等等,哇,blihh,blohh,bluhh必须在不同的textarea中打印

但我不知道如何切片data1 | data2 | data3 | data4 | data5 |搜索匹配时进入数组

这是我的代码:

import java.io.*;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Jfetizanan
 */
public class DATALOAD {

    /**
     * @param args the command line arguments
     * @throws UnsupportedEncodingException
     * @throws FileNotFoundException
     * @throws IOException  
     */
    public static void main(String[] args) throws UnsupportedEncodingException, FileNotFoundException, IOException {
        try{
      FileInputStream fstream = new FileInputStream("data.dat");
            try (DataInputStream in = new DataInputStream(fstream)) {
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                String strLine;
                while ((strLine = br.readLine()) != null)   {

                 if (strLine.startsWith("JFETZ")){
                 System.out.println("Data Found");

                 }
                 else
                 {System.out.println("Nothing Found in this line");}
                }
            }
      }catch (Exception e){
          System.err.println("Error: " + e.getMessage());
      }
    }
}

2 个答案:

答案 0 :(得分:0)

{p> split()它由|

String values[] = strLine.split("\\|");

并将搜索键与数组values[0]

的第一个元素进行比较
while ((strLine = br.readLine()) != null)   {
 String values[] = strLine.split("\\|");
 if ("JFETZ".equals(values[0]))){
   System.out.println("Data Found");
 }else{
   System.out.println("Nothing Found in this line");}
 }
}

答案 1 :(得分:0)

你得到str喜欢:

String str = "12293 | blahh | blehh | blihh | blohh | bluhh";
String[] arr = str.split("\\| "); // give space after | to trim spaces