*:first-child {
margin-top: 0 !important; }
body > *:last-child {
margin-bottom: 0 !important; }
a {
color: #4183C4;
text-decoration: none; }
a.absent {
color: #cc0000; }
a.anchor {
display: block;
padding-left: 30px;
margin-left: -30px;
cursor: pointer;
position: absolute;
top: 0;
left: 0;
bottom: 0; }
h1, h2, h3, h4, h5, h6 {
margin: 20px 0 10px;
padding: 0;
font-weight: bold;
-webkit-font-smoothing: antialiased;
cursor: text;
position: relative; }
h1:hover a.anchor, h2:hover a.anchor, h3:hover a.anchor, h4:hover a.anchor, h5:hover a.anchor, h6:hover a.anchor {
background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA09pVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMy1jMDExIDY2LjE0NTY2MSwgMjAxMi8wMi8wNi0xNDo1NjoyNyAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvIiB4bWxuczp4bXBNTT0iaHR0cDovL25zLmFkb2JlLmNvbS94YXAvMS4wL21tLyIgeG1sbnM6c3RSZWY9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9zVHlwZS9SZXNvdXJjZVJlZiMiIHhtcDpDcmVhdG9yVG9vbD0iQWRvYmUgUGhvdG9zaG9wIENTNiAoMTMuMCAyMDEyMDMwNS5tLjQxNSAyMDEyLzAzLzA1OjIxOjAwOjAwKSAgKE1hY2ludG9zaCkiIHhtcE1NOkluc3RhbmNlSUQ9InhtcC5paWQ6OUM2NjlDQjI4ODBGMTFFMTg1ODlEODNERDJBRjUwQTQiIHhtcE1NOkRvY3VtZW50SUQ9InhtcC5kaWQ6OUM2NjlDQjM4ODBGMTFFMTg1ODlEODNERDJBRjUwQTQiPiA8eG1wTU06RGVyaXZlZEZyb20gc3RSZWY6aW5zdGFuY2VJRD0ieG1wLmlpZDo5QzY2OUNCMDg4MEYxMUUxODU4OUQ4M0REMkFGNTBBNCIgc3RSZWY6ZG9jdW1lbnRJRD0ieG1wLmRpZDo5QzY2OUNCMTg4MEYxMUUxODU4OUQ4M0REMkFGNTBBNCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PsQhXeAAAABfSURBVHjaYvz//z8DJYCRUgMYQAbAMBQIAvEqkBQWXI6sHqwHiwG70TTBxGaiWwjCTGgOUgJiF1J8wMRAIUA34B4Q76HUBelAfJYSA0CuMIEaRP8wGIkGMA54bgQIMACAmkXJi0hKJQAAAABJRU5ErkJggg==) no-repeat 10px center;
text-decoration: none; }
h1 tt, h1 code {
font-size: inherit; }
h2 tt, h2 code {
font-size: inherit; }
h3 tt, h3 code {
font-size: inherit; }
h4 tt, h4 code {
font-size: inherit; }
h5 tt, h5 code {
font-size: inherit; }
h6 tt, h6 code {
font-size: inherit; }
h1 {
font-size: 28px;
color: black; }
h2 {
font-size: 24px;
border-bottom: 1px solid #cccccc;
color:#00ccff;
margin: 25px auto 12px auto;
padding-bottom: 2px;
}
h3 {
font-size: 18px;
color:#993300;}
h4 {
font-size: 16px; }
h5 {
font-size: 14px; }
h6 {
color: #777777;
font-size: 14px; }
p, blockquote, ul, ol, dl, li, table, pre {
margin: 0 0; }
/*hr {
background: transparent url(data:image/png;base64,iVBORw0KGgoA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EE4Mzg3MkMyOUY0OCIvPiA8L3JkZjpEZXNjcmlwdGlvbj4gPC9yZGY6UkRGPiA8L3g6eG1wbWV0YT4gPD94cGFja2V0IGVuZD0iciI/PqqezsUAAAAfSURBVHjaYmRABcYwBiM2QSA4y4hNEKYDQxAEAAIMAHNGAzhkPOlYAAAAAElFTkSuQmCC) repeat-x 0 0;
border: 0 none;
color: #cccccc;
height: 4px;
padding: 0;
}*/
hr {
margin: 0 0 19px;
border: 0;
border-bottom: 1px solid #ccc;
}
body > h2:first-child {
margin-top: 0;
padding-top: 0; }
body > h1:first-child {
margin-top: 0;
padding-top: 0; }
body > h1:first-child + h2 {
margin-top: 0;
padding-top: 0; }
body > h3:first-child, body > h4:first-child, body > h5:first-child, body > h6:first-child {
margin-top: 0;
padding-top: 0; }
a:first-child h1, a:first-child h2, a:first-child h3, a:first-child h4, a:first-child h5, a:first-child h6 {
margin-top: 0;
padding-top: 0; }
h1 p, h2 p, h3 p, h4 p, h5 p, h6 p {
margin-top: 0; }
li p.first {
display: inline-block; }
li {
margin: 0; }
ul, ol {
padding-left: 30px; }
ul :first-child, ol :first-child {
margin-top: 0; }
dl {
padding: 0; }
dl dt {
font-size: 14px;
font-weight: bold;
font-style: italic;
padding: 0;
margin: 15px 0 5px; }
dl dt:first-child {
padding: 0; }
dl dt > :first-child {
margin-top: 0; }
dl dt > :last-child {
margin-bottom: 0; }
dl dd {
margin: 0 0 15px;
padding: 0 15px; }
dl dd > :first-child {
margin-top: 0; }
dl dd > :last-child {
margin-bottom: 0; }
blockquote {
border-left: 4px solid #dddddd;
padding: 0 15px;
color: #777777; }
blockquote > :first-child {
margin-top: 0; }
blockquote > :last-child {
margin-bottom: 0; }
/*table {
padding: 0;border-collapse: collapse; }
table tr {
border-top: 1px solid #cccccc;
background-color: white;
margin: 0;
padding: 0; }
table tr:nth-child(2n) {
background-color: #f8f8f8; }
table tr th {
font-weight: bold;
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }
table tr td {
border: 1px solid #cccccc;
margin: 0;
padding: 6px 13px; }
table tr th :first-child, table tr td :first-child {
margin-top: 0; }
table tr th :last-child, table tr td :last-child {
margin-bottom: 0; }*/
img {
max-width: 100%; }
span.frame {
display: block;
overflow: hidden; }
span.frame > span {
border: 1px solid #dddddd;
display: block;
float: left;
overflow: hidden;
margin: 13px 0 0;
padding: 7px;
width: auto; }
span.frame span img {
display: block;
float: left; }
span.frame span span {
clear: both;
color: #333333;
display: block;
padding: 5px 0 0; }
span.align-center {
display: block;
overflow: hidden;
clear: both; }
span.align-center > span {
display: block;
overflow: hidden;
margin: 13px auto 0;
text-align: center; }
span.align-center span img {
margin: 0 auto;
text-align: center; }
span.align-right {
display: block;
overflow: hidden;
clear: both; }
span.align-right > span {
display: block;
overflow: hidden;
margin: 13px 0 0;
text-align: right; }
span.align-right span img {
margin: 0;
text-align: right; }
span.float-left {
display: block;
margin-right: 13px;
overflow: hidden;
float: left; }
span.float-left span {
margin: 13px 0 0; }
span.float-right {
display: block;
margin-left: 13px;
overflow: hidden;
float: right; }
span.float-right > span {
display: block;
overflow: hidden;
margin: 13px auto 0;
text-align: right; }
code, tt {
margin: 0 2px;
padding: 0 0px;
white-space: nowrap;
/*background-color: #fff;*/
color: rgba(51, 102, 255, 255);
/*border: 1px solid #eaeaea;*/
/*background-color: #f8f8f8;*/
/*border-radius: 3px; */}
pre code {
margin: 0;
padding: 0;
white-space: pre;
border: none;
color: rgba(0, 0, 0, 255);
background: transparent; }
.highlight pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
font-size: 13px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px; }
pre {
background-color: #f8f8f8;
border: 1px solid #cccccc;
font-size: 14px;
line-height: 19px;
overflow: auto;
padding: 6px 10px;
border-radius: 3px; }
pre code, pre tt {
background-color: transparent;
border: none; }
sup {
font-size: 0.83em;
vertical-align: super;
line-height: 0;
}
* {
-webkit-print-color-adjust: exact;
}
@media screen and (min-width: 914px) {
body {
/*width: 854px;*/
/*margin:0 auto;*/
}
}
@media print {
table, pre {
page-break-inside: avoid;
}
pre {
word-wrap: break-word;
}
}
#cnblogs_post_body li ul li {
list-style-type: circle !important;
}
#cnblogs_post_body a {
text-decoration: none;
color: #4183C4;
}
#mainContent .postBody h2 {
margin: 25px auto 12px auto;
padding-bottom: 2px;
}
-->
存储数据(下)——文件存储
这篇文章学到的内容:
1、回顾之前学习的三种数据存储方式(SharedPreferences、xml和Sqlite)
2、文件存储(存储数据到data/data/packagename/files和存储数据到sdcard)
1、回顾之前学习的三种数据存储方式(SharedPreferences、xml和Sqlite)
SharedPreferences
轻量级的存储类,主要保存一些常用的配置,适合用简单的数据类型,int、String、boolean等。它的本质也是基于xml来存储key-value键值对数据。
Xml存储
轻量级的存储类,如果数据量不大,但是数据又比较复杂,可以选择使用这种方式存储数据。可以自定义xml标签格式。处理xml时,能通过自带底层的本地xml Parser来解析xml。这里建议采用xmlpull方式来操作xml
Sqlite存储
功能强大,可以实现数据库的许多操作。它就是一个轻量级的数据库,被设计用于嵌入式的设备,所以android采用sqlite来作为支持的关系型数据库管理系统。到2015年,sqlite已经到sqlite3了,具体的操作,和我们常见的数据库一样。如果数据量大,而且有许多关系表,建议采用数据库来做数据存储。
2、进入主题,介绍剩下三种数据存储方式
android除了上面的三种方式,还有三种数据存储:文件存储、contentProvider存储,网络存储。这里介绍文件存储。后两个以后会讲。
3、文件存储
文件存储的方式,说白了就是通过流的方式来创建文件来存储数据。只不过,我们是在手机的存储卡中来创建这些文件的。
存储数据到data/data/packagename/files
关键代码就是Context.openFileOutput(fileName, mode);执行这句话,会在data/data/packagename/files目录下创建fileName文件,我们可以用流的方式把数据写入到fileName中。mode是文件模式。你可也在后面的文件模式查看android支持的缓存文件模式。
存内容
这里只贴关键代码。
1 /** 2 * 存储内容到data/data/packagename/file 3 * 4 * @param act 5 * @param content 6 * 文件内容 7 * @param fileName 8 * 文件名 9 * @param mode 10 * 模式 11 */ 12 public static void saveContentToDataFile(Activity act, String content, 13 String fileName, int mode) { 14 15 try { 16 FileOutputStream fos = act.openFileOutput(fileName, mode); 17 fos.write(content.getBytes("UTF-8")); 18 fos.close(); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 }
读内容
这里只贴关键代码。
1 /** 2 * 读取data/data/packagename/file/fileName文件中的字符内容 3 * @param act 4 * @param fileName 5 * @return 6 */ 7 public static String readContentFromDataFile(Activity act, String fileName) { 8 9 String result = ""; 10 try { 11 FileInputStream in = null; 12 ByteArrayOutputStream bout = null; 13 byte[] buf = new byte[1024]; 14 bout = new ByteArrayOutputStream(); 15 int length = 0; 16 in = act.openFileInput(fileName); // 获得输入流 17 while ((length = in.read(buf)) != -1) { 18 bout.write(buf, 0, length); 19 } 20 byte[] content = bout.toByteArray(); 21 result = new String(content, "UTF-8"); 22 in.close(); 23 bout.close(); 24 } catch (Exception e) { 25 e.printStackTrace(); 26 } 27 return result; 28 }
检查文件是否存在
这里只贴关键代码。
1 /** 2 * 检查file是否存在 3 * @param act 4 * @param fileName 5 * @return 6 */ 7 public static boolean checkFileIsExistInData(Activity act, String fileName){ 8 9 boolean result = false; 10 File file = act.getFileStreamPath(fileName); 11 if (file != null && file.exists()) { 12 result = true; 13 } 14 return result; 15 }
文件模式
1.Context.MODE_PRIVATE:私有覆盖模式
只能被当前应用访问,并且如果写入,则覆盖。
2.Context.MODE_APPEND:私有追加模式
只能被当前应用访问,并且如果写入,则追加。
3.Context,MODE_WORLD_READABLE:公有只读模式
可以被其他应用读取。
4.Context.MODE_WORLD_WRITEABLE:公有可写模式
可以被其他应用写入,但不能读取。 注意,如果希望其他使得文件模式叠加,则可以使用加号连接:
比如:Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE表示其他应用可读写。
存储文件到SDCard
关键代码,就是要活的sdcard的根路径。Environment.getExternalStorageDirectory()就是得到当前外部存储设备的根目录。
检测sdcard设备是否存在
这里只贴关键代码。
1 public static boolean checkSDCARD() { 2 3 String status = Environment.getExternalStorageState(); 4 5 if (status.equals(Environment.MEDIA_MOUNTED)) { 6 return true; 7 } 8 9 return false; 10 }
在sdcard中创建目录
这里只贴关键代码。
1 public static boolean createDir2SDCard(String path) { 2 3 File dir = new File(path); 4 5 if (!dir.exists()) { 6 return dir.mkdirs(); 7 } 8 9 return true; 10 }
在sdcard中创建文件
这里只贴关键代码。
1 public static File createFile2SDCard(String path, String fileName) { 2 3 // /////////////////////////////////////// 4 // 创建SD卡目录 5 // /////////////////////////////////////// 6 File dir = new File(path); 7 8 if (!dir.exists()) { 9 dir.mkdirs(); 10 } 11 12 // ////////////////////////////////////////// 13 // 创建SD卡文件 14 // /////////////////////////////////////////// 15 File file = new File(path + fileName); 16 17 if (file.exists()) { 18 19 file.delete(); 20 } 21 22 try { 23 file.createNewFile(); 24 } catch (IOException e) { 25 // TODO Auto-generated catch block 26 e.printStackTrace(); 27 } 28 29 return file; 30 }
在sdcard中删除目录和目录下的所有文件
这里只贴关键代码。
public static boolean deleteDir4SDCard(String path) { File dir = new File(path); if (dir.exists()) { File[] files = dir.listFiles(); for (File file : files) { file.delete(); } dir.delete(); } return true; }
在sdcard中检查文件是否存在
这里只贴关键代码。
1 public static boolean checkFileExist(String path) { 2 3 File file = new File(path); 4 5 if (file.exists()) { 6 7 return true; 8 } 9 10 return false; 11 }
在sdcard中创建文件并写入内容
这里只贴关键代码。
1 /** 2 * 保存内容到sdcard 3 * @param act 4 * @param content 5 * @param fileName 6 * @param mode 7 */ 8 public static void saveContentToSDCardFile(Activity act, String content, 9 String fileName) { 10 11 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // 如果sdcard插入 12 try { 13 String sdcardRootPath = Environment.getExternalStorageDirectory() + "/"; 14 FileOutputStream fos = new FileOutputStream(sdcardRootPath + fileName); 15 fos.write(content.getBytes("UTF-8")); 16 fos.close(); 17 } catch (Exception e) { 18 e.printStackTrace(); 19 } 20 } 21 }
在sdcard中读取内容
这里只贴关键代码。
1 /** 2 * 从sdcard中某个文件读取内容 3 * @param act 4 * @param fileName 5 * @return 6 */ 7 public static String readContentFromSDCardFile(Activity act, String fileName) { 8 9 String result = ""; 10 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // 如果sdcard插入 11 String sdcardRootPath = Environment.getExternalStorageDirectory() + "/"; 12 try { 13 FileInputStream in = null; 14 ByteArrayOutputStream bout = null; 15 byte[] buf = new byte[1024]; 16 bout = new ByteArrayOutputStream(); 17 int length = 0; 18 in = new FileInputStream(sdcardRootPath + fileName); 19 while ((length = in.read(buf)) != -1) { 20 bout.write(buf, 0, length); 21 } 22 byte[] content = bout.toByteArray(); 23 result = new String(content, "UTF-8"); 24 in.close(); 25 bout.close(); 26 } catch (Exception e) { 27 e.printStackTrace(); 28 } 29 } 30 return result; 31 }
本站文章为宝宝巴士 SD.Team原创,转载务必在明显处注明:(作者官方网站:宝宝巴士)
转载自【宝宝巴士SuperDo团队】 原文链接: http://www.cnblogs.com/superdo/p/4809580.html