newSplit

<pre name="code" class="java">package net.tqm.view.action;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JOptionPane;

import net.tqm.view.FileUtil;

public class Split
{
    public static void actByLineCount( JButton jbtnSqlScriptSplit )
    {
        jbtnSqlScriptSplit.addMouseListener( new MouseAdapter()
        {
            @Override
            public void mousePressed( MouseEvent e )
            {
                File origFile;
                int fileLength;
                if( ( origFile = FileUtil.selectFile( null ) ) == null )
                {
                    return;
                }
                try
                {
                    fileLength = Integer.parseInt( JOptionPane.showInputDialog( "please input split LINE count:" ) );
                }
                catch( Exception e1 )
                {
                    JOptionPane.showMessageDialog( null, "无长度或输入异常" );
                    return;
                }
                splitByFileLength( origFile, fileLength, (Integer) null );
            }
        } );
    }

    public static void actByFileCount( JButton jbtnSqlScriptSplit )
    {
        jbtnSqlScriptSplit.addMouseListener( new MouseAdapter()
        {
            @Override
            public void mousePressed( MouseEvent e )
            {
                File origFile;
                int fileLength;
                if( ( origFile = FileUtil.selectFile( null ) ) == null )
                {
                    return;
                }
                int fileCount = 0;
                try
                {
                    int origFileLength = getFileLength( origFile );
                    fileCount = Integer.parseInt( JOptionPane.showInputDialog( "please input split FILE count:" ) );
                    if( origFileLength % fileCount == 0 )
                        fileLength = origFileLength / fileCount;
                    else
                        fileLength = origFileLength / ( fileCount + 1 );
                }
                catch( Exception e1 )
                {
                    JOptionPane.showMessageDialog( null, "无长度或输入异常" );
                    return;
                }
                splitByFileLength( origFile, fileLength, fileCount );
            }
        } );
    }

    protected static int getFileLength( File origFile )
    {
        BufferedReader reader = null;
        int fileLength = 0;
        try
        {
            reader = new BufferedReader( new FileReader( origFile ) );
            fileLength = 0;
            // 一次读入一行,直到读入null为文件结束
            while( reader.readLine() != null )
            {
                fileLength++;
            }
            reader.close();
        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
        finally
        {
            if( reader != null )
            {
                try
                {
                    reader.close();
                }
                catch( IOException e1 )
                {
                }
            }
        }
        return fileLength;
    }

    private static void splitByFileLength( File origFile, int eachFileLength, int fileCount )
    {
        BufferedReader reader = null;
        try
        {
            int pow = getFileLength( origFile ) % fileCount;

            System.out.println( "以行为单位读取文件内容,一次读一整行:" );
            reader = new BufferedReader( new FileReader( origFile ) );
            int currentOrigLineCount = 0, newFileCount = 0, wrotedLineCount = 0;
            String tempString = null, useDB = null, firstPartName = null;
            String tailName = "";
            File newFile = null;
            String[] splitName = origFile.getName().split( "-" );
            if( sqlFileNameWithMoreThan2Dash( origFile, splitName ) )
            {
                for( int j = 3; j < splitName.length; j++ )
                {
                    tailName = tailName + "-" + splitName[j];
                }
                firstPartName = splitName[0] + "-" + splitName[1] + "-" + splitName[2];
            }
            else
            {
                firstPartName = origFile.getName();
            }
            String pathname = origFile.getAbsolutePath().substring( 0, origFile.getAbsolutePath().lastIndexOf( "\\" ) )
                + "\\" + firstPartName + ( newFileCount++ ) + tailName;
            newFile = new File( pathname );

            FileOutputStream w = new FileOutputStream( newFile );
            // 一次读入一行,直到读入null为文件结束
            while( ( tempString = reader.readLine() ) != null )
            {
                wrotedLineCount++;
                int newEachFileLength = 0;
                boolean shouldStartNewSqlFileWithLineCountAndDeviceSentenceBlockRestrict = false, shouldStartNewOrdinaryFile = false;
                
                boolean currentFileCountLessThanPow = newFileCount < pow;
                if( currentFileCountLessThanPow )
                {
                    newEachFileLength = eachFileLength + 1;
                    shouldStartNewSqlFileWithLineCountAndDeviceSentenceBlockRestrict = ( currentOrigLineCount++ > newEachFileLength
                        * newFileCount )
                        && tempString.trim().equals( "" ) && origFile.getName().endsWith( ".sql" );
                    shouldStartNewOrdinaryFile = ( currentOrigLineCount >= newEachFileLength * newFileCount )
                        && !origFile.getName().endsWith( ".sql" ) && newFileCount < fileCount;
                }
                else
                {
                    shouldStartNewSqlFileWithLineCountAndDeviceSentenceBlockRestrict = ( currentOrigLineCount++ > eachFileLength
                        * newFileCount + pow )
                        && tempString.trim().equals( "" ) && origFile.getName().endsWith( ".sql" );
                    shouldStartNewOrdinaryFile = ( currentOrigLineCount >= eachFileLength * newFileCount + pow )
                        && !origFile.getName().endsWith( ".sql" ) && newFileCount < fileCount;
                }
                if( currentOrigLineCount == 0 && origFile.getName().endsWith( ".sql" ) )
                {
                    useDB = tempString;
                }
                System.out.println( tempString );
                w.write( tempString.getBytes() );
                w.write( "\n".getBytes() );
                if( shouldStartNewSqlFileWithLineCountAndDeviceSentenceBlockRestrict || shouldStartNewOrdinaryFile )
                {
                    w.flush();
                    w.close();
                    pathname = origFile.getAbsolutePath().substring( 0, origFile.getAbsolutePath().lastIndexOf( "\\" ) )
                        + "\\" + firstPartName + ( newFileCount++ ) + tailName;
                    newFile = new File( pathname );
                    newFile.createNewFile();
                    w = new FileOutputStream( newFile );
                    if( currentOrigLineCount > 1 && useDB != null )
                    {
                        w.write( useDB.getBytes() );
                        w.write( "\n".getBytes() );
                    }
                }

            }
            w.flush();
            w.close();
            reader.close();
            origFile.delete();
            JOptionPane.showMessageDialog( null, "读取了" + currentOrigLineCount + "行," + " 执行了" + wrotedLineCount + "完成" );
        }
        catch( IOException e )
        {
            e.printStackTrace();
        }
        finally
        {
            if( reader != null )
            {
                try
                {
                    reader.close();
                }
                catch( IOException e1 )
                {
                }
            }
        }
    }

    private static boolean sqlFileNameWithMoreThan2Dash( File origFile, String[] splitName )
    {
        return origFile.getName().endsWith( ".sql" ) && splitName.length > 2;
    }
}



                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值