openFileOutput()对文件操作
2013-04-28 14:02
281 查看
android 中使用openFileOutput(name, null),对文件进行保存与读取操作
当要用到读取文件时,必须使用c.getFilesDir()来得到已经保存的路径:
ex:
private SQLiteDatabase copyDatabase2Device() throws IOException {
String databasePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";//"/dictionary/" ;
String databaseName = databasePath + "adnnet.db";
InputStream inStream = null;
FileOutputStream outStream = null;
if(!(new File(databaseName)).exists()) {
inStream = c.getResources().openRawResource(R.raw.adnnet);
outStream = new FileOutputStream(databaseName);
int count = 0;
if(inStream != null) {
byte[] buffer = new byte[4096];
while((count = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, count);
}
}
outStream.flush();
if(inStream != null) {
inStream.close();
inStream = null;
}
if(outStream != null) {
outStream.close();
outStream = null;
}
}
return SQLiteDatabase.openOrCreateDatabase(databaseName, null);
}
private boolean copyDatabase2Device1() {
InputStream inStream;
OutputStream outStream;
try {
if (isSdcardReally()&& !new File(c.getFilesDir()+"/adnnet.db").exists()) {
inStream = c.getResources().openRawResource(R.raw.adnnet);
outStream = c.openFileOutput("adnnet.db", Context.MODE_PRIVATE);
int count = 0;
byte[] buffer = new byte[4096];
while ((count = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, count);
}
outStream.flush();
outStream.close();
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private String isDatabaseOK() {
File file = c.getFilesDir();
return file.getName();
}
private boolean isSdcardReally() {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
return true;
return false;
}
public SQLiteDatabase openDatabase() {
try {
if (copyDatabase2Device1()) {
db = SQLiteDatabase.openOrCreateDatabase(c.getFilesDir()+"/adnnet.db", null);
return db;
}
} catch (Exception e) {
e.printStackTrace();
closeDatabase();
}
return null;
}
public void closeDatabase() {
if(db != null) {
db.close();
db = null;
}
}
当要用到读取文件时,必须使用c.getFilesDir()来得到已经保存的路径:
ex:
private SQLiteDatabase copyDatabase2Device() throws IOException {
String databasePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";//"/dictionary/" ;
String databaseName = databasePath + "adnnet.db";
InputStream inStream = null;
FileOutputStream outStream = null;
if(!(new File(databaseName)).exists()) {
inStream = c.getResources().openRawResource(R.raw.adnnet);
outStream = new FileOutputStream(databaseName);
int count = 0;
if(inStream != null) {
byte[] buffer = new byte[4096];
while((count = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, count);
}
}
outStream.flush();
if(inStream != null) {
inStream.close();
inStream = null;
}
if(outStream != null) {
outStream.close();
outStream = null;
}
}
return SQLiteDatabase.openOrCreateDatabase(databaseName, null);
}
private boolean copyDatabase2Device1() {
InputStream inStream;
OutputStream outStream;
try {
if (isSdcardReally()&& !new File(c.getFilesDir()+"/adnnet.db").exists()) {
inStream = c.getResources().openRawResource(R.raw.adnnet);
outStream = c.openFileOutput("adnnet.db", Context.MODE_PRIVATE);
int count = 0;
byte[] buffer = new byte[4096];
while ((count = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, count);
}
outStream.flush();
outStream.close();
inStream.close();
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private String isDatabaseOK() {
File file = c.getFilesDir();
return file.getName();
}
private boolean isSdcardReally() {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
return true;
return false;
}
public SQLiteDatabase openDatabase() {
try {
if (copyDatabase2Device1()) {
db = SQLiteDatabase.openOrCreateDatabase(c.getFilesDir()+"/adnnet.db", null);
return db;
}
} catch (Exception e) {
e.printStackTrace();
closeDatabase();
}
return null;
}
public void closeDatabase() {
if(db != null) {
db.close();
db = null;
}
}
相关文章推荐
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- android文件操作OpenFileInput OpenFileOutput
- andriod学习——Content的openFileOutput 和 openFileInput操作文件
- Android文件操作中的openFileOutPut和openFileInput
- Android文件操作中的openFileOutPut和openFileInput
- SharedPreferences数据、openFileOutput文件、SQLite数据库文件存储位置
- android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()
- Android应用私有存储文件的写入与读取-openFileInput 和 openFileOutput
- android:内部存储空间 IO 操作openFileInput()、output(13)
- Android数据存储之文件 openFileOutput & openFileInput
- 创私有的应用程序文件openFileInput()和openFileOutput()
- 用openFileOutput() 把文件放入SD卡
- Android--模拟登陆用户名密码,使用File或openFileOutput保存文件
- OpenFileDialog SaveFileDialog FolderBrowserDialog 文件路径操作
- Python基础【第十一篇】文件操作(file()、open()方法和fileinput模块)
- Silverlight之文件操作(Isolated Store、IsolatedStorageSettings 、OpenFileDialog、SaveFileDialog)
- android 文件读写 (openFileOutput)
- Android File操作-this.openFileOutput()的NullPointerException
- 文件存储openFileOutput和openFileInput