欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android实现文件读写功能

时间:2021-02-18 11:21:06|栏目: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();
 }
 }

上一篇:Android SharedPreferences存储的正确写法

栏    目:Android代码

下一篇:android仿微信联系人索引列表功能

本文标题:android实现文件读写功能

本文地址:http://www.codeinn.net/misctech/65428.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有