六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 80|回复: 0

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

[复制链接]

升级  46%

33

主题

33

主题

33

主题

秀才

Rank: 2

积分
119
 楼主| 发表于 2013-2-5 01:39:22 | 显示全部楼层 |阅读模式
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()+"是存在的");}elseSystem.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){}}*/}}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表