package com.example.login.service;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map; import android.annotation.SuppressLint;
import android.content.Context; /*
* mode 1私有、2可读、3可写、4可读写
*/
public class LoginService {
public static boolean savaUserInfo(Context context, String username, String password,int mode)
{
try{
//File file = new File("/data/data/com.example.login/info.txt");
//File file = new File(context.getFilesDir(),"info.txt");
//FileOutputStream fos = new FileOutputStream(file);
//FileOutputStream fos = context.openFileOutput("private.txt", context.MODE_PRIVATE);
FileOutputStream fos = null;
switch(mode)
{
case 1:
fos = context.openFileOutput("private.txt", context.MODE_PRIVATE);
break;
case 2:
fos = context.openFileOutput("readable.txt", context.MODE_WORLD_READABLE);
break;
case 3:
fos = context.openFileOutput("writeable.txt", context.MODE_WORLD_WRITEABLE);
break;
case 4:
fos = context.openFileOutput("public.txt", context.MODE_WORLD_READABLE + context.MODE_WORLD_WRITEABLE);
break;
}
fos.write((username + "##" + password).getBytes());
fos.close();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
} }

2、关于在另外项目测试文件的读写权限

 package com.example.other;

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void readPrivate(View view)
{
try
{
File file = new File("/data/data/com.example.login/files/private.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String result = br.readLine();
Toast.makeText(this, result, 0).show();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "读取私有文件失败!", 0).show();
}
} public void writePrivate(View view)
{
try
{
File file = new File("/data/data/com.example.login/files/private.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write("aaa".getBytes());
fos.close();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "写入私有文件失败!", 0).show();
}
} public void readPublic(View view)
{
try
{
File file = new File("/data/data/com.example.login/files/public.txt");
FileInputStream fis = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String result = br.readLine();
Toast.makeText(this, result, 0).show();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "读取私有文件失败!", 0).show();
}
} public void writePublic(View view)
{
try
{
File file = new File("/data/data/com.example.login/files/public.txt");
FileOutputStream fos = new FileOutputStream(file);
fos.write("aaa##bbb".getBytes());
fos.close();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "写入私有文件失败!", 0).show();
}
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

采用openFileOutput获取输出流的更多相关文章

  1. C#开发微信门户及应用(14)-在微信菜单中采用重定向获取用户数据

    我曾经在系列文章中的<C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍>中介绍了微信菜单里面的重定向操作,通过这个重定向操作,我们可以获取一个code值,然后获取用户的open ...

  2. springboot 采用HttpClient获取天气预报 异常及原因

    采用httpClient调用天气预报地址获取出现异常 2018-10-04 15:18:25.815 ERROR 10868 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[ ...

  3. 一个采用python获取股票数据的开源库,相当全,及一些量化投资策略库

    tushare: http://tushare.waditu.com/index.html 为什么是Python? 就跟javascript在web领域无可撼动的地位一样,Python也已经在金融量化 ...

  4. js采用正则表达式获取地址栏参数

    getQueryString:function(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*) ...

  5. 详解Android数据存储技术

    前言 学习Android相关知识,数据存储是其中的重点之一,如果不了解数据,那么让你跟一款没有数据的应用玩,你能玩多久呢?答案是这和没有手机几乎是差不多的.我们聊QQ,聊微信,看新闻,刷朋友圈等都是看 ...

  6. java 网络编程基础 TCP/IP协议:服务端ServerSocket;客户端Socket; 采用多线程方式处理网络请求

    1.Java中客户端和服务器端通信的简单实例 Java中能接收其他通信实体连接请求的类是ServerSocket,ServerSocket对象用于监听来自客户端的Socket连接,如果没有连接,它将一 ...

  7. 用JS获取地址栏参数的方法

    采用正则表达式获取地址栏参数: function GetQueryString(name) {      var reg = new RegExp("(^|&)"+ nam ...

  8. java URL实现调用其他系统发送报文并获取返回数据

    模拟本系统通过Url方式发送报文到目标服务器,并获取返回数据:(实现类) import java.io.BufferedOutputStream; import java.io.BufferedRea ...

  9. 用JS获取地址栏参数的方法(超级简单)

    方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) {      var reg = new RegExp("( ...

随机推荐

  1. linux内核源码阅读之facebook硬盘加速flashcache之二

    flashcache数据结构都在flashcache.h文件中,但在看数据结构之前,需要先过一遍flashcache是什么,要完成哪些功能?如果是自己设计这样一个系统的话,大概要怎么设计. 前面讲过, ...

  2. Node.mongoose

    简介 mongodb是一款面向文档的数据库,不是关系型数据库,新手熟悉mysql.sqlserver等数据库的人可能入手稍微困难些,需要转换一下思想,可以不需要有固定的存储模式,以文档模型为存储内容相 ...

  3. js静态方法和实例方法

    js静态方法 function foo(){} // 声明类 foo.method = function(){} // 方法体 使用:foo.method() js实例方法 function foo( ...

  4. c#关于EXCEL导出数据库的做法

    using System;using System.Diagnostics;using System.Collections;using System.Data;using System.Web;us ...

  5. gridview的高级使用

      后台数据 using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...

  6. jQuery源码,匿名函数自执行

    jQuery框架的首尾是这样写的()(), (function(window){//这个window是个入参,随便起个名字都行 //这里面全都是js代码 })(window)//这个括号里的windo ...

  7. hdu 4779 Tower Defense 2013杭州现场赛

    /** 题意: 有两种塔,重塔,轻塔.每种塔,能攻击他所在的一行和他所在的一列, 轻塔不 能被攻击,而重塔可以被至多一个塔攻击,也就是说重塔只能被重塔攻击.在一个n*m 的矩阵中,最少放一个塔,可放多 ...

  8. (Problem 47)Distinct primes factors

    The first two consecutive numbers to have two distinct prime factors are: 14 = 2  7 15 = 3  5 The fi ...

  9. BZOJ 1631: [Usaco2007 Feb]Cow Party

    题目 1631: [Usaco2007 Feb]Cow Party Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 491  Solved: 362[Sub ...

  10. java学习之动态代理模式

    package com.gh.dynaproxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Metho ...