android 文件讀寫

讀取:

public static String _getJsonString(String fileName)
throws IOException {
if ((fileName == null) || fileName.isEmpty()) {
return "";
}
String retString = "";
FileInputStream fis = null;
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory()
+ "/" + fileName + ".json");
if (file.exists()) {
fis = new FileInputStream(file);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();

retString = new String(buffer);
} else {

}
}
return retString;
}
寫:

public static void saveSettingFile(String fileName, String content) {
FileOutputStream fos = null;
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory()
+ "/" + fileName + ".json");
try {
fos = new FileOutputStream(file);
byte[] buffer = content.getBytes();
fos.write(buffer);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Gson 讀寫:

public static void saveServerInfo(String fileName, String content) {
FileOutputStream fos = null;
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory()
+ "/" + fileName + ".json");
try {
fos = new FileOutputStream(file);
byte[] buffer = content.getBytes();
fos.write(buffer);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

public static ServerInfo getServerInfo(String fileName)
throws IOException {
ServerInfo serverInfo = new ServerInfo();
if ((fileName == null) || fileName.isEmpty()) {
serverInfo = null;
return serverInfo;
}
FileInputStream fis = null;
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
File file = new File(Environment.getExternalStorageDirectory()
+ "/" + fileName + ".json");
if (file.exists()) {
fis = new FileInputStream(file);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();

Gson gson = new Gson();
serverInfo = gson.fromJson(new String(buffer),
ServerInfo.class);
} else {
serverInfo = null;
}
}
return serverInfo;
}
調用:

public void onSetIPAndPort(View view) {
ServerInfo serverInfo = new ServerInfo();
try {
serverInfo = JsonFileWriteAndRead.getServerInfo("videochat");
} catch (IOException e) {
e.printStackTrace();
}

//寫入ip和端口
String ip = ipSet.getText().toString();
String port = portSet.getText().toString();
serverInfo.setIpString(ip);
serverInfo.setPortString(port);
Gson gson = new Gson();
if (ip.isEmpty() || port.isEmpty()) {
Toast.makeText(this, "地址或端口為空", Toast.LENGTH_SHORT).show();
} else {
JsonFileWriteAndRead.saveServerInfo("videochat", gson.toJson(serverInfo));
Toast.makeText(this, "地址和端口已經寫入文件", Toast.LENGTH_SHORT).show();
}
}
---------------------?

轉載于:https://www.cnblogs.com/ly570/p/10995918.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/247801.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/247801.shtml
英文地址,請注明出處:http://en.pswp.cn/news/247801.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

element組件庫中table自定義分頁效果

1.在data中設置初始值 // 頁數 頁碼search: { offset: 1, // 當前頁limit: 10, // 條數total:0, //總數},2.設置獲取后的數據分配 :data"tableData.slice((search.offset - 1) * search.limit, search.offset * search.limit)" 3.展示效果:

Django信號量

信號 Django中提供了“信號調度”,用于在框架執行操作時解耦。通俗來講,就是一些動作發生的時候,信號允許特定的發送者去提醒一些接受者。 1、Django內置信號 1 Model signals2 pre_init # django的modal執行其構造方…

uni-app 調用接口封裝文檔

1.創建 util 文件夾 ,并在文件夾中創建 api.js const BASE_URL http://localhost:8082 // 域名頭 export const myRequest (options)>{return new Promise((resolve,reject)>{uni.request({url:BASE_URLoptions.url,method: options.method || GET,data: …

HTML中INPUT type=date標簽如何賦值注意問題

現在的html5 input標簽支持type"date" 顯示有日期的日歷控件,一行簡單的代碼就能顯示出一個日歷控件,但是有的時候需要給它一個默認的日期值,這個時候可能就要用到val(), attr("value","")等方法&a…

數據輪播圖翻頁封裝(左右點擊)

效果圖&#xff1a; <template><div class"box" style"height:200px;"><!-- 左 --><div class"box-left" click"submitrote(left)" mouseenter"chenge_menu(1)" mouseleave"chenge_menu(2)"…

jquery自動點擊按鈕

jquery自動點擊按鈕$(document).ready(function(){$("#imgcode_img").trigger("click");});

087-把PHP數組中的元素按隨機順序重新排列shuffle

<?php$arrarray(3,23,A,f,123,hello); //定義一個數組echo 排序之前的數組信息&#xff1a;<br>;print_r($arr);shuffle($arr); //對數組進行隨機排序echo <br>排序之后的數組信息&#xff1a;<br>;print_r($arr); //輸出數組信息#…

highcharts圖表高級入門之polar:極地圖的基本配置以及一些關鍵配置說明

highcharts圖表組件內的極地圖polar的實現和效果都還是很簡單和美觀的。 1、需要設置chart的polar屬性為true&#xff1b;以表示是極地圖&#xff1b; 2、其他的設置和普通圖表就沒什么區別了的&#xff0c;這里附上一個完整的示例代碼&#xff1a; highcharts圖表組件內的極地…

基于Element ui 實現輸入框只能輸入數字并支持千分位

實現效果 設置子組件 <template><el-inputref"money"v-model.trim"money":placeholder"placeholder"v-bind"$attrs"v-on"$listeners"input"formatNumber(money,money)"keyup.enter.native"moneyCh…

Vue-使用webpack+vue-cli搭建項目

一、準備 安裝NodeJs 安裝Webpack 配置環境變量 技巧使用&#xff1a; 1. npm 淘寶路徑配置&#xff1a;npm config set registryhttps://registry.npm.taobao.org  2.查看npm命令列表 > $ npm help 二、搭建項目 1、全局安裝vue腳手架工具 vue-cli npm install vue…

element 日歷組件-自定義內容

這只是個子組件 <template><div ref"topBox" class"swiper-in page-container bg-white"><div class"w-full page-head">我的排班<i class"close-btn el-icon-close" click"closeCurrentPage"><…

CSS-合理使用z-index控制盒子視軸高度,解決z-index失效

關于z-index我們的共識 共識一 首先&#xff0c;我們都同意&#xff0c;z-index對于普通盒子是無效的&#xff0c;這里的“普通盒子”是除了下文我會提到的“神奇盒子”外的所有盒子&#xff0c;至于什么是“神奇盒子”請慢慢看。 對于普通盒子&#xff0c;不管z-index值如何&…

Activiti6.0教程 Eclipse安裝Activiti Diagram插件(一)

最近這段時間打算出一個Activiti6.0的詳細教程&#xff0c;Activiti作為一個流行的開源工作流引擎&#xff0c;正在不斷發展&#xff0c;其6.0版本以API形式提供服務&#xff0c;而之前版本基本都是要求我們的應用以JDK方式與其交互&#xff0c;只能將其攜帶到我們的應用中&…

JS性能優化之文檔碎片-document.createDocumentFragment

講這個方法之前&#xff0c;我們應該先了解下插入節點時瀏覽器會做什么。 在瀏覽器中&#xff0c;我們一旦把節點添加到document.body&#xff08;或者其他節點&#xff09;中&#xff0c;頁面就會更新并反映出這個變化&#xff0c;對于少量的更新&#xff0c;一條條循…

man(2) W

wait(2) wait3(2) wait4(2) waitpid(2) waitid(2) SEE ALSO _exit(2), clone(2), fork(2), kill(2), ptrace(2), sigaction(2), sig‐ nal(2), wait4(2), pthread_create(3), credentials(7), signal(7)轉載于:https://www.cnblogs.com/xpylovely/p/11018021.html

window.onload和$(document).ready(function(){})的區別

前段時間在面試之前查找并整理了一下window.onload和$(document).ready(function(){})區別&#xff0c;今天有時間更到我的博客上&#xff0c;由于本人資歷尚淺&#xff0c;如有不對的地方&#xff0c;還請指正。 原文出自&#xff1a;http://www.php100.com/html/program/jque…

任務計劃

普及組題庫:(94/100) luogu: 網絡流&#xff1a;P2423,P2055,P3191,P3153,P2598,P4174 線段樹&#xff1a;P1712,P4145,P4868,P2619,P2572,P4247,P4246,P4198 ST表&#xff1a;P2880 DFS序(或歐拉序)線段樹&#xff1a;P3178,P3459 樹鏈剖分&#xff1a;P3384,P3379,P4315,P248…

左右滑動欄

展示效果圖&#xff1a; 父 <template><div ref"topBox" class"swiper-in page-container bg-white"><div class"page-body"><drag-box class"drag-box"><div class"relative" :class"{ex…

Active MQ

轉載于:https://www.cnblogs.com/ygl888/p/11020647.html

jQuery Ajax 實例

一、$.ajax的一般格式 $.ajax({ type: POST, url: url , data: data , success: success , dataType: dataType }); 二、$.ajax的參數描述 參數 描述 url 必需。規定把請求發送到哪個 URL。 data 可選。映射或字符串值。規定連同請求發送到服務器的數據。 success(data, textSt…