本文介绍了计算从文件数之和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个TextView的文件显示所有价格(例如£18.99£50等)的总和,目前它只是读取/显示了文件的最后价格。

这是我目前的code写入到文件:

  total.setText(total.getText());
        尝试{
            FOS的FileOutputStream = openFileOutput(TotalSavings,Context.MODE_PRIVATE);
            fos.write(total.getText()的toString()的getBytes());
            fos.close();
        }赶上(例外五){
            e.printStackTrace();
        }

这是我目前的code从文件读取(成员burmat提出了一些修改):

 公共无效savingstotalbutton(查看视图){        双总= 0;        尝试{
            的BufferedReader inputReader =新的BufferedReader(新的InputStreamReader(
                    openFileInput(TotalSavings)));
            字符串inputString;
            @燮pressWarnings(未使用)
            StringBuffer的StringBuffer的=新的StringBuffer();
            而((inputString = inputReader.readLine())!= NULL){                如果(inputString.length()大于0){
                串线= inputString.replaceAll([^ 0-9],);
                总=总+ Double.parseDouble(线);
                }
            }
            savingstotaltext.setText(将String.valueOf(总));
        }赶上(IOException异常五){
            e.printStackTrace();
        }
    }

任何帮助是AP preciated。

编辑:
我手动修改了TotalSavings.txt的内容,并添加不同的价格和复制的到/文件的应用程序文件夹中。它读取所有的价格,并给出了工作的总和,但问题是,写入功能覆盖第1行,它永远不会转到下一行。

编辑2:整code,它使用计算按钮,显示计算和结果写入文件TotalSavings.txt

 公共无效钙(查看视图){
        InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(),0);
        如果(price.getText()。的toString()。等于())
            返回;
        如果(disc.getText()。的toString()。等于())
            返回;
        双priceVal = Double.parseDouble(price.getText()的toString());
        双discVal = Double.parseDouble(disc.getText()的toString());
        双重优惠= priceVal / 100.0 * discVal;
        INT DI =(INT)(折扣* 100);
        双totalVal = priceVal - (DI / 100.0);
        的NumberFormat NF = NumberFormat.getCurrencyInstance(Locale.getDefault());
        savings.setText(nf.format(折扣));
        total.setText(nf.format(totalVal));        savings.setText(savings.getText());
        尝试{
            FOS的FileOutputStream = openFileOutput(TotalSavings,Context.MODE_PRIVATE);
            fos.write(savings.getText()的toString()的getBytes());
        fos.close();
        }赶上(例外五){
            e.printStackTrace();
        }    }


解决方案

回答你的问题写入文件是每一个你写你在写什么那里之前的文件时,你需要做什么就是变化

 的FileOutputStream FOS = openFileOutput(TotalSavings,Context.MODE_PRIVATE)

 的FileOutputStream FOS = openFileOutput(TotalSavings,Context.MODE_PRIVATE | Context.MODE_APPEND)

这告诉你的android要附加到文件以及保密。

修改
你的新问题,是因为你把文字直接在其他文本后,要强制一个新行到您的文件。最简单的方法将是这样的:

 字符串totalFromField = total.getText()的toString()+\\ n。
fos.write(totalFromField.getBytes());

I want to show the sum of all prices (e.g. £18.99 £50 etc.) from a file in a TextView, currently it just reads/shows the last price from the file.

This is my current code for writing to a file:

total.setText(total.getText());
        try {
            FileOutputStream fos = openFileOutput("TotalSavings", Context.MODE_PRIVATE);
            fos.write(total.getText().toString().getBytes());
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

This is my current code for reading from a file (member burmat suggested some changes):

public void savingstotalbutton(View view) {

        double total = 0;

        try {
            BufferedReader inputReader = new BufferedReader(new InputStreamReader(
                    openFileInput("TotalSavings")));
            String inputString;
            @SuppressWarnings("unused")
            StringBuffer stringBuffer = new StringBuffer();
            while ((inputString = inputReader.readLine()) != null) {

                if (inputString.length() > 0) {
                String line = inputString.replaceAll("[^0-9.]", "");
                total = total + Double.parseDouble(line);
                }
            }
            savingstotaltext.setText(String.valueOf(total));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Any help is appreciated.

Edit:I modified the contents of the TotalSavings.txt manually and added different prices and copied that to the /files folder of the App. It reads all the prices and gives the sum which works but the problem is that the write function overwrites the 1st line, it never goes to the next line.

Edit 2: The whole code which uses calc button to show calculation and the write the result to the file TotalSavings.txt

public void calc(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        if (price.getText().toString().equals(""))
            return;
        if (disc.getText().toString().equals(""))
            return;
        double priceVal = Double.parseDouble(price.getText().toString());
        double discVal = Double.parseDouble(disc.getText().toString());
        double discount = priceVal / 100.0 * discVal;
        int di = (int) (discount * 100);
        double totalVal = priceVal - (di / 100.0);
        NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.getDefault());
        savings.setText(nf.format(discount));
        total.setText(nf.format(totalVal));

        savings.setText(savings.getText());
        try {
            FileOutputStream fos = openFileOutput("TotalSavings", Context.MODE_PRIVATE);
            fos.write(savings.getText().toString().getBytes());
        fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
解决方案

The answer to your problem with writing to the file is every time you are writing to the file you are over writing what was there before, what you need to do is change

FileOutputStream fos = openFileOutput("TotalSavings", Context.MODE_PRIVATE)

to:

FileOutputStream fos = openFileOutput("TotalSavings", Context.MODE_PRIVATE | Context.MODE_APPEND)

This tells android you want to append to the file as well as keep it private.

EDITYou new issue is because you are placing the text directly after the other text, you want to force a new line into your file. The simplest way will be something like:

String totalFromField = total.getText().toString() + "\n";
fos.write(totalFromField.getBytes());

这篇关于计算从文件数之和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-23 02:03