【问题标题】:java bufferedReader. how to read parts of a linejava bufferedReader。如何读取一行的部分内容
【发布时间】:2015-04-11 21:40:20
【问题描述】:

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

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

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

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

【问题讨论】:

  • 您尝试过什么?您基本上是在询问如何在 java 中读取文本文件(逐行),然后用空格分隔这些行(字符串)。这两个问题都在 SO 和互联网上的其他各个地方得到了广泛的回答

标签: java


【解决方案1】:

所以你想逐行读取文件并分隔每一行?

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
}

【讨论】:

    【解决方案2】:

    查看java.util.Scanner 类,它可以帮助从Reader 中读取单独的标记。

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

    【讨论】:

      【解决方案3】:

      您将空格(分隔属性)和换行符(分隔整个过程信息)作为分隔符。

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

      【讨论】:

        【解决方案4】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-10-18
          • 2015-05-14
          • 2012-12-22
          • 1970-01-01
          • 2011-10-28
          • 1970-01-01
          • 2013-10-30
          相关资源
          最近更新 更多