import java.io.*;
public class sum {
public static void Sum(String s1,String s2,String s3){
try{
int num=0,temp=0,ch;
FileWriter w = new FileWriter(s2, true);
FileReader r = new FileReader(s1);
while((ch=r.read())!=-1){
switch((char)ch){
case '1':temp = temp*10+1;break;
case '2':temp = temp*10+2;break;
case '3':temp = temp*10+3;break;
case '4':temp = temp*10+4;break;
case '5':temp = temp*10+5;break;
case '6':temp = temp*10+6;break;
case '7':temp = temp*10+7;break;
case '8':temp = temp*10+8;break;
case '9':temp = temp*10+9;break;
case '0':temp = temp*10+0;break;
default:{
num+=temp;
temp = 0;
break;
}
}
}
//System.out.println(num);
w.write(s3);
w.write(num);//
问题就出在这里
w.close();
}catch (Exception e){
e.printStackTrace();
}
}
public static void main(String[] args){
String read_path_import = "import.txt";
String read_path_comment = "comment.txt";
String read_path_like = "like.txt";
String write_path = "sum.txt";
String tips_import = ":";
String tips_comment = ":";
String tips_like = ":";
Sum(read_path_import,write_path,tips_import);
Sum(read_path_comment,write_path,tips_comment);
Sum(read_path_like,write_path,tips_like);
}
}
public class sum {
}
原因出在这里:
FileWriter从类OutputStreamWriter继承的
1、public void write(int c)
c - 指定要写入字符的ASCII。
这里data数组中的整数是作为字符的ASCII,最终显示的是ASCII值为这些的字符,并不是这些数字本身。要显示这些数字本身,可改为:
fos.write(String.valueOf(data[i]));
注意valueOf()方法的大小写。