-1
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class Process_TCPProbe_dm{

   public static void main(String[] args) throws IOException {
      
       //Extracting the port numbers from the file names passed in the arguments
       String portNumber1 = args[1].substring(9, 14);
       String portNumber2 = args[2].substring(9, 14);
       String portNumber3 = args[3].substring(9, 14);
              
       int i = 0;
       Scanner s = new Scanner(new FileReader(args[0]));
       //Initialising array of contents with length equals to number of lines in the input file
       String[] contents = new String[18];      
       while(true)
       {
           if (i == 18)
               break;//Skipping the last line of the file
           //Replaces the whitespace with comma and stores in the string array
           contents[i] = s.nextLine().replace(" ", ",");
          
           i++;  
       }
      
       BufferedWriter bw1 = new BufferedWriter(new FileWriter(args[1]));
       BufferedWriter bw2 = new BufferedWriter(new FileWriter(args[2]));
       BufferedWriter bw3 = new BufferedWriter(new FileWriter(args[3]));
       //Iterating the array of contents using a for each loop
       for(String each:contents)
       {
           //Writing the string to the corresponding file which matches with the portNumber
           //along with a new line
           if (each.contains(portNumber1))
           {
               bw1.write(each);
               bw1.newLine();
              
           }
           else if(each.contains(portNumber2))
           {
               bw2.write(each);
               bw2.newLine();
              
           }
           else if(each.contains(portNumber3))
           {
               bw3.write(each);
               bw3.newLine();
              
           }
       }
       //This is necessary to finally output the text from the buffer to the corresponding file
       bw1.flush();
       bw2.flush();
       bw3.flush();
      
       //Closing all the file reading and writing handles
       bw1.close();
       bw2.close();
       bw3.close();
       s.close();
   }

}

这是我的代码,但是,它只读取 19 行大小的文件。我知道我可以使用ArrayList,但我不确定如何去做。我希望我的问题是有道理的。如果它可以帮助您更好地理解,请把代码的目的放在下面。

此图像是代码编写用途的说明。它可能只是额外的信息,但我把它放在这里以防它更好地帮助解释我的问题。

4

2 回答 2

2

只需像这样用 Array Lins 替换您的数组ArrayList<String> list = new List<>();,然后contents[i] = s.nextLine().replace(" ", ",");list.add(s.nextLine().replace(" ", ","));. After this 替换,您将不再需要计数器“i”并在 if 条件下中断循环。只需添加scanner.hasNext()一段时间条件。

于 2020-07-03T22:53:39.640 回答
2

您的循环条件应该基于Scanner.hasNextLine并且您不需要变量i。使用 anArrayList对未知数量的元素进行评分。

Scanner s = new Scanner(new FileReader(args[0]));
final List<String> contents = new ArrayList<>();  
while(s.hasNextLine()){
   contents.add(s.nextLine().replace(" ", ","));
}

这可以使用 缩短Files.lines。请注意,这会引发IOException您需要处理的问题。

final List<String> contents = Files.lines(Paths.get(args[0]))
    .map(s->s.replace(" ", ",")).collect(Collectors.toList());
于 2020-07-03T22:56:38.523 回答