说明 :IntelliJ IDEA Maven工程
1.将资源文件夹的数据库复制到指定位置
在工程中resour新建raw,将模板数据库data.db拷贝进去
实现将文件复制到D盘根目录下(D:/data.db)
调用MySQLiteHelper.getDatabase();import com.mb.network.utils.Log; import java.io.File; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; public class MySQLiteHelper { static String TAG = "MySQLiteHelper.class"; static String targetPath = "D:/data.db"; public static void getDatabase(){ try{ File filePath = new File(targetPath); if(!filePath.exists()){ InputStream source= MySQLiteHelper.class.getClassLoader().getResourceAsStream("raw/data.db"); Files.copy(source, Paths.get(targetPath), StandardCopyOption.REPLACE_EXISTING); Log.e("","复制文件成功"); } else{ Log.e(TAG, "数据库已存在"); } }catch (Exception e){ Log.e(TAG, "错误: "+ e) ; } } }