如何在while循环中使用split方法进一步拆分从txt文件读入的信息?

时间:2014-08-31 10:28:31

标签: java split filereader

比如说,文本文件中的一行是:Richard,RL#17

我在while循环中拆分数据,我可以成功获取两个独立的信息。

现在我该做什么才能进一步分割字段[0] - 字符串 - 这样我就可以将“理查德”和“RL”分开。我必须再次拆分,这次是用“,”。

  File inputFile = new File ("Personnel.txt");
  FileReader in = new FileReader (inputFile);
  BufferedReader inFromFile = new BufferedReader (in);

  String line = inFromFile.readLine ();
  int cnt = 0;

  while (line != null)
  {
     String [] field = line.split ("#");
     int age = Integer.parseInt (field [1]);
     workers [cnt] = new Personnel (field [0], age);
     cnt++;
     line = inFromFile.readLine();
  }
  inFromFile.close();

  for (int i = 0; i < cnt; i ++)
  {
     System.out.println ();
     System.out.println (workers [i]);
  }

因此,我可以成功读取数据并使用配对的对象类显示它。现在,我想知道我会进一步将名为“Richard,RL#17”的名称分成两个独立的过去。我可以再次使用拆分方法,如果是,怎么做?

我已经尝试构建另一个while循环,但我不确定如何引用field [0] - 文本文件中的名称,在本例中 - 并从那里拆分。我尝试了另一个程序,但没有成功。另一个程序如下:

     File inputFile = new File ("placeHolder.txt");
     FileReader inFile = new FileReader (inputFile);

     placeHolder[] lotsOfText = new placeHolder[10];

     int cnt = 0;

     for(int i = 0; i < lotsOfText.length; i++)
     {
        String line = inFile.readLine();
        while(line != null)
        {
         String[] field = line.split(",");
         field = line.split("#");
         lotsOfText[cnt] = new placeHolder(field[0],field[1],field[2],field[3]);


         cnt++;
        }   
     }  

     System.out.println(field[0] + " " + field[1] + " " + field[2] + " " + field[3]);

   }

2 个答案:

答案 0 :(得分:1)

我认为这是你想要实现的目标。

添加 Manjunath Anand 的回应: 如果你想获得3个字符串,&#34;理查德&#34;,&#34; RL&#34;,&#34; 17&#34;从文件中的单行然后存储到对象中并打印它,您可以执行以下操作:

编辑:添加了一些异常处理并测试了代码:

public class Testing {

class Personnel {
    String name;
    String initials;
    int age;
    Personnel(String name, String initials, int age) {
        this.name = name;
        this.initials = initials;
        this.age = age;
    }
}

void readData() {
    File inputFile = new File ("Personnel.txt");
    FileReader in = null;
    BufferedReader inFromFile = null;
    ArrayList<Personnel> data = null;
    try {
        in = new FileReader (inputFile);
        inFromFile = new BufferedReader (in);

        // list that will hold all personnel objects
        data = new ArrayList<Personnel>();
        String line = inFromFile.readLine ();

        while (line != null) {
            String [] field = line.split("([,#])");
            String name = field[0]; // field[0] = "Richard"
            String initials = field[1]; // field[1] = "RL"
            int age = Integer.valueOf(field[2]);// field[2] = "17";
            data.add(new Personnel (name, initials, age));
            line = inFromFile.readLine();
        }
        inFromFile.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } 

    for(Personnel p : data) {
        System.out.println("Name:\t\t" + p.name);
        System.out.println("Initials:\t" + p.initials);
        System.out.println("Age:\t\t" + p.age);
        System.out.println();
    }
}

public static void main(String[] args) {
    Testing test = new Testing();
    test.readData();
}

}

答案 1 :(得分:0)

请尝试以下代码: -

String str1 = "Richard,RL#17";
String[] arr = str1.split("([,#])");

它会在一个阵列中一次性给你全部

编辑:

    File inputFile = new File ("Personnel.txt");
    FileReader in = new FileReader (inputFile);
    BufferedReader inFromFile = new BufferedReader (in);
    // Create a scanner object which has convenient methods to read from file reader.
    Scanner s = new Scanner(inFromFile); 
    String [] field;
    String personLine;
    // Check if there is any data in the file
    while(s.hasNextLine()) {
        // read the entire line form the scanner of the file
        personLine = s.nextLine().trim();
        field = personLine.split("([,#])"); // you can put any number of delimiters within the square braces which seperate the data for person stored in the file
        // rest of your code logic
    } // keep reading till you finish all data