将输入文件解析为java中的两个单独文件

时间:2021-04-18 19:45:25

I have a input file as like below (for example):

我有一个输入文件,如下所示(例如):

10 12
1
2
...
9
10
1
2
...
11
12

The first tells that the next 10 lines are for part1

第一个告诉接下来的10行代表part1

Then following next 12 lines are for part2.

接下来的12行是第2部分。

I would like to create two separate files part1.txt and part2.txt parsing the original input.txt file.

我想创建两个单独的文件part1.txt和part2.txt解析原始input.txt文件。

How can do that? Any suggestions kindly? I am using java Scanner.

怎么办?好心的任何建议?我正在使用java Scanner。

Solution (partial): that worked for me based on the suggestions below

解决方案(部分):根据以下建议为我工作

    Scanner scanner = new Scanner(filename);        
    try {
        String[] first_line = scanner.nextLine().split("\\s+", 3); // reading the first line of the input file

        int EdgeCount = Integer.parseInt(first_line[0]);    
        int VertexCount = Integer.parseInt(first_line[1]);  
        String hasWeight = first_line[2];

        while (scanner.hasNextLine()) {
            if(EdgeCount != 0) { // check whether any more edges are left to read from input file
                Scanner edge_scanner = new Scanner(scanner.nextLine());

....

3 个解决方案

#1


1  

Since this sounds like homework, I will not go into too much code detail, but you could read the first line, and then use the .split("\\s+") method from the String class.

由于这听起来像家庭作业,我不会考虑太多的代码细节,但你可以阅读第一行,然后使用String类中的.split(“\\ s +”)方法。

Once you do this, you will end up with an array with 10 in the first location and 12 in the second.

完成此操作后,您将得到一个阵列,其中第一个位置为10,第二个位置为12。

While you iterate over the next lines, simply keep a counter and check if the value of the counter is less than or equal to 10. If this holds, then you know that you need to output one file. If the condition no longer holds and the counter is now greater than 10 but less than or equal to 10 + 12, then you know that you should print in the second file.

当您遍历下一行时,只需保留一个计数器并检查计数器的值是否小于或等于10.如果这样,则您知道需要输出一个文件。如果条件不再成立且计数器现在大于10但小于或等于10 + 12,那么您知道应该在第二个文件中打印。

#2


1  

First of all, Read file line by line, write first 10 line to part1.txt then write after 12 line to part2.txt.

首先,逐行读取文件,将第10行写入part1.txt,然后在12行之后写入part2.txt。

For this use such a this pattern:

为此使用这样的模式:

BufferedReader br = new BufferedReader( new FileReader("your input file path") );

BufferedReader br = new BufferedReader(new FileReader(“your input file path”));

    String line = null;

    int lineCounter = 1;

    while( (line = br.readLine()) != null)
    {
    if( (lineCounter % 23 ) < 11 )
    {
       //Write part1.txt
    }
    else if( (lineCounter %23) > 10 )
    {
        //write part2.txt
    }
    lineCounter++;
    }

    br.close();

#3


1  

Try this,

Scanner scanner = new Scanner(System.in);
br  = new BufferedReader(new FileReader("fileName.txt"));
int first = scanner.nextInt(); //10
int second = scanner.nextInt();//12
int x = 0;
int j = 0;
while ((sCurrentLine  = br.readLine()) != null) 
   {
     if (x <= first)
     {
        x++;
        //write in 1st file
     }
     else if (j <= second)
     {
        j++;
        //write in 2nd file
     }
}
br.close();

#1


1  

Since this sounds like homework, I will not go into too much code detail, but you could read the first line, and then use the .split("\\s+") method from the String class.

由于这听起来像家庭作业,我不会考虑太多的代码细节,但你可以阅读第一行,然后使用String类中的.split(“\\ s +”)方法。

Once you do this, you will end up with an array with 10 in the first location and 12 in the second.

完成此操作后,您将得到一个阵列,其中第一个位置为10,第二个位置为12。

While you iterate over the next lines, simply keep a counter and check if the value of the counter is less than or equal to 10. If this holds, then you know that you need to output one file. If the condition no longer holds and the counter is now greater than 10 but less than or equal to 10 + 12, then you know that you should print in the second file.

当您遍历下一行时,只需保留一个计数器并检查计数器的值是否小于或等于10.如果这样,则您知道需要输出一个文件。如果条件不再成立且计数器现在大于10但小于或等于10 + 12,那么您知道应该在第二个文件中打印。

#2


1  

First of all, Read file line by line, write first 10 line to part1.txt then write after 12 line to part2.txt.

首先,逐行读取文件,将第10行写入part1.txt,然后在12行之后写入part2.txt。

For this use such a this pattern:

为此使用这样的模式:

BufferedReader br = new BufferedReader( new FileReader("your input file path") );

BufferedReader br = new BufferedReader(new FileReader(“your input file path”));

    String line = null;

    int lineCounter = 1;

    while( (line = br.readLine()) != null)
    {
    if( (lineCounter % 23 ) < 11 )
    {
       //Write part1.txt
    }
    else if( (lineCounter %23) > 10 )
    {
        //write part2.txt
    }
    lineCounter++;
    }

    br.close();

#3


1  

Try this,

Scanner scanner = new Scanner(System.in);
br  = new BufferedReader(new FileReader("fileName.txt"));
int first = scanner.nextInt(); //10
int second = scanner.nextInt();//12
int x = 0;
int j = 0;
while ((sCurrentLine  = br.readLine()) != null) 
   {
     if (x <= first)
     {
        x++;
        //write in 1st file
     }
     else if (j <= second)
     {
        j++;
        //write in 2nd file
     }
}
br.close();