Dived a larger characters file

/*
 * 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();
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值