java文件操作之java写文件简单示例
时间:2023-10-21 14:37:13
代码很简单,直接上代码,大家参考使用吧
package com.it.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.content.Context;
public class LoginService {
/**
* 保存文件
* @param context 上下文
* @param username
* @param password
* @return
*/
public static boolean saveUserInfo(Context context,String username,String password){
File file=new File(context.getFilesDir(),"user.bat"); //在当前包下,创建文件
try {
FileOutputStream fis = new FileOutputStream(file);
fis.write((username+"##"+password).getBytes());
fis.close();
return true;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}
}
/**
* 回显用户名 密码
* @param context
* @return
*/
public static Map<String,String> getUserInfo(Context context){
File file=new File(context.getFilesDir(),"user.bat");
try {
Map<String,String> map=new HashMap<String, String>();
FileInputStream fis = new FileInputStream(file);
BufferedReader br=new BufferedReader(new InputStreamReader(fis));
String str=br.readLine();
String[] infos=str.split("##");
map.put("username", infos[0]);
map.put("password", infos[1]);
return map;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
深入理解Kotlin的泛型系统
![](https://img.aspxhome.com/file/2023/9/138679_0s.png)
Spring Boot 与 kotlin 使用Thymeleaf模板引擎渲染web视图的方法
java 模仿拼多多红包递减算法的实现
使用Java Minio搭建自己的文件系统详解
![](https://img.aspxhome.com/file/2023/7/130907_0s.jpg)
Android基础教程数据存储之文件存储
![](https://img.aspxhome.com/file/2023/3/86743_0s.png)
Android开发环境安装和配置图文教程
![](https://img.aspxhome.com/file/2023/0/138530_0s.png)
C++类的返回值是*this的成员函数问题
java实现小球碰撞功能
![](https://img.aspxhome.com/file/2023/5/64105_0s.jpg)
Java OOP三大特征之封装继承与多态详解
![](https://img.aspxhome.com/file/2023/2/67632_0s.png)
Android实现给TableLayou绘制边框的方法
![](https://img.aspxhome.com/file/2023/4/137734_0s.png)
Springboot整合Dozer实现深度复制的方法
Java修饰符 abstract,static,final 的区别详解
C#四舍五入用法实例
简单聊聊c# 事件
![](https://img.aspxhome.com/file/2023/9/78189_0s.png)
Java命令行下Jar包打包小结
![](https://img.aspxhome.com/file/2023/7/70517_0s.jpg)
Android线程实现图片轮播
![](https://img.aspxhome.com/file/2023/5/106015_0s.jpg)
用C#实现启动另一程序的方法实例
解析spring事务管理@Transactional为什么要添加rollbackFor=Exception.class
![](https://img.aspxhome.com/file/2023/4/72314_0s.png)
C#编程自学之开篇介绍
![](https://img.aspxhome.com/file/2023/0/116440_0s.png)
简略分析Android的Retrofit应用开发框架源码
![](https://img.aspxhome.com/file/2023/4/93304_0s.png)