android實(shí)現(xiàn)文件讀寫功能
本文實(shí)例為大家分享了android實(shí)現(xiàn)文件讀寫功能的具體代碼,供大家參考,具體內(nèi)容如下
讀?。?/p>
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;
}
調(diào)用:
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, "地址和端口已經(jīng)寫入文件", Toast.LENGTH_SHORT).show();
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android studio 切換flutterSDK之后報(bào)錯(cuò)及解決辦法(推薦)
這篇文章主要介紹了Android studio 切換flutterSDK之后報(bào)錯(cuò)及解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Flutter實(shí)現(xiàn)笑嘻嘻的動(dòng)態(tài)表情的示例代碼
這篇文章主要為大家介紹了如何利用Flutter實(shí)現(xiàn)笑嘻嘻的動(dòng)態(tài)表情,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Flutter有一定幫助,感興趣的可以了解一下2022-04-04
android實(shí)現(xiàn)簡(jiǎn)單的矩形裁剪框
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)簡(jiǎn)單的矩形裁剪框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
Android實(shí)現(xiàn)監(jiān)聽電話呼叫狀態(tài)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)監(jiān)聽電話呼叫狀態(tài)的方法,涉及Android權(quán)限控制及電話狀態(tài)監(jiān)聽的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android實(shí)現(xiàn)定時(shí)任務(wù)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)定時(shí)任務(wù)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
Android 獲取IP地址的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 獲取IP地址的實(shí)現(xiàn)方法的相關(guān)資料,這里提供了具體實(shí)現(xiàn)的方法及代碼,使用WIFI 和GPRS的思路,需要的朋友可以參考下2016-11-11
flutter 輸入框組件TextField的實(shí)現(xiàn)代碼
這篇文章主要介紹了flutter 輸入框組件TextField的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

