使用IF逻辑inbeded创建文件

时间:2013-05-02 01:48:16

标签: java android file-io

实际上有两个问题:

主要问题: 我希望你写出一个文件不会有简单的逻辑,这是不对的! 我有一个光标在通常由onCreate调用的屏幕上加载动态列表。这样可行。现在,我需要写出" sdCard"作为备份。 (如果我的手表决定重置为第一天,我会重新加载 - 它还允许我从我的带有键盘的PC上添加条目。)

我决定最好的方法是调用现有的光标,但设置一个开关来指示写出来。文件需要try-catch,所以我将它放在open,write和close之间。 "作家"未定义。所以我把它全部放在一个" TRY"如果没有括号,那就有效 - 没有" IF" s。

但添加" IF(--SWITCH SET)" {--- writer.write(strBuRec); ..}"这需要{ - }现在再次写作是未定义的。

我当然希望我做错其他事情(可能是愚蠢的事情)!我可以将代码复制到第二个游标中,但不愿意。

第二个问题: 请注意,关闭游标(//cursor.close();)已注释掉。这是因为如果重新绘制屏幕或在这种情况下,重新调用光标写出我的文件,我将光标关闭。如果我关闭它,我只能加载一次光标。

注意:这是我的WIMMOne的一个简单的应用程序,所以它需要版本7.这段代码是一个片段,(错误的决定,但它在那里)。

非常感谢, 克拉克

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor)
   {
    Log.d("EventLst","0 LoadFin");
    int iRecNo = 0;
    iBuCnt = 0;
    mAdapter.swapCursor(cursor);

         //----------------------------------------
         // if exporting, open the file

    try 
       {
        if (strRunBu == "Y")
           {
            FileWriter writer;
            String path = Environment.getExternalStorageDirectory().getAbsoluteFile() + "/Event";
            File dir = new File(path);   
            Log.d("Eventfile","00 File:"  + dir);
            File flEvent = new File(dir, "EVENT.TXT");
            boolean canIWrite = dir.canWrite();

            Log.d("Eventfile","0 File:" + flEvent + "=" + canIWrite);
            flEvent.createNewFile();                           
            Log.d("Eventfile","1 File:" + flEvent);
            writer = new FileWriter(flEvent);
           }

             // ------------------------------------------
             // Insert dummy first record to serve as a label
             // 
        String strBuRec = "";
        strRecord.clear();
        strRecord.add(0, "mm-dd-yy: Event name");
        cursor.moveToFirst();

        Log.d("EventLst","1 LoadFin DO");
             // ----------------------------------------
             // Read from cursor and add each record to list 
         while (cursor.isAfterLast() == false)
           {
            iRecNo = iRecNo + 1;
                 // - Table has 4 columns, read them into string array: strC
            String strC[] = { (cursor.getString(0)), (cursor.getString(1)),
                                  (cursor.getString(2)), (cursor.getString(3))
                            };
                 // - The fourth column is the date/time in milliseconds since
                 // January 1,1970
                 // convert to date in yyyy-mm-dd format
            String strDateMil = (cursor.getString(3));
            long lgDate = cursor.getLong(3);
            Log.d("EventLst","4 LoadCSR:" + "I:" + iRecNo + "Ld:" + lgDate);
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd-yy");
            String strDate = dateFormat.format(new Date(lgDate));

                 // - Concatenate date and event into one string, add to table
            strRecord.add(iRecNo, strDate + ": " + strC[2]);

                 // - save record number for each event in strRecId
                 // - Records are sorted by date, so we need to save RowId to pass
                 // - to edit screen
            strRecId.add((cursor.getString(0)));

                 //---------------------------
                 // if-creating export file, write a record
            if (strRunBu == "Y")
               {
                dateFormat = new SimpleDateFormat("HH:mm");
                String strTime = dateFormat.format(new Date(lgDate));

                strBuRec = ( (cursor.getString(1)) + "," + (cursor.getString(2))
                              + "," + strDate + "," + strTime + "\r\n" );
                Log.d("EventLst","4 LoadCSR:" + "BU:" + strBuRec); 

                       // ERROR:  writer cannot be resolved   ??????????
                writer.write(strBuRec); 
                Log.d("Eventfile","4 File:" + "wrote");
               }

            strEventRec.add(iBuCnt, strBuRec);
            iBuCnt = iBuCnt + 1;

            cursor.moveToNext();
         }    // ----end of while loop
            //------------------------------------
            //  COULD NOT CLOSE THE CURSOR?????
            //cursor.close();
            //------------------------------------
        if (strRunBu == "Y")
           {
            // ERROR:  writer cannot be resolved  ???????????
            writer.flush();
            // ERROR:  writer cannot be resolved  ???????????
            writer.close();
           };
       } //---> BACKTO try 
       catch (IOException e)
          {
           Toast.makeText(getActivity(), "Close ER"+ e,
           Toast.LENGTH_SHORT).show();
          }
      Log.d("Eventfile","4 File:" + "Closed");
      strRunBu = "N";

      lstAdapter = new ArrayAdapter<String>(getActivity(),
                   R.layout.event_row, R.id.text1, strRecord);

         // * Call to SetListAdapter()informs ListFragment how to fill ListView
         // * here use ArrayAdapter
    setListAdapter(lstAdapter);
         // Log.d("EventLst","8 LoadCSR:" + "ALLDONE");

   }

1 个答案:

答案 0 :(得分:1)

{}定义范围 - 您需要确保在正确的范围内定义变量。即不在if范围内,而是包含if

的范围

另外,对我来说,就像你在try-catch块中有太多了一样!

另外:此行if (strRunBu == "Y")有一个经典的新手错误,即将字符串与==进行比较 - 请改用if (strRunBu.equals("Y"))