簡體   English   中英

java bufferedReader。 如何讀取行的一部分

[英]java bufferedReader. how to read parts of a line

好吧,這是我的問題。 我寫了一種算法來做特定的事情。 當前,我自己在類構造函數中創建進程並將它們存儲在優先級隊列中。 但是我希望能夠編寫多行的.txt文件。 每行將代表一個進程,其不同屬性之間用空格隔開。 這是我的.txt的樣子:

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

p1,p2 ...等是每個進程的名稱。 那么第二列是第一屬性,第三列是第二屬性。

我需要能夠一次讀取每一列,並將值存儲在我的流程中。 如何讀取這些值並加以區分? (將它們作為單獨的東西處理)

因此,您想逐行讀取文件並分開每一行嗎?

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
}

看看java.util.Scanner類,它可以幫助從Reader讀取單獨的標記。

它具有將下一個標記讀取為整數,字符串或許多其他類型的方法。 Javadoc類中也有一些示例...

您同時獲得了空格(分隔屬性)和換行(分隔整個過程信息)作為分隔符。

使用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進一步優化此方法。

為了能夠逐行讀取文件,我使用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]+ " ");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM