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. Python经常使用第三方工具、库、骨架

    Python ImagingLibrary(PIL):它提供强大的图形处理的能力,并提供广泛的图形文件格式支持.该库能进行图形格式的转换.打印和显示.还能进行一些图形效果的处理,如图形的放大.缩小和旋 ...

  2. .net Windows服务程序和安装程序制作图解

    最近项目中用到window服务程序,以前没接触过,比较陌生,花了两天的时间学习了下,写了个简单的服务,但在制作安装程序的时候,参照网上很多资料,却都制作不成功,可能是开发环境或项目配置的不同,这里把自 ...

  3. Android中使用NDK

    首先用Android Studio下载NDK 这个比较简单,就不多说了 1.写调用jni的Java代码 新建一个JniUtils类 public class JniUtils { static { S ...

  4. 数据库基础(子查询练习、链接查询(join on 、union)及其练习)

    子查询练习一:查询销售部里的年龄大于35岁的人的所有信息 练习二:将haha表中部门的所有数字代码转换为bumen表中的字符串显示 练习三:将haha表中部门的所有数字代码转换为bumen表中的字符串 ...

  5. 分布式session

    前端用户请求经过随机分发之后,可能会命中后端任意的Web Server,并且 Web Server 也可能会因为各种不确定的原因宕机.在这种情况下,session 是很难在集群间同步的,而通过将ses ...

  6. YIi配置debug工具、yii配置gii工具

    $config['bootstrap'][] = 'debug';$config['modules']['debug'] = [ 'class'=>'yii\debug\Module', 'al ...

  7. NOI 能量采集

    /** 大意: 求解 在[1,n] x, [1,m] y,之间有多少个gcd(x,y) = d d = min(n,m) 思路: 对于任意一个d 在[1,n] x, [1,m] y, gcd(x,y) ...

  8. 用macports装了一份openssl

    我已经用macports装了一份openssl,然后自己又编译了一份openssl....第三方给Mac出的一个类似BSD Ports的一个软件包管理工具装的话只需要sudo port install ...

  9. Multiple markers at this line @Override的解决方法

    Multiple markers at this line - implements java.awt.event.ActionListener.actionPerformed - The metho ...

  10. Google瓦片地图算法解析

    基本概念: 地图瓦片地址:http://mt2.google.cn/vt/lyrs=m@167000000&hl=zh-CN&gl=cn&x=420&y=193& ...