请高手帮忙编个简单的java或者c的小程序。数数字的个数

来源:百度知道 编辑:UC知道 时间:2024/06/09 18:27:22
例如,我的文件是这样的
1
1
2
2
2
2
2
2
3
4
4
4
5
5
5
5
…………
2个1,6个2,1个3,3个4,4个5……
当然,我的文件中并不是1,2,3,4,5这些数字,而是一行一行的字符,我这是大概说个意思。
然后得到的输出结果是2,6,1,3,4,…….保存在一个txt文件中。

谢谢啦~~~

import java.io.*;

class Test1
{
public static void main(String[] args) throws IOException
{
FileReader fr = new FileReader("c:\\test.txt");
BufferedReader br = new BufferedReader(fr);

//File wf = new File("c:\\","result.txt");
FileWriter fw = new FileWriter("c:\\result.txt");
PrintWriter pw = new PrintWriter(fw);

String linesame = br.readLine();
String line = br.readLine();
int samestr = 1;
while(line != null)
{
if(!line.equals(linesame))
{
pw.write(String.valueOf(samestr));
pw.println();
pw.flush();
linesame = line;
System.out.print(String.valueOf(samestr)+'\n');
samestr=1;
}
else