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
android 文件读写相关推荐
- 【转】Android - 文件读写操作 总结
Android - 文件读写操作 总结 原文出处:http://blog.csdn.net/ztp800201/article/details/7322110 在android中的文件放在不同位置,它 ...
- Android 文件读写最全解析
本文目录 Android文件读写概述 读取raw目录文件 读取assets目录文件 data/data/(包名) 目录文件读写 写数据 读数据 sdcard文件读写 申请动态权限 写数据 读数据 sd ...
- android文件读写操作布局文件代码,android实现文件读写功能
本文实例为大家分享了android实现文件读写功能的具体代码,供大家参考,具体内容如下 读取: public static String _getJsonString(String fileName) ...
- Android文件读写操作(assets 文件、 raw文件、内部存储文件、外部存储文件)
Android中的文件读写操作是不可或缺的,每个应用都会涉及到读写操作.这里将读写操作分成了四个部分 assets文件夹中文件数据的读取 raw文件夹中的文件数据的读取 Android内部存储文件的读 ...
- Android - 文件读写操作 总结
http://blog.csdn.net/ztp800201/article/details/7322110 在android中的文件放在不同位置,它们的读取方式也有一些不同. 本文对android中 ...
- Android:文件读写.
//往SD卡写入文件的方法public void savaFileToSD(String filename, String filecontent) throws Exception {//如果手机已 ...
- android 文件读写概述
(1)data/data下面主要是/data/data/Package Name目录, 该目录下面主要是/data/data/Package Name/Shared_Pref, /data/data/ ...
- qt android文件读写文件,qt android 开发之写入xml配置文件篇
应用初始化本地数据,需要读取配置文件,小编介绍如何通过xml简单写入配置文件,至于为什么选择这个格式,主要因为在qml中有一个很好 的类使用:XmlListModel xmlListModel能直接读 ...
- android文件读写
public class service { private Context context;//引入上下文对象,要使用其中的方法openFileOutput public service ...
最新文章
- (转载)IIS安装配置全过程
- 学长毕业日记 :本科毕业论文写成博士论文的神操作20170408
- linux中内核中machine_desc,Linux-内核-学习笔记(13):移植三星官方内核
- web相关基础知识1
- PHP、mysql面试题 (附答案+实现代码)
- 基于物理着色(二)- Microfacet材质和多层材质
- 广东省的盆友们,这波退税及时雨你赶上了吗?
- HttpClient 教程 (六)
- FPGA MCU FSMC通信接口——NAND Flash模式
- 利用谷歌浏览器模拟网速慢的情况
- 一道狗血的ACM题:Poker Hands
- 今日恐慌与贪婪指数为15 恐慌程度小幅上升
- Axure RP9制作汉堡菜单
- Ubuntu下安装NS-3图文教程
- Python 图片转icon
- 如何创建一个Windows软件
- Wine Mono 环境安装并运行.Net WPF
- 视频监控平台GB28181:媒体流保活机制
- 电子信息工程与计算机就业现状,电子信息工程现状及发展前景
- 中秋节灯谜趣事:古人中秋节…
热门文章
- Continuous Intervals Gym - 102222L(2018宁夏邀请赛暨2019银川icpc网络预选赛)
- ibd文件如何变为json_如何轻松了解 Python 必学的 django 框架?
- oracle查询pga大小GB,Oracle如何调整SGA和PGA的大小
- python定时器库_Python定时器完整示例 python定时器用法举例
- 微软修复工具_微软正在推出更新以修复此前被发现的Windows 10 SFC问题
- 【算法竞赛学习】数字中国创新大赛智慧海洋建设-Task2数据分析
- java求多项式回归_多项式回归(Polynomial Regression)(附代码)
- Codeforce-CodeCraft-20 (Div. 2)-B. String Modification (找规律+模拟)
- POJ 3267为什么优先队列超时,DP就能过,难过
- Source Insight Source Navigator使用技巧