-5

实际上我正在开发一个应用程序,当应用程序第一次运行时,它将创建一个 txt 文件,如果文件已经存在,那么它将简单地将其写入文件。
但我不知道如何以编程方式检查该文件是否存在。我试过了,

try  
    {  
        FPartyCreation = new File ("/data/data/com.MediExcel/files/","dbPartyCreation.dat");  
        if (FPartyCreation.exists())  
        {  
            writeOnFile();
        }  
        else  
        {  
            FPartyCreation.createNewFile();  
            writeOnFile();
        }  
    }  
    catch (IOException ioe)  
    {  
        ioe.printStackTrace();  
    }  

但它给出了以下错误,并且文件也没有在同一路径上创建。

07-06 01:39:26.183: E/AndroidRuntime(23107): FATAL EXCEPTION: main
07-06 01:39:26.183: E/AndroidRuntime(23107): java.lang.NullPointerException
07-06 01:39:26.183: E/AndroidRuntime(23107):    at com.MediExcel.AddMethodPartyCreation.getMethod(AddMethodPartyCreation.java:28)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at com.MediExcel.PartyCreationActivity$1.onClick(PartyCreationActivity.java:64)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at android.view.View.performClick(View.java:3511)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at android.view.View$PerformClick.run(View.java:14105)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at android.os.Handler.handleCallback(Handler.java:605)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at android.os.Looper.loop(Looper.java:137)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at android.app.ActivityThread.main(ActivityThread.java:4424)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at java.lang.reflect.Method.invokeNative(Native Method)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at java.lang.reflect.Method.invoke(Method.java:511)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-06 01:39:26.183: E/AndroidRuntime(23107):    at dalvik.system.NativeStart.main(Native Method)

请帮我

谢谢

4

1 回答 1

1

创建 SQLite 数据库 -

public class DatabaseHelper extends SQLiteOpenHelper {

static final String dbName="demoDB";
static final String employeeTable="Employees";
static final String colID="EmployeeID";
static final String colName="EmployeeName";
static final String colAge="Age";
static final String colDept="Dept";

static final String deptTable="Dept";
static final String colDeptID="DeptID";
static final String colDeptName="DeptName";

static final String viewEmps="ViewEmps";

创建数据库

public void onCreate(SQLiteDatabase db) 
{
    // TODO Auto-generated method stub

    db.execSQL("CREATE TABLE "+deptTable+" ("+colDeptID+ " INTEGER PRIMARY KEY , "+
    colDeptName+ " TEXT)");

  db.execSQL("CREATE TABLE "+employeeTable+" 
    ("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+
        colName+" TEXT, "+colAge+" Integer, "+colDept+" 
    INTEGER NOT NULL ,FOREIGN KEY ("+colDept+") REFERENCES 
    "+deptTable+" ("+colDeptID+"));");


  db.execSQL("CREATE TRIGGER fk_empdept_deptid " +
    " BEFORE INSERT "+
    " ON "+employeeTable+

    " FOR EACH ROW BEGIN"+
    " SELECT CASE WHEN ((SELECT "+colDeptID+" FROM "+deptTable+" 
    WHERE "+colDeptID+"=new."+colDept+" ) IS NULL)"+
    " THEN RAISE (ABORT,'Foreign Key Violation') END;"+
    "  END;");

  db.execSQL("CREATE VIEW "+viewEmps+
    " AS SELECT "+employeeTable+"."+colID+" AS _id,"+
    " "+employeeTable+"."+colName+","+
    " "+employeeTable+"."+colAge+","+
    " "+deptTable+"."+colDeptName+""+
    " FROM "+employeeTable+" JOIN "+deptTable+
    " ON "+employeeTable+"."+colDept+" ="+deptTable+"."+colDeptID
    );
  //Inserts pre-defined departments
  InsertDepts(db);  
 }

看看这个Using-SQLite-Database-with-Android博客。希望这个博客足以满足您的查询。

于 2012-07-04T09:34:49.393 回答