我需要在文本文件中写入数字,然后我必须读取文件,最后对数字求和。我可以读写文件,但我不知道如何进行数学运算。
package clase.pkg13.de.septiembre;
import java.io.*;
public class FicheroTexto {
public static void main (String args[]) {
try {
PrintWriter salida = new PrintWriter ( new BufferedWriter(new FileWriter("C:\\Users\\Santiago\\Desktop\\prueba.txt")));
salida.println("1");
salida.println("2");
salida.close();
BufferedReader entrada = new BufferedReader(new FileReader("C:\\Users\\Santiago\\Desktop\\prueba.txt"));
String s, s2 = new String();
while((s= entrada.readLine()) !=null)
s2 = s2 + s + "\n";
System.out.println("Numbers:"+"\n"+s2);
entrada.close();
} catch (java.io.IOException e) {
// Do nothing
}
}
}