android实现文件读写功能
作者:小新110 发布时间:2022-03-10 16:31:23
标签:android,文件读写
本文实例为大家分享了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://blog.csdn.net/cau_eric/article/details/90410761
0
投稿
猜你喜欢
- 概念:有enum关键字修饰的类,成为枚举类1、枚举规则枚举类的对象可以有类里面定义,不支持重新new出来,枚举类有构造函数,其他的类都一样,
- 概述在mvn clean install时,控制台各种报错,大概有:java:[8,52] 程序包com.xxx不存在java:[98,27
- 在介绍程序实现之前,我们先看下Android中Activities和Task的基础知识。我们都知道,一个Activity 可以启动另一个Ac
- Java Exception 捕获和显示实例详解 在进行Java B/S架构开发时,经常有这样的场景:后端处理业务逻辑,前端负责
- yaml介绍YAML(YAML Ain't Markup Language),一种数据序列化格式优点:容易阅读容易与脚本语言交互以数
- 不一致,那么用来接收查询出来的result对应的数据将会是Null,如果不使用resultMap,那么一般为了避免pojo对象对应的属性为N
- 本文实例为大家分享了JavaWeb实现用户登录与注册功能的具体代码,供大家参考,具体内容如下用到的知识客户端:HTML CSS JS (JQ
- JDK * :利用反射机制生成一个实现代理接口的匿名类,在调用具体方法前调用InvokeHandler来处理。CGlib * :利用AS
- 最近在做项目时需要对异常进行全局统一处理,主要是一些分类入库以及记录日志等,因为项目是基于Springboot的,所以去网络上找了一些博客文
- 前言在之前的文章中,使用mybatis-plus生成了对应的包,在此基础上,我们针对项目的api接口,添加swagger配置和注解,生成sw
- 这篇文章主要介绍了MyBatis执行Sql的流程实例解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的
- 本文实例讲述了java之swing下拉菜单实现方法。分享给大家供大家参考。具体如下:import java.awt.*;import jav
- 一、PointCut接口/* * Copyright 2002-2012 the original author or authors. *
- 一.递归方法1.递归就是自己调用本身的方法,前提是有方法。2.递归使用找出递归的规律递归要有出口条件,也就是结束条件3.注意事项递归次数不能
- 运行效果C#实现using Android.App;using Android.OS;using Android.Widget;namesp
- java 基础之JavaBean属性命名规范问题JavaBean属性名要求:前两个字母要么都大写,要么都小写下面我们来找找如果不遵循这个规范
- 相应的类库可在我的资源页面中找到,关于类成员的说明可通过对象浏览器查看函数说明Imports BitOperatorLibrary.Shif
- 1.标识符①用于给变量、类和方法命名(类名首字母大写,变量和方法名首字母小写并遵循驼峰原则)②标识符的命名规范: ■ 标识符必须
- 本文实例为大家分享了C#实现围棋游戏的具体代码,供大家参考,具体内容如下之所以选择围棋作为大作业一方面是想挑战一下,另一方面是由于从6岁学围
- 实践过程效果代码public partial class Form1 : Form {