日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android文件读写操作布局文件代码,android实现文件读写功能

發(fā)布時間:2023/12/2 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android文件读写操作布局文件代码,android实现文件读写功能 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

本文實例為大家分享了android實現(xiàn)文件讀寫功能的具體代碼,供大家參考,具體內(nèi)容如下

讀取:

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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

總結(jié)

以上是生活随笔為你收集整理的android文件读写操作布局文件代码,android实现文件读写功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。