遍列出文本文档里的数字并求和

package ceshi;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;

public class FileTest {
	public static void main(String[] args) {
		Scanner scanner = null;
        try {
            scanner = new Scanner(new File("C:\\Documents and Settings\\tliu\\桌面\\dd.txt"));
            int text = 0;
            while (scanner.hasNextInt()) {
            	text += scanner.nextInt();
            }
            System.out.println(text);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            scanner.close();
        }
        
        /*
		File file = new File("C:\\Documents and Settings\\tliu\\桌面\\dd.txt");
		if(file.exists()){
			System.out.println(file.getName()+"是存在的");
		}else
			System.out.println(file.getName()+"是不存在的");
		if(file.isFile()){
			try{
				BufferedReader input = new BufferedReader(new FileReader(file));
				StringBuffer buffer = new StringBuffer();
				String text = null;
				while((text = input.readLine()) != null)
					buffer.append(text+",");
				String str = buffer.toString();
				String[] s = str.split(",");
				int i = 0;
				for(String k : s){
					i += Integer.parseInt(k);
				}
				System.out.println(i);
			}catch(IOException e){
				
			}
		}*/
	}
}

你可能感兴趣的:(java,C++,c,C#)