采用openFileOutput获取输出流
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获取输出流的更多相关文章
- C#开发微信门户及应用(14)-在微信菜单中采用重定向获取用户数据
我曾经在系列文章中的<C#开发微信门户及应用(11)--微信菜单的多种表现方式介绍>中介绍了微信菜单里面的重定向操作,通过这个重定向操作,我们可以获取一个code值,然后获取用户的open ...
- springboot 采用HttpClient获取天气预报 异常及原因
采用httpClient调用天气预报地址获取出现异常 2018-10-04 15:18:25.815 ERROR 10868 --- [nio-8080-exec-5] o.a.c.c.C.[.[.[ ...
- 一个采用python获取股票数据的开源库,相当全,及一些量化投资策略库
tushare: http://tushare.waditu.com/index.html 为什么是Python? 就跟javascript在web领域无可撼动的地位一样,Python也已经在金融量化 ...
- js采用正则表达式获取地址栏参数
getQueryString:function(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*) ...
- 详解Android数据存储技术
前言 学习Android相关知识,数据存储是其中的重点之一,如果不了解数据,那么让你跟一款没有数据的应用玩,你能玩多久呢?答案是这和没有手机几乎是差不多的.我们聊QQ,聊微信,看新闻,刷朋友圈等都是看 ...
- java 网络编程基础 TCP/IP协议:服务端ServerSocket;客户端Socket; 采用多线程方式处理网络请求
1.Java中客户端和服务器端通信的简单实例 Java中能接收其他通信实体连接请求的类是ServerSocket,ServerSocket对象用于监听来自客户端的Socket连接,如果没有连接,它将一 ...
- 用JS获取地址栏参数的方法
采用正则表达式获取地址栏参数: function GetQueryString(name) { var reg = new RegExp("(^|&)"+ nam ...
- java URL实现调用其他系统发送报文并获取返回数据
模拟本系统通过Url方式发送报文到目标服务器,并获取返回数据:(实现类) import java.io.BufferedOutputStream; import java.io.BufferedRea ...
- 用JS获取地址栏参数的方法(超级简单)
方法一:采用正则表达式获取地址栏参数:( 强烈推荐,既实用又方便!) function GetQueryString(name) { var reg = new RegExp("( ...
随机推荐
- rpc的学习
rpc(Remote process call 即远程过程调用)是一种请求-相应的协议, 主要使用于C/S架构中,使得分布式系统成为可能.由客户端发起请求,服务端调用各种参数处理请求,当服务器在处理请 ...
- Effective C++ 第二版 10) 写operator delete
条款10 写了operator new就要同时写operator delete 写operator new和operator delete是为了提高效率; default的operator new和o ...
- JAVA ThreadPoolExecutor(转)
原文链接:http://blog.csdn.net/historyasamirror/article/details/5961368 基础 在我看来,java比C++的一个大好处就是提供了对多线程的支 ...
- js遍历对象的属性并且动态添加属性
var person= { name: 'zhangsan', pass: '123' , 'sni.ni' : 'sss', hello:function (){ for(var i=0;i< ...
- Codeforces 18C C. Stripe
Codeforces 18C C. Stripe 链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=86640#problem/E 题 ...
- C#用正则表达式 获取网页源代码标签的属性或值
1.有url获取到网页源代码: using System.Web; using System.IO; using System.Net; private void GetHtmlinfo(string ...
- was服务器下修改jsp无效果
使用了jsp静态导入,修改了导入的jsp页面,但是目标页面没出现效果.先说下静态导入和动态导入的区别: include指令用于引入其它JSP页面,如果使用include指令引入了其它JSP页面,那么J ...
- Spring学习之杂七杂八
源码解读Spring IOC原理 - http://www.cnblogs.com/ITtangtang/p/3978349.html 1. 使用Groovy配置Bean http://blog.cs ...
- javascript学习(9)——[设计模式]单例
单例模式,相信大家对此都不陌生,我们主要讲下javascript中几个比较常见的设计模式: (1).普通的单体 (2).具有局部变量的强大单体 (3).惰性单体 (4).分支单体 下面我们就一一进行介 ...
- WEB ICON 的探讨
一般考虑到webicon 就是cssSprite和自定义font:本文基于下述而总结和进行分析,如有笔误有望指出,谢谢 在线教程:用字体在网页中画ICON图标 http://www.imooc.com ...