android实现文件读写功能

作者:小新110 时间:2022-03-10 16:31:23 

本文实例为大家分享了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

标签:android,文件读写
0
投稿

猜你喜欢

  • C#逆变与协变详解

    2021-10-25 08:07:54
  • 一文带你了解C#中抽象方法与虚方法的区别

    2023-07-23 00:14:32
  • Java面试题之HashMap 的 hash 方法原理是什么

    2022-09-11 20:20:54
  • C#实现窗体抖动的两种方法

    2021-10-06 10:20:52
  • 详解android与服务端交互的两种方式

    2022-11-08 00:24:51
  • 打印Java程序的线程栈信息方式

    2021-11-02 19:00:28
  • JAVA JNI函数的注册过程详细介绍

    2023-02-07 18:41:17
  • SpringBoot Mybatis动态数据源切换方案实现过程

    2022-10-12 03:25:03
  • C#下解析HTML的两种方法介绍

    2021-07-03 16:08:21
  • jdk-logging log4j logback日志系统实现机制原理介绍

    2022-03-22 11:45:28
  • Java单元测试Powermockito和Mockito使用总结

    2021-11-12 14:59:07
  • Android开发自学笔记(二):工程文件剖析

    2021-09-08 11:12:56
  • Java探索之Feign入门使用详解

    2023-08-18 03:38:06
  • C#锁住文件的操作步骤

    2022-07-18 13:41:20
  • android阅读器长按选择文字功能实现代码

    2023-09-16 08:48:42
  • socket编程时的发送与接收数据时的问题解析

    2022-12-31 03:57:28
  • Java 1.8使用数组实现循环队列

    2022-02-11 04:00:10
  • JAVA中的final关键字用法实例详解

    2021-06-25 12:10:08
  • 利用Java生成带有文字的二维码

    2022-05-21 15:01:38
  • Java网络编程实例——简单模拟在线聊天

    2023-08-20 04:39:42
  • asp之家 软件编程 m.aspxhome.com