java文件操作之java寫文件簡單示例
代碼很簡單,直接上代碼,大家參考使用吧
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"); //在當(dāng)前包下,創(chuàng)建文件
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;
}
}
}
相關(guān)文章
SpringBoot與Spring中數(shù)據(jù)緩存Cache超詳細(xì)講解
我們知道內(nèi)存讀取速度遠(yuǎn)大于硬盤讀取速度,當(dāng)需要重復(fù)獲取相同數(shù)據(jù)時,一次一次的請求數(shù)據(jù)庫或者遠(yuǎn)程服務(wù),導(dǎo)致在數(shù)據(jù)庫查詢或者遠(yuǎn)程方法調(diào)用上小號大量的時間,最終導(dǎo)致程序性能降低,這就是數(shù)據(jù)緩存要解決的問題,學(xué)過計算機(jī)組成原理或者操作系統(tǒng)的同學(xué)們應(yīng)該比較熟悉2022-10-10
Spring SpringMVC,Spring整合MyBatis 事務(wù)配置的詳細(xì)流程
這篇文章給大家介紹SSM整合詳細(xì)流程步驟 Spring SpringMVC,Spring整合MyBatis 事務(wù)配置,本文通過實(shí)例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-10-10
Java并發(fā)編程之詳解CyclicBarrier線程同步
在之前的文章中已經(jīng)為大家介紹了java并發(fā)編程的工具:BlockingQueue接口,ArrayBlockingQueue,DelayQueue,LinkedBlockingQueue,PriorityBlockingQueue,SynchronousQueue,BlockingDeque接口,ConcurrentHashMap,CountDownLatch,本文為系列文章第十篇,需要的朋友可以參考下2021-06-06
Spring boot基于ScheduledFuture實(shí)現(xiàn)定時任務(wù)
這篇文章主要介紹了Spring boot基于ScheduledFuture實(shí)現(xiàn)定時任務(wù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06

