android實現(xiàn)文件讀寫功能
更新時間:2020年07月28日 09:10:37 作者:小新110
這篇文章主要為大家詳細介紹了android實現(xiàn)文件讀寫功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了android實現(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();
}
}
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)基礎(chǔ)之創(chuàng)建啟動界面Splash Screen的方法
這篇文章主要介紹了Android開發(fā)基礎(chǔ)之創(chuàng)建啟動界面Splash Screen的方法,以實例形式較為詳細的分析了Android定制啟動界面的布局及功能實現(xiàn)相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-10-10
android app判斷是否有系統(tǒng)簽名步驟詳解
這篇文章主要為大家介紹了android app判斷是否有系統(tǒng)簽名步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11
android獲取圖片尺寸的兩種方式及bitmap的縮放操作
這篇文章主要介紹了android獲取圖片尺寸的兩種方式及bitmap的縮放操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08

