java写文件 java文件操作之java写文件简单示例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

java写文件 java文件操作之java写文件简单示例

  2021-03-19 我要评论
想了解java文件操作之java写文件简单示例的相关内容吗,在本文为您仔细讲解java写文件的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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"); //在当前包下,创建文件
  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;
  }
 }
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们