我正在尝试以下代码:
http://androidgenuine.com/?tag=export-database-Android
public void exportdata()
{
try
{
myInput = new FileInputStream("/data/data/com.example.hello/databases/BikeMaintenance.db");
File directory = new File("/sdcard/BikeMaintenance/Data/");
// Create the folder if it doesn't exist:
if (!directory.exists())
{
directory.mkdirs();
}
OutputStream myOutput = new FileOutputStream(directory.getPath()+"/BikeMaintenance.backup");
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0)
{
myOutput.write(buffer, 0, length);
msg="Backup Succesfull!";
Toast.makeText(MainPage.this,msg,Toast.LENGTH_SHORT).show();
}
// Close and clear the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
catch (FileNotFoundException e)
{
msg="FileNotFoundException";
Toast.makeText(MainPage.this,msg,Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
catch (IOException e)
{
msg="Backup Unsuccesfull!";
Toast.makeText(MainPage.this,msg,Toast.LENGTH_SHORT).show();
}
}
java.io.FileNotFoundException: /data/data/com.example.hello/databases/BikeMaintenance.db: open failed: ENOENT (No such file or directory) at libcore.io.IoBridge.open(IoBridge.java:416) at java.io.FileInputStream.(FileInputStream.java:78) at java.io.FileInputStream.(FileInputStream.java:105) at com.example.hello.MainPage.exportdata(MainPage.java:625) at com.example.hello.MainPage.backup(MainPage.java:692) at com.example.hello.MainPage$1.onClick(MainPage.java:494) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4745) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) at dalvik.system.NativeStart.main(Native Method) Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) at libcore.io.Posix.open(Native Method) at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) at libcore.io.IoBridge.open(IoBridge.java:400) ... 14 more
您的代码在我看来不错,但我认为您没有访问该.db文件的权限,您可以使用DataBaseHelper类访问您的.db文件
public class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.android.electricbillgenerator/databases/";
private static String DB_NAME = "BikeMaintenance.db";
private SQLiteDatabase myDataBase;
private Context myContext;
public static final String COLUMN_ID = "_id";
public static final String COLUMN_USERNAME = "UserName";
public static final String COLUMN_PASSWORD = "Password";
public static final String TABLE_NAME = "Login";
private static final String DATABASE_NAME = "BikeMaintenance.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
// private static final String DATABASE_CREATE = "create table "
// + TABLE_NAME + "(" + COLUMN_ID
// + "," + COLUMN_USERNAME
// + "," + COLUMN_PASSWORD+ ");";
// private static final String DATABASE_CREATE = "create table "
// + TABLE_NAME + "(" + COLUMN_ID
// + " integer primary key autoincrement, " + COLUMN_USERNAME
// + " text not null," + COLUMN_PASSWORD+ "text not null);";
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, 8);
this.myContext = context;
boolean dbexist = checkDataBase();
if (dbexist) {
//System.out.println("Database exists");
openDataBase();
} else {
System.out.println("Database doesn't exist");
try {
createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void onCreate(SQLiteDatabase database) {
// try {
// database.execSQL(DATABASE_CREATE);
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Log.w(DataBaseHelper.class.getName(),
// "Upgrading database from version " + oldVersion + " to "
// + newVersion + ", which will destroy all old data");
// db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// onCreate(db);
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn't
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
}
我有一个FileInputStream。我希望从中读取面向字符的行数据,直到找到特定的分隔符。然后我想将FileInputStream(当前位置设置在分隔符行末尾之后)传递给需要InputStream的库。 我可以使用BufferedReader一次遍历一行文件,一切都很好。但是,这会将底层文件流保留在 在一个不确定的位置——BufferedReader必须向前看,我不知道有多远,而且没有办法告诉
bufferedinputstream(BIS)比FileInputStream(FIS)快的原因请参见“为什么使用bufferedinputstream逐字节读取文件比使用FileInputStream快?”?是吗 使用BufferedInputStream,该方法委托给重载read()方法,该方法读取8192个字节,并在FIS读取单个字节时缓冲它们,直到需要它们 据我所知,磁盘是一个“块设备”
我正在尝试访问jar中包含的文件。 我的代码开头如下: 我发现以下错误: 查找URL或初始化文件似乎没有问题,但我无法让FileInputStream正常工作。有人知道我做错了什么吗?我们将不胜感激。
我不熟悉随机文件访问,我遇到了一个问题——据我所知,RandomAccessFile类提供了一个用于读/写的随机访问文件。我可以使用seek()方法移动到首选位置并开始阅读或写作,但在这种情况下并不重要。完全是随机访问吗?但在FileInputStream中,我有同样的能力 这种方法提供了我从某个特定的地方阅读。那么,有什么区别呢?(我猜,输入流读取所有文件,但只是通过断路位置之前的所有符号,但它
我试图分块读取输入流并写入文件以避免内存问题,我接收json格式的数据,并使用以下代码写入文件。 我的问题是,大多数json都写得很好,虽然其中一些包含损坏的数据,但我不确定我是否正确地将CharBuffer与BufferedReader一起使用,我观察到的另一件事是,对于少量数据,它正确地将CharBuffer写入文件,当我从服务器接收到更大的数据(大约2MB的输入流-不是很大)时,我通常会遇到
是否有任何方法来管理这一事实?例如,告诉MongoDB设置只在次要数据可用的情况下才从次要数据传递数据,并引用主要的其他数据?