java 文件读取部分内容_java中如何实现文件的批量读取并提取部分内容写入一个新文件。 单一文件读取写入参照补充...

publicclassDicTransform{publicstaticvoidmain(finalString[]args)throwsIOException{finalStringscelFile="C:\\Dic\\IT计算机.scel";//readscelintobytearrayfinalByteArrayOutputS...

public class DicTransform {

public static void main(final String[] args) throws IOException {

final String scelFile = "C:\\Dic\\IT计算机.scel";

// read scel into byte array

final ByteArrayOutputStream dataOut = new ByteArrayOutputStream();

try (RandomAccessFile file = new RandomAccessFile(scelFile, "r"); final FileChannel fChannel = file.getChannel();) {

fChannel.transferTo(0, fChannel.size(), Channels.newChannel(dataOut));

}

// scel as bytes

final ByteBuffer dataRawBytes = ByteBuffer.wrap(dataOut.toByteArray());

dataRawBytes.order(ByteOrder.LITTLE_ENDIAN);

System.out.println("文件: " + scelFile);

final byte[] buf = new byte[1024];

final String[] pyDict = new String[512];

final int totalWords = dataRawBytes.getInt(0x120);

// pinyin offset

dataRawBytes.position(dataRawBytes.getInt());

final int totalPinyin = dataRawBytes.getInt();

for (int i = 0; i < totalPinyin; i++) {

final int idx = dataRawBytes.getShort();

final int len = dataRawBytes.getShort();

dataRawBytes.get(buf, 0, len);

pyDict[idx] = new String(buf, 0, len, "UTF-16LE");

}

// extract dictionary

int counter = 0;

PrintWriter pw = new PrintWriter("C:/Dic2/new.txt");

for (int i = 0; i < totalWords; i++) {

final StringBuilder py = new StringBuilder();

final StringBuilder word = new StringBuilder();

int alternatives = dataRawBytes.getShort();

int pyLength = dataRawBytes.getShort() / 2;

boolean first = true;

while (pyLength-- > 0) {

final int key = dataRawBytes.getShort();

if (first) {

first = false;

} else {

py.append('\'');

}

py.append(pyDict[key]);

}

first = true;

while (alternatives-- > 0) {

if (first) {

first = false;

} else {

word.append(", ");

}

final int wordlength = dataRawBytes.getShort();

dataRawBytes.get(buf, 0, wordlength);

word.append(new String(buf, 0, wordlength, "UTF-16LE"));

// skip bytes

dataRawBytes.get(buf, 0, dataRawBytes.getShort());

}

// System.out.println(word.toString() + "\t" + py.toString());

System.out.println(word.toString());

pw.println(word);

counter++;

}

pw.close();

System.out.println("\n读出词汇'" + scelFile + "': " + counter);

}

}

展开

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值