TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
- import java.io.*;
- import java.util.*;
- public class TestPrintWriter {
- public static void main(String[] args)throws IOException {
- File file=new File("temp2.txt");
- if(file.exists()){
- System.out.println("File temp.txt already exists.");
- System.exit(0);
- }
-
- PrintWriter output=new PrintWriter(new FileWriter(file));
-
- for(int i=0;i<10;i++){
- output.print((int)(Math.random()*100)+" ");
- }
-
- output.close();
- BufferedReader input=new BufferedReader(new FileReader("temp2.txt"));
-
- int total=0;
- String line;
- while((line=input.readLine())!=null){
- StringTokenizer tokens=new StringTokenizer(line);
- while(tokens.hasMoreTokens())
- total+=Integer.parseInt(tokens.nextToken());
- }
-
- output=new PrintWriter(new FileWriter(file,true));
- output.printf("
- ");
- output.printf("Total is %d ",total);
- output.close();
- }
- }
-
- 运行后,temp2.txt的内容:
复制代码 85 10 92 59 71 87 2 95 31 73 Total is 605
源码下载:http://file.javaxxz.com/2014/11/5/235846187.zip |
|