欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android实现文件读写功能

程序员文章站 2023-08-15 22:55:48
本文实例为大家分享了android实现文件读写功能的具体代码,供大家参考,具体内容如下 读取: public static string _getjsonstr...

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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。