这是一个模拟atm的 一行三个值 分别是账户 密码 和余额
我可以从txt里读出数据 就是不知道取了钱、存了钱以后我该怎么更新txt里的数据
package atm;
/**
* Created by lenovo on 2016/5/5.
*/
import java.awt.List;
import java.io.*;
import java.util.HashMap;
import java.util.Scanner;
public class c {
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
int line=1;
Scanner sc = new Scanner(System.in);
System.out.println("请输入账户");
int a=sc.nextInt();
HashMap m=new HashMap();
while((lineTxt = bufferedReader.readLine())!= null){
String[] lin=lineTxt.split(" ");
m.put(lin[0]+lin[1],lin[2]);
int zh=Integer.parseInt(lin[0]);
int mm=Integer.parseInt(lin[1]);
if(a==zh){
System.out.println("请输入密码");
int b=sc.nextInt();
if(b==mm){
int g=0;
while(g<4) {
int money = Integer.parseInt(lin[2]);
System.out.println("1.查看余额\n2.取款\n3.存款\n4.退出\n");
int c=sc.nextInt();
{
switch (c) {
case 1:
System.out.println("您账户内余额为:" + money);
g = 1;
break;
case 2:
System.out.println("输入您要取的金额:");
int d = sc.nextInt();
if (d > money) {
System.out.println("余额不足!");
g = 2;
} else {
money -= d;
System.out.println("您账户内余额为:" + money);
g = 2;
}
break;
case 3:
System.out.println("请输入你要存的金额:");
int e = sc.nextInt();
money += e;
System.out.println("您账户内余额为:" + money);
g = 3;
break;
case 4:break;
default:
System.out.println("1.查看余额\n2.取款\n3.存款\n4.退出\n");
g = sc.nextInt();
return;
}
}
}
}
else
{
System.out.println("您输入的密码错误");
}
}else{
System.out.println("您输入的账户不存在");
}
}
read.close();
}else{
System.out.println("找不到指定的文件");
}
}
catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String arg[]){
String filePath = "C:\\Users\\lenovo\\Desktop\\11.txt";
readTxtFile(filePath);
}
}
public class c {
public static void readTxtFile(String filePath){
try {
String encoding="GBK";
File file=new File(filePath);
if(file.isFile() && file.exists()){ //判断文件是否存在
InputStreamReader read = new InputStreamReader(
new FileInputStream(file),encoding);//考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
int line=1;
System.out.println("请输入账户");
Scanner sc = new Scanner(System.in);
int a=sc.nextInt();
HashMap m=new HashMap();
ArrayList<String> lineList = new ArrayList();
boolean isOper = false;//是否做存款取款动作了,钱没变化就没必要写文件了
boolean isLogined = false;//是否已经登录过,登陆过就不用再循环找是否还有相同用户了
while((lineTxt = bufferedReader.readLine())!= null){//先把文件读出来
lineList.add(lineTxt);
}
bufferedReader.close();
read.close();
for(int i=0;i<lineList.size()&&!isLogined;i++){
lineTxt = lineList.get(i);
String[] lin=lineTxt.split(" ");
m.put(lin[0]+lin[1],lin[2]);
int zh=Integer.parseInt(lin[0]);
int mm=Integer.parseInt(lin[1]);
if(a==zh){
System.out.println("请输入密码");
int b=sc.nextInt();
if(b==mm){
isLogined = true;
int g=0;
while(g<4) {
int money = Integer.parseInt(lin[2]);
System.out.println("1.查看余额\n2.取款\n3.存款\n4.退出\n");
g=sc.nextInt();
{
switch (g) {
case 1:
System.out.println("您账户内余额为:" + money);
break;
case 2:
System.out.println("输入您要取的金额:");
int d = sc.nextInt();
if (d > money) {
System.out.println("余额不足!");
} else {
money -= d;
System.out.println("您账户内余额为:" + money);
}
lin[2] = money+"";//这里需要重新赋值
isOper = true;
break;
case 3:
System.out.println("请输入你要存的金额:");
int e = sc.nextInt();
money += e;
System.out.println("您账户内余额为:" + money);
lin[2] = money+"";//这里需要重新赋值
isOper = true;
break;
case 4:
lineTxt = lin[0] + " " + lin[1] + " " + lin[2];
lineList.set(i, lineTxt);//更新余额
break;
default:
System.out.println("1.查看余额\n2.取款\n3.存款\n4.退出\n");
g = sc.nextInt();
}
}
}
}
else
{
System.out.println("您输入的密码错误");
}
}else{
System.out.println("您输入的账户不存在");
}
}
if(isOper){//写文件
FileWriter fw = new FileWriter(file);
//遍历clist集合写入到fileName中
for (String str: lineList){
fw.write(str);
fw.write("\n");
}
//刷新缓冲区
fw.flush();
//关闭文件流对象
fw.close();
}
}else{
System.out.println("找不到指定的文件");
}
}
catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public static void main(String arg[]){
String filePath = "C:\\11.txt";
readTxtFile(filePath);
}
}
Java操作文件可以读写的,如果数据变化就重写回文件就可以了。就是api的使用了。
更新数据其实就是对文件的读和写操作,但是要注意的是做好同步处理,还有互斥操作。否则可能读取到的数据会出错。我个人建议写一个专门处理这些操作的管家类,这样会很方便。希望对你有用