/**
* 创建用户类额
* @author square 凉
*
*/
public class User {
private String userName;
private String password;
private String Name;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public User(String userName, String password, String name) {
super();
this.userName = userName;
this.password = password;
Name = name;
}
//重写tostring方法
@Override
public String toString() {
return "userName:" + userName + ", password:" + password + ", Name:" + Name;
}
}
-----------------------------------------------------------------------------------------------------------------
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 通过代码读入文档中的内容,实现内容的规定显示
* @author square 凉
*
*/
public class Test2Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1 标识文件
File file = new File("G:\\英雄时刻\\user.data");
// //创建输出流对象
// FileOutputStream output = new FileOutputStream(file,true);
// String str = new User("zhaoliu", "zl123", "赵六").toString();//将对象信息写入文档
// if(file.length() > 0) {
// str = ";" + str;
// }
// byte[] bytes = str.getBytes();
// output.write(bytes, 0, bytes.length);
// output.flush();
// output.close();
/*
* 读取标识文件,并按规定输出显示
*/
readShow(file);
}
private static void readShow(File file) throws IOException {
// TODO Auto-generated method stub
FileInputStream input = new FileInputStream(file);//创建输入流
byte[] bytes = new byte[4096];//定义字节数组,存储读入的数据
int num;
String str = null;
while ((num = input.read(bytes, 0, bytes.length)) != -1) {
str = new String(bytes, 0, num);
}
String[] splituser = str.split(";");
String username = null;
String password = null;
String name = null;
System.out.println("编号" + "\t" + "用户名" + "\t" + "密码" + "\t" + "姓名");
for (int i = 0; i < splituser.length; i++) {
String[] splitshux = splituser[i].split(",");
for (int j = 0; j < splitshux.length; j++) {
String[] split = splitshux[j].split(":");
if (j == 0) {
username = split[1];
} else if (j == 1) {
password = split[1];
} else {
name = split[1];
}
}
System.out.println(i + 1 + "\t" + username + password + "\t" + name);
}
input.close();
}
}
原文地址:https://www.cnblogs.com/IT-xiaoliang/p/10773460.html