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

publicclassDicTransform{publicstaticvoidmain(finalString[]args)throwsIOException{fina... 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);
}
}
展开
 我来答
yr93739981
2017-03-24 · TA获得超过809个赞
知道答主
回答量:339
采纳率:0%
帮助的人:68.3万
展开全部
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Testa
{
public static void main(String[] args)
{
//传入参数为文件目录
test("d:/a.txt");
}

public static void test(String filePath){
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}

String []columnName = {"Id", "Name", "Languages", "Math", "English"}; //列名
int []courseIndexs = {2, 3, 4}; //课程对应的列
int i,j,index;
String line;
List> students = new ArrayList>();
//记录Id和总值,用于排序
List> sortList = new ArrayList>();
try {
br.readLine(); //去掉第一行
while((line = br.readLine()) != null){
index = 0;
String []se = line.split(" ");
Map student = new HashMap();
for(i = 0; i < se.length; i++){
if("".equals(se[i])){
continue;
}
if(index >= columnName.length){
continue;
}
student.put(columnName[index], se[i]);
index++;
}
//计算平均值,总值
double total = 0;
for(j = 0; j < courseIndexs.length; j++){
total += Double.parseDouble((String) student.get(columnName[courseIndexs[j]]));
}
double average = total / courseIndexs.length;
//只取一位小数
average = Math.round(average * 10)/10;
student.put("Total", total);
student.put("Average", average);

Map sort = new HashMap();
sort.put("Id", student.get("Id"));
sort.put("Total", student.get("Total"));
sortList.add(sort);

students.add(student);
}
br.close();

//排序
for(i = 0; i < sortList.size(); i++){
for(j = i + 1; j < sortList.size(); j++){
if((Double)sortList.get(i).get("Total") < (Double)sortList.get(j).get("Total")){
Map temp = sortList.get(i);
sortList.set(i, sortList.get(j));
sortList.set(j, temp);
}
}
}
Map sortedId = new HashMap();
for(i = 0; i < sortList.size(); i++){
sortedId.put(sortList.get(i).get("Id"), i+1);
}

//设定序号
for(j = 0; j < students.size(); j++){
students.get(j).put("Order", sortedId.get(students.get(j).get("Id")));
}

//输出(写到原文件)
//PrintWriter pw = new PrintWriter(new File(filePath));
//输出(写到其他文件)
PrintWriter pw = new PrintWriter(new File("D:/b.txt"));

pw.println("Id\tName\tLan\tMath\tEnglish\tAverage\tTotal\tSort");
int cIndex;
for(i = 0; i < students.size(); i++){
Map st = students.get(i);
cIndex = 0;
pw.println(st.get(columnName[cIndex++]) + "\t" + st.get(columnName[cIndex++])
+ "\t" + st.get(columnName[cIndex++])+ "\t" + st.get(columnName[cIndex++])
+ "\t" + st.get(columnName[cIndex++])
+ "\t" + st.get("Total")
+ "\t" + st.get("Average")
+ "\t" + st.get("Order"));
}
pw.flush();
pw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式