Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package xx;
- import java.io.BufferedWriter;
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- public class FileHandler {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- String username;
- String password;
- Scanner scanner = new Scanner(System.in);
- BufferedWriter buffWriter= null;
- FileWriter fileWriter = null;
- for (int i=0;i<3;i++) {
- try {
- username = scanner.next();
- password = scanner.next();
- User user = new User(username, password);
- File file = new File("someNewFile.txt");
- fileWriter = new FileWriter(file.getAbsoluteFile());
- buffWriter= new BufferedWriter(fileWriter);
- buffWriter.write(user.getUsername());
- buffWriter.write(user.getPassword());
- buffWriter.flush();
- System.out.println("Ready!");
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- if(buffWriter!=null ){
- try {
- buffWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }if(fileWriter != null){
- try {
- fileWriter.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
- }
- package xx;
- public class MyException extends Exception{
- @Override
- public String getMessage() {
- return "Bad";
- }
- }
- package xx;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class UserValidation {
- private static Pattern pattern;
- private static Pattern passwordPattern;
- private static Matcher matcher;
- private static final String USERNAME_PATTERN = "^[a-z]{3,10}$";
- private static final String PASSWORD_PATTERN = "^[A-Z]{5,10}%";
- public UserValidation(){
- pattern = Pattern.compile(USERNAME_PATTERN);
- passwordPattern = Pattern.compile(PASSWORD_PATTERN);
- }
- public static boolean usernameValidator(String username) {
- matcher = pattern.matcher(username);
- return matcher.matches();
- }
- public static boolean passwordValidator(String password) {
- matcher = passwordPattern.matcher(password);
- return matcher.matches();
- }
- }
- package xx;
- public class User {
- public User(String username, String password) {
- super();
- this.username = username;
- this.password = password;
- }
- String username;
- String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) throws MyException {
- if (!UserValidation.usernameValidator(username))throw new MyException();
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password)throws MyException {
- if(!UserValidation.passwordValidator(password))throw new MyException();
- this.password = password;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement