用java读入、修改、再把修改后的数据写入新txt,希望写入新txt的结果是所有的相邻字母对出现的次数(ab=次,ba=次,aa=次,等等),原始数据中的*和#分别表示字母串的开始和结束。
我是Java初学者,请大家帮忙,写一个可以直接运行并且初学者能读懂的代码,关键部分给出注释。感谢大家热心帮忙。
源txt部分数据(都是这种格式):
*
a
b
#
*
a
b
b
#
*
a
a
b
c
#
*
a
c
c
b
#
*
d
#
*
a
d
b
a
d
d
c
#
[code="java"]
public static void main(String[] args) throws Exception {
adjacentLetters();
}
public static void adjacentLetters() throws Exception {
String inFile = "D:\\data.txt";
String outFile = inFile;
BufferedReader bs = null;
BufferedWriter bw = null;
try{
bs = new BufferedReader(new FileReader(new File(inFile)));
char[] ch = new char[3];
char c = 0;
char oc = 0;
Map<String, Integer> outData = new TreeMap<String, Integer>();
String key = "";
Integer sum = 0;
while (bs.read(ch) != -1) {
c = ch[0];
if(c == '*' || c == '#'){
oc = 0;
continue;
}
if(oc == c || (oc + 1) == c || (oc - 1) == c){
key = String.valueOf(oc) + String.valueOf(c);
sum = outData.get(key);
if(sum == null){
sum = 0;
}
outData.put(key, ++sum);
}
oc = c;
}
//bs.close();
//bs = null;
if(!outData.isEmpty()){
//true 追加 false不追加(至文件尾)
bw = new BufferedWriter(new FileWriter(new File(outFile), false));
for(Entry<String, Integer> entry: outData.entrySet()){
bw.write(entry.getKey() + ":" + entry.getValue() + "\r\n");
}
}
}finally{
if(bs != null)
bs.close();
if(bw != null)
bw.close();
}
}
[/code]
[code="java"]
public static void main(String[] args) throws Exception{
String inFile = "D:\demo.txt";
String outFile = "D:\out.txt";
BufferedReader bs = null;
BufferedWriter bw = null;
try{
bs = new BufferedReader(new FileReader(new File(inFile)));
ArrayList outData = new ArrayList();
Map<String,Integer>map=new HashMap<String, Integer>();
String line = null;
String outStr="";
//如果确定ID1一直是整数的活,可以用整数比,否则可以用字符串的equals比较
while((line = bs.readLine()) != null){
outData.add(line);
}
ArrayList<Integer>startList=new ArrayList<Integer>();//保存所有开始索引
ArrayList<Integer>endList=new ArrayList<Integer>();//保存所有结束索引
ArrayList<String> list = new ArrayList<String>();//存放所有的abc读取的数据
String tempArray="";
for (int i = 0; i < outData.size(); i++) {
// System.err.println("----\t"+outData.get(i));
//判断开始结束
if(outData.get(i).equals("*"))
{
startList.add(i);
}
if(outData.get(i).equals("#"))
{
endList.add(i);
}
tempArray+=outData.get(i);
}
System.err.println("tempArray "+tempArray);
//startList 索引集合=endList 索引集合 否则错误
for (int i = 0; i < startList.size(); i++) {
int m=startList.get(i);
int n=endList.get(i);
String temp=tempArray.substring(m,n);
System.err.println("i"+tempArray.substring(m,n));//打印所有出去*,#的字母
//要求统计 ab,ba aa 次数
if(temp.indexOf("ab")>=0)//存在ab
{
if(map.containsKey("ab"))//map 存在ab 获取value 中的 次数
{
int count=map.get("ab");
count=count+1;//累加
map.put("ab", count);
}else
{
map.put("ab", 1);
}
}
if(temp.indexOf("ba")>=0)//存在ba
{
if(map.containsKey("ba"))//map 存在ba 获取value 中的 次数
{
int count=map.get("ba");
count=count+1;//累加
map.put("ba", count);
}else
{
map.put("ba", 1);
}
}
if(temp.indexOf("aa")>=0)//存在ba
{
if(map.containsKey("aa"))//map 存在ba 获取value 中的 次数
{
int count=map.get("aa");
count=count+1;//累加
map.put("aa", count);
}else
{
map.put("aa", 1);
}
}
}
for (Map.Entry<String, Integer> map1: map.entrySet()) {
outStr+="letter:"+map1.getKey()+" count:"+map1.getValue()+"\r\n";
}
if(!outStr.isEmpty()){
bw = new BufferedWriter(new FileWriter(new File(outFile)));
bw.write(outStr);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(bs != null){
bs.close();
}
if(bw != null){
bw.close();
}
}
}
[/code]
测试通过,希望能帮上忙,不懂请留言
矩阵 那
a
a
b
c这个字符串怎么算?aa算一次相邻,ab算一次相邻
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class Counter {
public static void main(String[] args) throws Exception {
adjacentLetters();
}
public static void adjacentLetters() throws Exception {
String inFile = "D:\\data.txt";
BufferedReader bs = null;
try {
bs = new BufferedReader(new FileReader(new File(inFile)));
char ch = 0;
int n = 0;
List<String> cache = new ArrayList<String>();
StringBuffer buf = new StringBuffer();
Map<String, Integer> outData = new HashMap<String, Integer>();
boolean begin_f = false;
while ((n = bs.read()) != -1) {
ch = (char) n;
if (ch == '*') {
begin_f = true;
continue;
}
if (ch == '\n' || ch == '\r') {
continue;
}
if (ch == '#') {
begin_f = false;
String r = buf.toString();
if (r.length() > 0) {
cache.add(r);
}
continue;
}
if (begin_f) {
buf.append(ch);
}
}
for (String t : cache) {
System.out.println(t);
}
for (String t : cache) {
char o = (char) -1;
char oc = (char) -1;
for (char c : t.toCharArray()) {
if (oc == (char) -1) {
oc = c;
} else {
o = c;
String key = o + "" + oc;
Integer num = outData.get(key);
if (num == null) {
num = 0;
}
outData.put(key, ++num);
oc = o;
}
}
}
for (Entry<String, Integer> entry : outData.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
} finally {
if (bs != null)
bs.close();
}
}
}
Java写这个太烦了,用可嵌入JAVA的集算器(相当于Java的一些类库, 参考[url]http://blog.raqsoft.cn/?p=4792[/url]实现会简单许多:
A1=file("E:\s.txt").import@i()
A2=A1.select(~!="#").group@i(~=="*")
A3=A2.conj(~.([~[-1],~]).to(2,))
A4=A3.groups(~).new(~(1)+"="+string(count(~))+"次")
A5=file("E:\result.txt").export(A4)