有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java动态重命名资产文件夹中的文件名

我的数据库(比如x.db)存储在资产文件夹(path=“\app\src\main\assets”)中。我试图在运行时加载它之前更改它的名称,但由于某些原因,我无法这样做。我的目标是为每个用户重命名数据库。因此,如果有3个用户,我希望有3个新数据库加上初始数据库。 我的代码第一次运行时就被剪掉了

    try {
        File oldFile = new File("\\app\\src\\main\\assets");
        File[] listOfFiles = oldFile.listFiles();//this is always empty.
        for (File listOfFile : listOfFiles) {
            if (listOfFile.isFile() && listOfFile.getName().equals("x.db")) {
                return listOfFile.renameTo(new File("y" + ".db"));
            }
        }
        return false;          
    } catch (Exception e) {
        return false;
    }

共 (1) 个答案

  1. # 1 楼答案

    My goal is to rename the database per user.

    然后,您不需要重命名资产文件,因为它是APK的一部分,因此受保护,因此无法重命名。您要做的是使用每个用户所需的数据库名称将资产复制到存储数据库的位置

    范例

    下面的示例创建了3个数据库,从单个资产(本例中为mydb)复制它们。核心代码是DatabaseHelper(SQLiteOpenHelper的一个子类)。通常情况下,这是从上下文中构造出来的。在这种情况下,将传递用户名,从而允许每个用户单独使用数据库。数据库名称是以资产名称为后缀的用户名

    数据库助手多用户数据库助手。java:-

    public class MultiUserDBHelper extends SQLiteOpenHelper {
    
        public static final String ASSET_NAME = "mydb";
        public String DB_PATH;
    
        Context mContext;
        SQLiteDatabase mDataBase;
    
        public MultiUserDBHelper(Context context, String User) {
    
            super(context, User+ASSET_NAME, null, 1);
            DB_PATH = context.getDatabasePath(User+ASSET_NAME).getPath();
            this.mContext = context;
            if (!checkDataBase()) {
                copyDataBase();
            }
            mDataBase = this.getWritableDatabase(); //Forces open and therefore creation if db doesn exist.
        }
    
    
        public void copyDataBase() {
            try {
                InputStream myInput = mContext.getAssets().open(ASSET_NAME);
                String outputFileName = DB_PATH;
                OutputStream myOutput = new FileOutputStream(outputFileName);
    
                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer)) > 0)
                    myOutput.write(buffer, 0, length);
    
                myOutput.flush();
                myOutput.close();
                myInput.close();
                ;
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException("Error copying database from asset");
            }
        }
    
    
        public boolean checkDataBase() {
            File dbfile = new File(DB_PATH);
            if (dbfile.exists()) return true;
            if (!(dbfile.getParentFile()).exists()) dbfile.getParentFile().mkdirs();
            return false;
        }
    
    
        @Override
        public void onCreate(SQLiteDatabase db) {
    
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        }
    }
    

    向3名用户演示上述功能的活动:-

    public class MainActivity extends AppCompatActivity {
    
        MultiUserDBHelper dbuser1, dbuser2, dbuser3;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            dbuser1 = new MultiUserDBHelper(this,"User1");
            dbuser2 = new MultiUserDBHelper(this,"User2");
            dbuser3 = new MultiUserDBHelper(this,"User3"); 
        }
    }
    
    • 显然,您将介绍一种选择/切换用户的方法。abve仅用于演示对每个数据库的复制/访问

    运行上述设备资源管理器后:

    enter image description here