java - 将字符串 [] 写入文件

标签 java android

我正在尝试将从一些动态创建的 EditText 中获得的一些值写入文件。EditText 位于一个数组列表中,我获取它们的值并将它们放入一个字符串 [] 中。 然后我需要将这些值放入这样的文件中:

产品[1]:数量[1]:价格[1]

product[2] : quantity[2] : price[2]

.....

产品[n]:数量[n]:价格[n]

我试过这个:

 private void writeToSDFile(){
    File root = android.os.Environment.getExternalStorageDirectory(); 
    totalc.append("\nExternal file system root: "+root);
    File dir = new File (root.getAbsolutePath() + "/download");
    dir.mkdirs();
    File file = new File(dir, "myData.txt");

    try {
        FileOutputStream f = new FileOutputStream(file);
        PrintWriter pw = new PrintWriter(f);
        pw.write("Total : ");
        pw.println(totaltest + price[1]);
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                " add a WRITE_EXTERNAL_STORAGE permission to the   manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }   
    totalc.append("\n\nFile written to "+file);
}

我尝试输入 price[1] 来测试它是否有效。 它在 LogCat 中给我以下错误:

06-13 21:23:43.525: E/AndroidRuntime(25345): FATAL EXCEPTION: main
06-13 21:23:43.525: E/AndroidRuntime(25345): java.lang.ArrayIndexOutOfBoundsException: length=0; index=1
06-13 21:23:43.525: E/AndroidRuntime(25345): at com.example.testlayout.MainActivity.writeToSDFile(MainActivity.java:89)

那么呢?如何将这些值发送到我的文件? 谢谢!

编辑: 这是我声明数组的位置和方式:

private String[] cant = new String[allcant.size()];
private String[] pret = new String[allpret.size()];
private String[] prod = new String[allpret.size()];

public void calculeaza() {

    totaltest = 0;

    String[] prod = new String[allprod.size()];
    for (int m = 0; m < allprod.size(); m++) {

        prod[m] = allcant.get(m).getText().toString();
        if (prod[m].matches("")) {
        //  Toast.makeText(this,
                //  "Ati omis cantitatea de pe pozitia " + (m + 1),
                //  Toast.LENGTH_SHORT).show();
            prod[m] = Float.toString(0);

        }
    }


    String[] cant = new String[allcant.size()];
    for (int j = 0; j < allcant.size(); j++) {

        cant[j] = allcant.get(j).getText().toString();
        if (cant[j].matches("")) {
        //  Toast.makeText(this,
                //  "Ati omis cantitatea de pe pozitia " + (j + 1),
                //  Toast.LENGTH_SHORT).show();
            cant[j] = Float.toString(0);

        }
    }

    String[] pret = new String[allpret.size()];
    for (int k = 0; k < allpret.size(); k++) {
        pret[k] = allpret.get(k).getText().toString();
        if (pret[k].matches("")) {
            //Toast.makeText(this,
            //      "Ati omis pretul de pe pozitia " + (k + 1),
            //      Toast.LENGTH_SHORT).show();
            pret[k] = Float.toString(0);

        }
    }

    for (int l = 0; l < allpret.size(); l++) {

        Float temp = Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]);
        alltotal.add(temp);
        totaltest = totaltest + temp;

        TextView totalf = (TextView) findViewById(R.id.total);
        totalf.setText(String.format("Total: %.2f", totaltest));




    }
}

问题可能是我声明了两次?一次在方法之外,一次在方法内部?但是如果我不在里面声明它,它就会崩溃。谁能告诉我为什么?

最佳答案

您遇到的错误表明您实际上正在访问 price[1] 但 price[] 的长度为 0,换句话说是空的。

关于java - 将字符串 [] 写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17072881/

相关文章:

java - Selenium Serenity 屏幕截图和电影延迟并挂起执行

java - SQL 到对象,多对多带字段

java - 如何制作最好的DTO请求类?

android - 2d 游戏物理,做对了

android:在显示之前具有条件延迟的可见性

java - 卸载 rxtxSerial.dll 时出现问题

java - 警告 : [overloads] method m1 is potentially ambiguous with method m2

java - TextView,不会设置文本

android - 覆盖 editTextStyle 不适用于最新的 Material Components 基本样式

android - 从android中的xml文件读取数组