FileWriter创建空文件

时间:2014-02-16 08:47:55

标签: java android filewriter android-file

我在写入文件时遇到了这个问题,我创建了文件,但它是空的。这是我使用的函数 - 我没有在logCat中得到任何错误,函数从头到尾执行没有任何问题,但文件是空的。任何人都可以帮助我并告诉我为什么这个文件是空的?

这是我使用的功能:

public void exportDataInCSV(final String file_name,
            final ArrayList<Assessment> assessments, final boolean dropBox)
            throws IOException {

        final File folder = new File(Environment.getExternalStorageDirectory()
                + "/Psychology Assessment Exports");

        boolean var = false;
        if (!folder.exists())
            var = folder.mkdir();

        final String filename = folder.toString() + "/" + file_name + ".csv";

        // show waiting screen
        // CharSequence contentTitle = getString(R.string.app_name);

        // final ProgressDialog progDailog = ProgressDialog.show(
        // DataExport.this, contentTitle, "Exporting data...",
        // true);// please
        // // wait
        try {
            FileWriter fw = new FileWriter(filename);

            fw.append("ChildID");
            fw.append(',');

            fw.append("ParentName");
            fw.append(',');

            fw.append("ChildName");
            fw.append(',');

            fw.append("ChildDOB");
            fw.append(',');

            fw.append("ChildGender");
            fw.append(',');

            fw.append("AssessmentID");
            fw.append(',');

            // AssessmentType DoctorName AssessmentDate Q1 Q2
            fw.append("AssessmentType");
            fw.append(',');

            fw.append("DoctorName");
            fw.append(',');

            fw.append("AssessmentDate");
            fw.append(',');

            // tscyc start here
            for (int n = 0; n < 90; n++) {
                fw.append("Q" + (n + 1));
                fw.append(',');
            }

            fw.append("R_RL");
            fw.append(',');
            fw.append("R_ATR");
            fw.append(',');
            fw.append("R_ANX");
            fw.append(',');
            fw.append("R_DEP");
            fw.append(',');
            fw.append("R_ANG");
            fw.append(',');
            fw.append("R_PTS-I");
            fw.append(',');
            fw.append("R_PTS-AV");
            fw.append(',');
            fw.append("R_PTS-AR");
            fw.append(',');
            fw.append("R_PTS-TOT");
            fw.append(',');
            fw.append("R_DIS");
            fw.append(',');
            fw.append("R_SC");
            fw.append(',');

            fw.append("RL");
            fw.append(",");
            fw.append("ATR");
            fw.append(",");
            fw.append("Anxiety");
            fw.append(",");
            fw.append("Depression");
            fw.append(",");
            fw.append("Anger");
            fw.append(",");
            fw.append("PTSD-Intrusion");
            fw.append(",");
            fw.append("PTSD-Avoidance");
            fw.append(",");
            fw.append("PTSD-Arousal");
            fw.append(",");
            fw.append("PTSD-Total");
            fw.append(",");
            fw.append("Dissociation");
            fw.append(",");
            fw.append("Sexual Concerns");
            fw.append(",");
            fw.append("Group");
            fw.append('\n');

            //
            ChildRecord child;
            int gender = -1;
            db = new DatabaseHelper(DataExport.this);
            db.openDB();
            for (int i = 0; i < assessments.size(); i++) {
                if (assessments.get(i).getQuestions().size() > 55) {

                    child = db.getChild(assessments.get(i).getChildID());
                    Log.v("--", child.getName());
                    fw.append(assessments.get(i).getChildID() + "");
                    fw.append(",");
                    fw.append(child.getParent1());
                    fw.append(",");
                    fw.append(child.getName());
                    fw.append(",");
                    fw.append(child.getDob());
                    fw.append(",");
                    gender = child.getGender();
                    switch (gender) {
                    case 1:
                        fw.append("Female");
                        break;
                    case 2:
                        fw.append("Male");
                        break;
                    }

                    fw.append(",");
                    fw.append(assessments.get(i).getId() + "");

                    fw.append(",");
                    fw.append("TSCYC");// edit later//add option for //
                                        // types
                    fw.append(",");

                    fw.append(db.getDoctorNameById(assessments.get(i)
                            .getDoctorID()));
                    fw.append(",");
                    fw.append(assessments.get(i).getDate());
                    fw.append(",");
                    int RL = 0;

                    ArrayList<Question> questions = new ArrayList<Question>();
                    for (int n = 0; n < assessments.get(i).getQuestions()
                            .size(); n++) {

                        questions = assessments.get(i).getQuestions();
                        int answer = questions.get(n).getAnswer();
                        // if (answer == 1) {
                        // RL++;
                        // }
                        fw.append(answer + "");
                        fw.append(",");
                    }// 3, 14, 22, 53, 66, 73, 83, 86, and 89
                    int[] RLArray = { 2, 13, 21, 52, 65, 72, 82, 85, 88 };
                    for (int n = 0; n < RLArray.length; n++) {
                        if (questions.get(RLArray[n]).getAnswer() == 1) {
                            RL++;
                        }
                    }
                    Log.v("--", "1!");
                    fw.append(RL + "");
                    fw.append(",");
                    int ATR = questions.get(8).getAnswer()
                            + questions.get(29).getAnswer()
                            + questions.get(36).getAnswer()
                            + questions.get(39).getAnswer()
                            + questions.get(50).getAnswer()
                            + questions.get(59).getAnswer()
                            + questions.get(63).getAnswer()
                            + questions.get(76).getAnswer()
                            + questions.get(78).getAnswer();
                    fw.append(ATR + "");
                    fw.append(",");

                    // Q7+Q21+Q31+Q32+Q42+Q44+Q57+Q67+Q76
                    int anxiety = questions.get(6).getAnswer()
                            + questions.get(20).getAnswer()
                            + questions.get(30).getAnswer()
                            + questions.get(31).getAnswer()
                            + questions.get(41).getAnswer()
                            + questions.get(43).getAnswer()
                            + questions.get(56).getAnswer()
                            + questions.get(66).getAnswer()
                            + questions.get(75).getAnswer();
                    fw.append(anxiety + "");
                    fw.append(",");
                    // Q2+Q18+Q41+Q54+Q61+Q68+Q71+Q84+Q88
                    int depression = questions.get(1).getAnswer()
                            + questions.get(17).getAnswer()
                            + questions.get(40).getAnswer()
                            + questions.get(53).getAnswer()
                            + questions.get(60).getAnswer()
                            + questions.get(67).getAnswer()
                            + questions.get(70).getAnswer()
                            + questions.get(83).getAnswer()
                            + questions.get(87).getAnswer();
                    fw.append(depression + "");
                    fw.append(",");
                    // Q1+Q15+Q23+Q34+Q43+Q58+Q62+Q87+Q90
                    int anger = questions.get(0).getAnswer()
                            + questions.get(14).getAnswer()
                            + questions.get(22).getAnswer()
                            + questions.get(33).getAnswer()
                            + questions.get(42).getAnswer()
                            + questions.get(57).getAnswer()
                            + questions.get(61).getAnswer()
                            + questions.get(86).getAnswer()
                            + questions.get(89).getAnswer();
                    fw.append(anger + "");
                    fw.append(",");
                    // Q4+Q11+Q19+Q24+Q27+Q36+Q63+Q69+Q80
                    int intrusion = questions.get(3).getAnswer()
                            + questions.get(10).getAnswer()
                            + questions.get(18).getAnswer()
                            + questions.get(23).getAnswer()
                            + questions.get(26).getAnswer()
                            + questions.get(35).getAnswer()
                            + questions.get(62).getAnswer()
                            + questions.get(68).getAnswer()
                            + questions.get(79).getAnswer();
                    fw.append(intrusion + "");
                    fw.append(",");
                    // Q8+Q13+Q29+Q39+Q49+Q55+Q70+Q72+Q81
                    int avoidance = questions.get(7).getAnswer()
                            + questions.get(12).getAnswer()
                            + questions.get(28).getAnswer()
                            + questions.get(38).getAnswer()
                            + questions.get(48).getAnswer()
                            + questions.get(54).getAnswer()
                            + questions.get(69).getAnswer()
                            + questions.get(71).getAnswer()
                            + questions.get(80).getAnswer();
                    fw.append(avoidance + "");
                    fw.append(",");
                    // Q10+Q17+Q26+Q45+Q47+Q48+Q56+Q74+Q82
                    // Q10+Q17+Q26+Q45+Q47+Q48+Q56+Q74+Q82
                    Log.v("--", "2!");
                    int arousal = questions.get(9).getAnswer()
                            + questions.get(16).getAnswer()
                            + questions.get(25).getAnswer()
                            + questions.get(44).getAnswer()
                            + questions.get(46).getAnswer()
                            + questions.get(47).getAnswer()
                            + questions.get(55).getAnswer()
                            + questions.get(73).getAnswer()
                            + questions.get(81).getAnswer();
                    fw.append(arousal + "");
                    fw.append(",");

                    int total = intrusion + avoidance + arousal;
                    fw.append(total + "");
                    fw.append(",");
                    int dissociation = questions.get(4).getAnswer()
                            + questions.get(24).getAnswer()
                            + questions.get(27).getAnswer()
                            + questions.get(32).getAnswer()
                            + questions.get(37).getAnswer()
                            + questions.get(45).getAnswer()
                            + questions.get(51).getAnswer()
                            + questions.get(77).getAnswer()
                            + questions.get(84).getAnswer();
                    fw.append(dissociation + "");
                    fw.append(",");

                    // Q6+Q12+Q16+Q20+Q35+Q50+Q59+Q65+Q75
                    int sexualConcerns = questions.get(5).getAnswer()
                            + questions.get(11).getAnswer()
                            + questions.get(15).getAnswer()
                            + questions.get(19).getAnswer()
                            + questions.get(34).getAnswer()
                            + questions.get(49).getAnswer()
                            + questions.get(58).getAnswer()
                            + questions.get(64).getAnswer()
                            + questions.get(74).getAnswer();
                    fw.append(sexualConcerns + "");
                    fw.append(",");
                    if (!db.isDBOpen())
                        db.openDB();
                    int group = child.getGroup();
                    fw.append(db.getTscore(group, "RL", RL) + "");
                    fw.append(",");
                    fw.append(db.getTscore(group, "ATR", ATR) + "");
                    fw.append(",");
                    fw.append(db.getTscore(group, "ANX", anxiety) + "");
                    fw.append(",");
                    fw.append(db.getTscore(group, "DEP", depression) + "");
                    fw.append(",");
                    fw.append(db.getTscore(group, "ANG", anger) + "");
                    fw.append(",");
                    Log.v("--", "3!");
                    fw.append(db.getTscore(group, "PTS-I", intrusion) + "");
                    fw.append(",");
                    fw.append(db.getTscore(group, "PTS-AV", avoidance) + "");
                    fw.append(",");
                    fw.append(db.getTscore(group, "PTS-AR", arousal) + "");
                    fw.append(",");
                    fw.append(db.getTscore(group, "PTS-TOT", total) + "");
                    fw.append(",");
                    fw.append(db.getTscore(group, "DIS", dissociation) + "");
                    fw.append(",");
                    fw.append(db.getTscore(group, "SC", sexualConcerns) + "");
                    fw.append(",");
                    Log.v("--", "4! "+ db.getTscore(group, "SC", sexualConcerns));
                    fw.append(group + "");
                    fw.append("\n");
                    // tscyc code ends here
                    Log.v("--", "TSCYC finish*");
                }
                db.closeDB();
            }
            fw.close();
        } catch (Exception e) {
        }

        // progDailog.dismiss();

        if (dropBox) {
            if (!session.isLinked())
                mDBApi.getSession().startAuthentication(DataExport.this);

            fileToDropbox(file_name);
        }

    }

0 个答案:

没有答案