一个文件读取并运算的问题

Tatum_99999
博客专家认证
2014-12-05 01:05:17
本人初学java,遇到一个没弄出结果的问题,这里请教一下。问题是这样的:

D盘有一个文件aaa.txt.txt,里边的内容是“1,-,4,+,3”(没有引号),现在要在eclipse中读取这个文件并算出结果。

我只能把它读出来,无法运算出来。
...全文
208 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
tony4geek 2014-12-05
  • 打赏
  • 举报
回复
重新改个名字在试试看看呢?
shixitong 2014-12-05
  • 打赏
  • 举报
回复
换个其它的文件名试试
Tatum_99999 2014-12-05
  • 打赏
  • 举报
回复
非常感谢!但是提示找不到文件,我仔细看了,确实是在D盘,而且是直接在D盘的文件:aaa.txt.txt;包括查看属性时的地址都是“aaa.txt.txt “不知道为什么就说找不到文件
shixitong 2014-12-05
  • 打赏
  • 举报
回复
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;

public class Test1 {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		//String str = "1,-,4,+,3,-,9";
		String path="D:" + File.separator + "aaa.txt.txt";
		String str = readFile(path);
		if(!str.equals("")){
			String[] strArray = str.split(",");
			System.out.println("获得计算的结果为:"+getCalcResult(strArray));
		}
		
	}

	public static String getCalcResult(String[] strArray) {
		String result = "";
		BigDecimal bg = new BigDecimal(0);
		for (int i = 0; i < strArray.length; i++) {
			String strTmp = strArray[i];
			if (strTmp.equals("+")) {
				bg = bg.add(new BigDecimal(strArray[i + 1]));
				i++;
			} else if (strTmp.equals("-")) {
				bg = bg.subtract(new BigDecimal(strArray[i + 1]));
				i++;
			} else if (strTmp.equals("*")) {
				bg = bg.multiply(new BigDecimal(strArray[i + 1]));
				i++;
			} else if (strTmp.equals("/")) {
				bg = bg.divide(new BigDecimal(strArray[i + 1]));
				i++;
			} else {
				bg = bg.add(new BigDecimal(strTmp));
			}

		}
		result = String.valueOf(bg.intValue());
		return result;
	}

	/**
	 * 读文件
	 * @return
	 * @throws IOException
	 */
	public static String readFile(String path) throws IOException {
		String content="";
		BufferedReader br=null;
		try {
			File file = new File(path);
			br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			content=br.readLine();
		} catch (Exception e) {
			e.printStackTrace();
		}
		finally{
			br.close();
		}
		return content;
	}

}
Tatum_99999 2014-12-05
  • 打赏
  • 举报
回复
我刚试了一下,可是不知为何连文件都输不出来了,打桩输出的都是文件地址 package lianxi; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; public class Lianxi { public static void main(String[] args) { try { File file=new File("D:"+File.separator+"aaa.txt.txt"); System.out.println(file); FileInputStream fis=new FileInputStream(file); System.out.println(fis); InputStreamReader isr=new InputStreamReader(fis); System.out.println(isr); BufferedReader br=new BufferedReader(isr); System.out.println(br.readLine()); } catch (Exception e) { // TODO Auto-generated catch block //e.printStackTrace(); } } } 几次输出结果都是:D:\aaa.txt.txt,请问这是什么情况?多谢!
Tatum_99999 2014-12-05
  • 打赏
  • 举报
回复
我刚试了一下,可是不知为何连文件都输不出来了,打桩输出的都是文件地址 package lianxi; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; public class Lianxi { public static void main(String[] args) { try { File file=new File("D:"+File.separator+"aaa.txt.txt"); System.out.println(file); FileInputStream fis=new FileInputStream(file); System.out.println(fis); InputStreamReader isr=new InputStreamReader(fis); System.out.println(isr); BufferedReader br=new BufferedReader(isr); System.out.println(br.readLine()); } catch (Exception e) { // TODO Auto-generated catch block //e.printStackTrace(); } } } 几次输出结果都是:D:\aaa.txt.txt,请问这是什么情况?多谢!
Tatum_99999 2014-12-05
  • 打赏
  • 举报
回复
我刚试了一下,可是不知为何连文件都输不出来了,打桩输出的都是文件地址 package lianxi; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; public class Lianxi { public static void main(String[] args) { try { File file=new File("D:"+File.separator+"aaa.txt.txt"); System.out.println(file); FileInputStream fis=new FileInputStream(file); System.out.println(fis); InputStreamReader isr=new InputStreamReader(fis); System.out.println(isr); BufferedReader br=new BufferedReader(isr); System.out.println(br.readLine()); } catch (Exception e) { // TODO Auto-generated catch block //e.printStackTrace(); } } } 几次输出结果都是:D:\aaa.txt.txt,请问这是什么情况?多谢!
qzw1210 2014-12-05
  • 打赏
  • 举报
回复
第一种方法:i=Integer.parseInt(s); 第二种方法:i=Integer.valueOf(s).intValue();
Tatum_99999 2014-12-05
  • 打赏
  • 举报
回复
就是不知道怎么转换,读取的是字符串,怎样转换成int类型或者integer呢?
qzw1210 2014-12-05
  • 打赏
  • 举报
回复
看看源码,读取的时候,转换下数据类型,进行运算。

67,549

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧