package cn.itcast.buffered;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;
public class Demo1 {
public static void main(String[] args) throws IOException {
File file = new File("F:\\1208project\\day21\\src\\cn\\itcast\\buffered\\Demo1.java");
FileReader fileReader = new FileReader(file);
String line = null;
while((line = myReadLine(fileReader))!=null){
System.out.println(line);
}
}
public static String myReadLine(FileReader fileReader) throws IOException{
StringBuilder sb = new StringBuilder();
int content = 0 ;
while((content = fileReader.read())!=-1){
if(content=='\r'){
continue;
}else if(content=='\n'){
break;
}else{
sb.append((char)content);
}
}
if(content ==-1){
return null;
}
return sb.toString();
}
public static void readTest1() throws IOException{
File file = new File("F:\\a.txt");
FileReader fileReader = new FileReader(file);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while((line = bufferedReader.readLine())!=null){
System.out.println(Arrays.toString("aaa".getBytes()));
}
bufferedReader.close();
}
}
package cn.itcast.buffered;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class Demo2 {
public static void main(String[] args) throws IOException {
File file = new File("F:\\a.txt");
FileWriter fileWriter = new FileWriter(file,true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write("\r\n");
bufferedWriter.write("前两天李克强来萝岗!!");
bufferedWriter.flush();
}
}
package cn.itcast.buffered;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Login {
static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
while(true){
System.out.println("请选择功能: A(注册) B(登陆)");
String option = scanner.next();
if("a".equalsIgnoreCase(option)){
reg();
}else if("b".equalsIgnoreCase(option)){
login();
}else{
System.out.println("你的输入有误,请重新输入...");
}
}
}
public static void login() throws IOException{
System.out.println("请输入用户名:");
String userName = scanner.next();
System.out.println("请 输入密码:");
String password = scanner.next();
String info = userName+" "+ password;
BufferedReader bufferedReader = new BufferedReader(new FileReader("F:\\users.txt"));
String line = null;
boolean isLogin = false;
while((line = bufferedReader.readLine())!=null){
if(info.equals(line)){
isLogin = true;
break;
}
}
if(isLogin){
System.out.println("欢迎"+userName+"登陆成功...");
}else{
System.out.println("不存在该用户信息,请注册!!");
}
}
public static void reg() throws IOException{
System.out.println("请输入用户名:");
String userName = scanner.next();
System.out.println("请 输入密码:");
String password = scanner.next();
String info = userName+" "+ password;
File file = new File("F:\\users.txt");
FileWriter fileWriter = new FileWriter(file,true);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(info);
bufferedWriter.newLine();
bufferedWriter.close();
}
}