基于Java中的文件内容和模式匹配拆分文件

时间:2016-05-31 16:43:20

标签: java split

我有以下要求:

 Rate: GBP
 12/01/1999,90.5911501,Validated
 .....
 .....
18/01/1999,90.954996,Validated
Rate: RMB
24/04/2008,132.2542,Validated
 .....
25/04/2008,132.2279,Validated
28/04/2008,131.69915,Validated
Rate: USD
21/11/11,-0.004419534,Validated
Rate: RMB
24/04/2008,132.2542,Validated
 .....
25/04/2008,132.2279,Validated
28/04/2008,131.69915,Validated
Rate: RMB
24/04/2008,132.2542,Validated
 .....
25/04/2008,132.2279,Validated
28/04/2008,131.69915,Validated
 Rate: GBP
 12/01/1999,90.5911501,Validated
 .....
 .....
 Rate: GBP
 12/01/1999,90.5911501,Validated
 .....
 .....
Rate: USD
21/11/11,-0.004419534,Validated

这里我有3条“费率:人民币”行,其详细信息直到下一个“费率”行。 我的要求是根据“Rate:RMB”分割这个文件并创建输出文件“Rate:RMB及其详细信息。输出如下:

Rate: RMB
24/04/2008,132.2542,Validated
 .....
25/04/2008,132.2279,Validated
28/04/2008,131.69915,Validated
Rate: RMB
24/04/2008,132.2542,Validated
.....
25/04/2008,132.2279,Validated
28/04/2008,131.69915,Validated
Rate: RMB
24/04/2008,132.2542,Validated
 .....
25/04/2008,132.2279,Validated
28/04/2008,131.69915,Validated

我尝试使用Java但无法获得所需的输出。

我的代码:

public static void main( String[] args ) throws IOException
{

    FileInputStream fis = new FileInputStream( "C:\\Users\\User\\Desktop\\SplitFile\\InputTest.txt" );

    BufferedReader br = new BufferedReader( new InputStreamReader( fis ) );
    // System.out.println("11111");
    String s = "";
    String rate = "RMB";
    StringBuilder sb = new StringBuilder();
    int count = 1;
    // s=br.readLine();
    while ( ( s = br.readLine() ) != null )
    {
        if ( s.contains( "Rate" ) && s.substring( 6, 9 ).equals( rate ) )
        {

            if ( sb.length() != 0 )
            {
                System.out.println( count );
                File file = new File( "C:\\Users\\User\\Desktop\\SplitFile\\" + rate + "_" + count + ".txt" );
                PrintWriter writer = new PrintWriter( file, "UTF-8" );
                writer.println( sb.toString() );
                writer.close();
                sb.delete( 0, sb.length() );
                count++;
            }
            continue;
            // System.out.println(name);
            // call read file

        }
        sb.append( s + "\n" );
        System.out.println( sb );
        // s=br.readLine();
    }

    br.close();
}

1 个答案:

答案 0 :(得分:0)

我假设您想在每次显示第Rate: RMB行时创建一个新文件并将其下面的所有内容输出到该文件,此代码应该为您解决问题:

public static void main( String[] args ) throws Exception
{
    //get desktop location
    String location = System.getenv( "USERPROFILE" ) + "\\Desktop\\SplitFile\\";

    String regex = "^Rate..RMB";

    //get input file
    FileInputStream inputfile = new FileInputStream( location + "InputTest.txt" );
    BufferedReader br = new BufferedReader( new InputStreamReader( inputfile ) );

    int count = 0;
    String in = br.readLine();

    //if the file starts with the pattern, skip the first line
    if( Pattern.matches( regex, in ) )
        br.readLine();

    //create first output file
    File outfile = new File( location + "OutputFile_" + count + ".txt" );
    PrintWriter pw = new PrintWriter( outfile );

    while( in != null )
    {
        //if pattern matches start a new file
        if( Pattern.matches( regex, in ) )
        {
            //close previous output
            pw.flush();
            pw.close();

            //create new output file
            outfile = new File( location + "OutputFile_" + count + ".txt" );
            pw = new PrintWriter( outfile );

            count++;
            in = br.readLine();
            continue;
        }

        //print to file
        pw.println( in );

        in = br.readLine();
    }

    //close final output
    pw.flush();
    pw.close();

    //close input
    br.close();
}