/*
* Created on 2005-4-17
*
* agrs: divied file
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package c12;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
/**
* @author zhang
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class DivideWordLibrary {
private static final String fileSuffix = ".txt";
private static final int maxLine = 20000;
private static String fileRename(String filename, String suffix, int index) {
int maxprefix = filename.toLowerCase()
.lastIndexOf(suffix.toLowerCase());
StringBuffer fileprefix = new StringBuffer();
for (int i = 0; i < maxprefix; i++) {
fileprefix.append(filename.charAt(i));
}
return fileprefix.append(index).append(suffix.toLowerCase()).toString();
}
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java DivideWordLibrary source");
System.exit(1);
}
try {
File source = new File(args[0]);
// File desc = new File(args[1]);
String filename = source.getName();
String path=source.getParent();
String str;
int i = 0,index=0;
BufferedReader in = new BufferedReader(new FileReader(source));
PrintWriter out = null;
while ((str = in.readLine()) != null) {
if (i % maxLine == 0) {
if (out != null)
out.close();
out = new PrintWriter(new FileWriter(new File(path+"/"+fileRename(
filename, fileSuffix, index++))));
}
out.println(str);
i++;
}
in.close();
if(out!=null)
out.close();
System.out.println("Successful!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}