正在学习PL/SQL,教材的随书光盘中的源码文件没有序号,而且有时和教材中的源码不匹配。
为了按照教材的顺序调试PL/SQL代码,要么手工录入代码,要么在源码中查找,两种方式都不让人省心(费眼)。
于是,想着把这些.sql脚本文件中的内容合并成一个,不就方便查找了吗。
新问题来了,20个文件,一个个打开,复制、粘贴,虽然工作量不是特别大,但也属于体力活。
如果有个工具,能自动完成合并工作就好了!
于是,在查找了一些网络资源的基础上,利用我的Excel和Java技能,这一想法还真就实现了。
感谢以下文章的作者:
1. 用java io流把多个txt文件的内容合并到一个文件里
http://zhidao.baidu.com/link?url=jYrECLVeIA4u3vpx4NK-nySCQLp1fqrofB1zF4BjAfe4fIgGP2oVqoeJIDQUOow4rODWDFJRLqUX8TsITBOkj3pMhrxas-7RMUCf3214Ccm
2.windows批量提取文件名的方法(转)
http://blog.sina.com.cn/s/blog_54f7c72c010102ts.html
代码如下:
package iApps;
import static java.lang.System.out;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.util.Arrays;
public class FileMergeTool {
public static void main(String[] args){
String outfile="D:/merged.txt";
String[] files=new String[] {"D:/temp/3SpaceIdentation.sql","D:/temp/CaseDemo.sql",
"D:/temp/CodeIdentifierDemo.sql","D:/temp/CollectionDemo.sql",
"D:/temp/Comments.sql","D:/temp/CreateFunction.sql",
"D:/temp/DDLStatement.sql","D:/temp/DynamicSQL.sql",
"D:/temp/Exception1.sql","D:/temp/FORLoop.sql",
"D:/temp/IdentifierDefine.sql","D:/temp/IfElse.sql",
"D:/temp/LoopStructure.sql","D:/temp/ObjectType.sql",
"D:/temp/PackageDemo.sql","D:/temp/PLSQLBlock1.sql",
"D:/temp/ProcedureDemo.sql","D:/temp/RecordDemo.sql",
"D:/temp/TiggerDemo.sql","D:/temp/VariableDemo.sql"
};
MergeFile.mergeFiles(outfile,files);
}
}
class MergeFile {
public static final int BUFSIZE = 1024 * 8;
public static void mergeFiles(String outFile, String[] files) {
FileChannel outChannel = null;
out.println("Merge " + Arrays.toString(files) + " into " + outFile);
try {
outChannel = new FileOutputStream(outFile).getChannel();
for(String f : files){
Charset charset=Charset.forName("utf-8");
CharsetDecoder chdecoder=charset.newDecoder();
CharsetEncoder chencoder=charset.newEncoder();
FileChannel fc = new FileInputStream(f).getChannel();
ByteBuffer bb = ByteBuffer.allocate(BUFSIZE);
CharBuffer charBuffer=chdecoder.decode(bb);
ByteBuffer nbuBuffer=chencoder.encode(charBuffer);
while(fc.read(nbuBuffer) != -1){
bb.flip();
nbuBuffer.flip();
outChannel.write(nbuBuffer);
bb.clear();
nbuBuffer.clear();
}
fc.close();
}
out.println("Merged!! ");
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {if (outChannel != null) {outChannel.close();}} catch (IOException ignore) {}
}
}
}
运行结果:
Merge [D:/temp/3SpaceIdentation.sql, D:/temp/CaseDemo.sql, D:/temp/CodeIdentifierDemo.sql, D:/temp/CollectionDemo.sql, D:/temp/Comments.sql, D:/temp/CreateFunction.sql, D:/temp/DDLStatement.sql, D:/temp/DynamicSQL.sql, D:/temp/Exception1.sql, D:/temp/FORLoop.sql, D:/temp/IdentifierDefine.sql, D:/temp/IfElse.sql, D:/temp/LoopStructure.sql, D:/temp/ObjectType.sql, D:/temp/PackageDemo.sql, D:/temp/PLSQLBlock1.sql, D:/temp/ProcedureDemo.sql, D:/temp/RecordDemo.sql, D:/temp/TiggerDemo.sql, D:/temp/VariableDemo.sql] into D:/merged.txt
Merged!!
彩蛋:
如果一个文件夹下有数百个文件,如何合并?
1. 提取文件名: tree d:/temp /f >d:/files.txt (其中,d:/temp为多个文件所在的文件夹,提取后的文件名将输出到d:/fiels.txt中)
2. 创建调用以上Java工具(MergeFile.mergeFiles)的参数:文件列表(不要告诉我你准备一个一个去复制、粘贴,再加引号、逗号串成文件名数组)
我的方法:2.1 将上一步提取到的文件名复制到Excel表,使用trim()函数去掉空格,为所有文件添加路径头,形成完整文件路径名
2.2 使用字符串连接函数,将各单元格中的文件路径名串成一个长字符串(可作为 new String[] 的方法体)
3.然后,运行上述Java程序,成功!