Android文件读写

一、 JAVA

Android中没有提供像J2SE里面多的让人抓狂的有关于IOAPI。所以使用起来非常简单轻巧。

Android系统中,这些文件保存在 /data/data/PACKAGE_NAME/files 目录下。

二、 数据读取

view plaincopy to clipboardprint?

public static String read(Context context, String file) {   

String data = "";   

try {   

FileInputStream stream = context.openFileInput(file);   

StringBuffer sb = new StringBuffer();   

int c;   

while ((c = stream.read()) != -1) {   

sb.append((char) c);   

}   

stream.close();   

data = sb.toString();   

  

} catch (FileNotFoundException e) {   

} catch (IOException e) {   

}   

return data;   

}  

public static String read(Context context, String file) {

String data = "";

try {

FileInputStream stream = context.openFileInput(file);

StringBuffer sb = new StringBuffer();

int c;

while ((c = stream.read()) != -1) {

sb.append((char) c);

}

stream.close();

data = sb.toString();

} catch (FileNotFoundException e) {

} catch (IOException e) {

}

return data;

}

从代码上,看起来唯一的不同就是文件的打开方式了: context.openFileInput(file); Android中的文件读写具有权限控制,所以使用context(Activity的父类)来打开文件,文件在相同的Package中共享。这里的 Package的概念同Preferences中所述的Package,不同于Java中的Package

三、 数据写入

view plaincopy to clipboardprint?

public static void write(Context context, String file, String msg) {   

try {   

FileOutputStream stream = context.openFileOutput(file,   

Context.MODE_WORLD_WRITEABLE);   

stream.write(msg.getBytes());   

stream.flush();   

stream.close();   

} catch (FileNotFoundException e) {   

} catch (IOException e) {   

}   

}  

public static void write(Context context, String file, String msg) {

try {

FileOutputStream stream = context.openFileOutput(file,

Context.MODE_WORLD_WRITEABLE);

stream.write(msg.getBytes());

stream.flush();

stream.close();

} catch (FileNotFoundException e) {

} catch (IOException e) {

}

}

在这里打开文件的时候,声明了文件打开的方式。

一般来说,直接使用文件可能不太好用,尤其是,我们想要存放一些琐碎的数据,那么要生成一些琐碎的文件,或者在同一文件中定义一下格式。其实也可以将其包装成Properties来使用:

view plaincopy to clipboardprint?

public static Properties load(Context context, String file) {   

Properties properties = new Properties();   

try {   

FileInputStream stream = context.openFileInput(file);   

properties.load(stream);   

} catch (FileNotFoundException e) {   

} catch (IOException e) {   

}   

return properties;   

}   

  

public static void store(Context context, String file, Properties properties) {   

try {   

FileOutputStream stream = context.openFileOutput(file,   

Context.MODE_WORLD_WRITEABLE);   

properties.store(stream, "");   

} catch (FileNotFoundException e) {   

} catch (IOException e) {   

}   

}  编程中文件读写是少不了的,如下:

四、 读:

public String ReadSettings(Context context){ 

      FileInputStream fIn = null; 

      InputStreamReader isr = null; 

      

      char[] inputBuffer = new char[255]; 

      String data = null; 

      

      try{ 

       fIn = openFileInput("settings.dat");       

          isr = new InputStreamReader(fIn); 

          isr.read(inputBuffer); 

          data = new String(inputBuffer); 

          Toast.makeText(context, "Settings read",Toast.LENGTH_SHORT).show(); 

          } 

          catch (Exception e) {       

          e.printStackTrace(); 

          Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show(); 

          } 

          finally { 

             try { 

                    isr.close(); 

                    fIn.close(); 

                    } catch (IOException e) { 

                    e.printStackTrace(); 

                    } 

          } 

          return data; 

     } 

五、 写:

    public void WriteSettings(Context context, String data){ 

      FileOutputStream fOut = null; 

      OutputStreamWriter osw = null; 

      

      try{ 

       fOut = openFileOutput("settings.dat",MODE_PRIVATE);       

          osw = new OutputStreamWriter(fOut); 

          osw.write(data); 

          osw.flush(); 

          Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show(); 

          } 

          catch (Exception e) {       

          e.printStackTrace(); 

          Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show(); 

          } 

          finally { 

             try { 

                    osw.close(); 

                    fOut.close(); 

                    } catch (IOException e) { 

                    e.printStackTrace(); 

                    } 

          } 

     }

使用方法:

WriteSettings(this,"setting0, setting1, setting2");

String data[] = ReadSettings(this).split(","); 

 初学Android, 初学JAVA,很不专业的找了一下相关资料,很不专业的还愣是没看全明白,还是老大给了个方案,照着做,还好没错。

在网上有看到过说写SD卡的路径要双斜杠,如://sdcard//t.txt,但我试下来双斜杠,单斜杠都可以。

写两个小函数,以供调用

六、 删除文件函数

输入参数:文件名(全路径如 "/sdcard/test.txt"

public boolean DeleteFile(String filename)

  {

   File file;

   

   file = new File(filename);

   if (file.exists())

    file.delete();

   else

    return false;

   return true;

  }

  

  

七、 写文件函数

,输入参数:文件名, 输入缓冲首地址,数据长度

  public boolean WriteFile(String filename, char [] str, int length)

     {

      try 

      {

       FileWriter fw = new FileWriter(filename);

       fw.write(str, 0, length);

       fw.flush();

       fw.close();

      }

      catch(IOException e)

      {

       e.printStackTrace();

       return false;

      }

      return true;

     }

  

  

八、  读文件函数

,输入参数:文件名, 读取缓冲首地址,数据长度

  public boolean ReadFile(String filename, char [] str, int length)

     {     

      try 

      {

       FileReader fr = new FileReader(filename);

       fr.read(str, 0, length);

       fr.close();

    }

    catch(IOException e)

    {

     e.printStackTrace();   

     return false;

    }

    return true;

     } 

九、 C实验:FILE*

FILE* file;

file = fopen("/data/file.dat", "w");//打开创建失败,可能是权限UID问题

if (file != NULL)

{

NOX_ANDROID_DEBUG_LOG_CUR("open data file :", file);

}

else

{

NOX_ANDROID_DEBUG_LOG_CUR("open data file error:", file);

}

NOX_ANDROID_DEBUG_LOG_CUR("GAME START END", 1);

file = fopen("/data/data/com.example.hellojni/file.dat", "w");//成功,程序自己目录下面,说明属于本进程用户

if (file != NULL)

{

NOX_ANDROID_DEBUG_LOG_CUR("open hellojni file :", file);

}

else

{

NOX_ANDROID_DEBUG_LOG_CUR("open hellojni file error:", file);

}

NOX_ANDROID_DEBUG_LOG_CUR("GAME START END", 1);

file = fopen("/data/M16.png", "w");//是一个用超级权限放里去的文件,已经存在,打开也成功

if (file != NULL)

{

NOX_ANDROID_DEBUG_LOG_CUR("open M16 file :", file);

}

else

{

NOX_ANDROID_DEBUG_LOG_CUR("open M16 file error:", file);

}

NOX_ANDROID_DEBUG_LOG_CUR("GAME START END", 1);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值