java bufferedReader。如何阅读一行的部分

时间:2015-04-11 21:40:21

标签: java

好的,这是我的问题。我写了一个算法来做特定的事情。目前,我在类构造函数中自己创建了我的进程,并将它们存储在优先级队列中。但是,我希望能够编写多行的.txt文件。每一行代表一个进程,其不同的属性由空格分隔。这是我的.txt的样子:

P1 0 8
P2 1 4
P3 2 9
P4 3 3
END 4 9999

p1,p2 ......等是每个进程的名称。然后第二列是第一个属性,第三列是第二个属性。

我需要能够一次读取每个列并将值存储在我的进程中。我如何阅读这些值并区分它们? (将它们视为单独的东西)

4 个答案:

答案 0 :(得分:3)

所以你想逐行阅读文件并分开每一行?

BufferReader in=new BufferedReader...
String line;
while ((line=in.readLine())!=null) {
  String[] data=line.split(" ");
  //now, data will be a array which contains the data
  //data[0] = the first item in the line
  //data[1] = the first number
  //data[2] = the second number
}

答案 1 :(得分:0)

查看java.util.Scanner课程,它有助于从Reader读取单独的令牌。

它有方法将下一个标记读取为整数,字符串或许多其他类型。 Javadoc类中还有一些例子......

答案 2 :(得分:0)

你有空格(分隔属性)和新行(分隔整个过程信息)作为分隔符。

使用BufferedReader,您可以读取整行(reader.readLine())来解析一个完整的流程信息,并使用String.split()来分隔属性(编辑:请参阅dyslabs的答案)。

显然更高效(但不太直观)的方法是读取单个字符(reader.read())并检查是否要读取空白字符或新行字符:

// caution: code is not tested but shows the general approach
List<ProcessInformation> processInfo = new ArrayList<>();
String pInfoStr = new String[3];

int processInfoIndex = 0;
String[] processInfoHolder = new String[3];
String processInfo = "";
int c;
while( (c = reader.read()) != -1 ) {
   if (Character.isWhitespace(c)) {
      processInfoHolder[processInfoIndex++] = processInfo;
      processInfoStr = "";
   }
   else if (c == 10) { // not sure if correct codepoint for whitespace
      processInfo.add(new ProcessInfo(processInfoHolder));
      processInfoIndex = 0;
   }
   else {
      processInfoStr += c;
   }
}

您可以使用StringBuilder更好地优化此方法。

答案 3 :(得分:0)

为了能够逐行读取文件,我使用readLine()!= null,同时为了检索由空格分隔的值,使用split方法并将单行的每个值存储在数组中, 这是我实现你的例子的方式:

public static void main(String[] args) {
    // TODO Auto-generated method stub
    BufferedReader buffer;
    FileReader fileReader;
    String p1[] = new String[4];
    String p2[] = new String[4];
    String p3[] = new String[4];
    String p4[] = new String[4];
    String end[] = new String[4];
    try {
        fileReader = new FileReader(new File("file.txt"));
        buffer = new BufferedReader(fileReader);
        String line;
        line = buffer.readLine();
        // ============= Read the fist line =============
        p1 = line.split("\\s+");

        while((line = buffer.readLine()) != null) {

            // ============= Read the second line =============
            p2 = line.split("\\s+");        

            // ============= Read the third line =============
            if((line = buffer.readLine()) != null) {
                p3 = line.split("\\s+");        
            }
            // ============= Read the forth line =============
            if((line = buffer.readLine()) != null) {
                p4 = line.split("\\s+");        
            }
            // ============= Read the last line =============
            if((line = buffer.readLine()) != null) {
                end = line.split("\\s+");       
            }

        }
        fileReader.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    int v1[] = new int[3];
    int v2[] = new int[3];
    int v3[] = new int[3];
    int v4[] = new int[3];
    int v_end[] = new int[3];


    for (int i = 0 ; i < p1.length; i++)
        System.out.print(p1[i]+ " ");
    System.out.println();
    for (int i = 0 ; i < p2.length; i++)
        System.out.print(p2[i]+ " ");
    System.out.println();
    for (int i = 0 ; i < p3.length; i++)
        System.out.print(p3[i]+ " ");
    System.out.println();
    for (int i = 0 ; i < p4.length; i++)
        System.out.print(p4[i]+ " ");
    System.out.println();
    for (int i = 0 ; i < end.length; i++)
        System.out.print(end[i]+ " ");
}