如何读取文本文件的每一列,并将它们放在单独的数组中?

时间:2013-09-22 23:37:46

标签: java

我正在进行编程分配,基本上我需要从txt文件中读取并对不同数组中的所有内容进行排序,这样我就可以整齐地显示cmd提示中的所有内容,并能够删除内容。

h Vito 123
d Michael 234 Heart
s Vincent 345 Brain Y
n Sonny 456 6
a Luca 567 Business
r Tom 678 Talking Y
j Anthony 789 Maintenance N
d Nicos 891 Bone
n Vicky 911 7

第一栏需要是employeeRole(员工,医生)。第二列是employeeName。第三列是employeeNumber,其中一些列有第四列(如果它是一个数字,它是患者的数量.Y用于扫描或接听电话)

所以我的思考过程将每一列放入它自己的数组中,然后以这种方式写出来。我能够使用

将每一行放入自己的数组中
public class ReadingFile {
 // String test;
  // char[] employeeRole = new char[9];
   String[] employeeRole = new String[9];
   String[] employeeName = new String[9], specialty;
   String[] wholeLine = new String[9];
  // String word;

   int[] employeeNum = new int[9];
   int r, n, l, num;
   public void Reader()
    {
    Scanner inputStream = null;
    Scanner inputStream2 = null;
    Scanner inputStream4 = null;
    try
    {
        BufferedReader inputStream3 =
                    new BufferedReader(new FileReader("data.txt"));
        inputStream = new Scanner(new FileInputStream("data.txt"));
        inputStream =
                    new Scanner(new FileInputStream("data.txt"));
        inputStream2 =
                    new Scanner(new FileInputStream("data.txt"));
                    inputStream4 =
                    new Scanner(new FileInputStream("data.txt"));
                    System.out.println("Yeah");

    }
    catch(FileNotFoundException e){
        System.out.println("File Not found");
        System.exit(1);
    }
for (l=0; l<9; l++)
{
    wholeLine[l] = inputStream2.nextLine();

    System.out.println(wholeLine[l]);
}

但我无法弄清楚该怎么做。然后进行拆分会将数组放入数组中?这意味着我会将每一行放入一个数组,然后将每个单词放入一个数组中?

所以我尝试了其他的东西,长度不等于1的任何东西都是employeeNum,但那时他们就有N和Y以及专利数量。

    for(r=0; r<9; r++) //role
    {
       String next = inputStream4.next();
       while( next.length() != 1)
       {
           next = inputStream4.next();
       }

        employeeRole[r] = next;

       System.out.println(employeeRole[r]);
    }

我也试过

for (r=0; r<9; r++)
{
    employeeRole[r] = wholeLine[r].substring(wholeLine[r].indexOf(1));
    //inputStream.nextLine();
    System.out.println(employeeRole[r]);
}

我只是不确定我是否正确的方式呢?如果我让它变得比实际更困难?或者,如果有更简单的方法来做到这一点。但是一切都完成后,输出应该能够基本上说

Doctors: 2
Name: Michael  Employee Number: 234 Specialty: Heart
Name: Nicos     Employee Number: 891 Specialty: Bone

非常感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:6)

你不必打开4个流来读取文件(我想你想打开“每列一个”但你不应该这样做)。

其次,你可以在空格(“”)上拆分字符串,它将为你提供完全符合你想要的列(对于每一行)。

代码示例:

    BufferedReader br = null;
    String[] characters = new String[1024];//just an example - you have to initialize it to be big enough to hold all the lines!

    try {

        String sCurrentLine;
        br = new BufferedReader(new FileReader("data.txt"));

        int i=0;
        while ((sCurrentLine = br.readLine()) != null) {
            String[] arr = sCurrentLine.split(" ");
            //for the first line it'll print
            System.out.println("arr[0] = " + arr[0]); // h
            System.out.println("arr[1] = " + arr[1]); // Vito
            System.out.println("arr[2] = " + arr[2]); // 123
            if(arr.length == 4){
                System.out.println("arr[3] = " + arr[3]);
            }

            //Now if you want to enter them into separate arrays
            characters[i] = arr[0];
            // and you can do the same with
            // names[1] = arr[1]
            //etc
            i++;
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }