一个处理文本数据的python脚本或者java小程序
发布网友
发布时间:2022-04-24 15:34
我来回答
共1个回答
热心网友
时间:2022-05-04 22:08
package com.ylq.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Test {
NumSaver saver = new NumSaver();
public static void main(String[] args) throws IOException {
Test test = new Test();
test.read("data/file.txt","data/out.txt");
}
public void read(String pathIn, String pathOut) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File(pathIn)));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(pathOut)));
String tmp = null;
while((tmp = reader.readLine()) != null){
System.out.println(tmp);
String years = getYearRange(tmp);
writer.write(years.substring(1,years.length() - 1) + "\n");
}
writer.write(saver.toString());
writer.flush();
reader.close();
writer.close();
}
public String getYearRange(String tmp){
String[] range = tmp.split(" \\- ");
List<Integer> years = new ArrayList<>();
for(int i = Integer.valueOf(range[0]); i <= Integer.valueOf(range[1]); i++){
years.add(i);
saver.save(i);
}
return years.toString();
}
public class NumSaver extends HashMap<Integer, Integer>{
private static final long serialVersionUID = 8101506360053704633L;
public void save(int num){
this.put(num, null);
}
@Override
public Integer put(Integer key, Integer value) {
return super.put(key, super.get(key) == null ? 1 : super.get(key) + 1);
}
@Override
public String toString() {
StringBuilder strBuilder = new StringBuilder();
for(Entry<Integer, Integer> entry : this.entrySet()){
strBuilder.append(entry.getKey() + ":" + entry.getValue() + ", ");
}
return strBuilder.toString().substring(0,strBuilder.lastIndexOf(","));
}
}
}
目录结构如下图所示:
看看是否满足你的要求
热心网友
时间:2022-05-04 22:08
package com.ylq.test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Test {
NumSaver saver = new NumSaver();
public static void main(String[] args) throws IOException {
Test test = new Test();
test.read("data/file.txt","data/out.txt");
}
public void read(String pathIn, String pathOut) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(new File(pathIn)));
BufferedWriter writer = new BufferedWriter(new FileWriter(new File(pathOut)));
String tmp = null;
while((tmp = reader.readLine()) != null){
System.out.println(tmp);
String years = getYearRange(tmp);
writer.write(years.substring(1,years.length() - 1) + "\n");
}
writer.write(saver.toString());
writer.flush();
reader.close();
writer.close();
}
public String getYearRange(String tmp){
String[] range = tmp.split(" \\- ");
List<Integer> years = new ArrayList<>();
for(int i = Integer.valueOf(range[0]); i <= Integer.valueOf(range[1]); i++){
years.add(i);
saver.save(i);
}
return years.toString();
}
public class NumSaver extends HashMap<Integer, Integer>{
private static final long serialVersionUID = 8101506360053704633L;
public void save(int num){
this.put(num, null);
}
@Override
public Integer put(Integer key, Integer value) {
return super.put(key, super.get(key) == null ? 1 : super.get(key) + 1);
}
@Override
public String toString() {
StringBuilder strBuilder = new StringBuilder();
for(Entry<Integer, Integer> entry : this.entrySet()){
strBuilder.append(entry.getKey() + ":" + entry.getValue() + ", ");
}
return strBuilder.toString().substring(0,strBuilder.lastIndexOf(","));
}
}
}
目录结构如下图所示:
看看是否满足你的要求