<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;
}
}