Guest User

Untitled

a guest
Jun 6th, 2016
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. package servlet1.webshop;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import java.util.StringTokenizer;
  9. public class UsersList {
  10.  
  11. private ArrayList<User> allUsers = new ArrayList<User>();
  12.  
  13. public UsersList (String path) {
  14. BufferedReader in = null;
  15. try {
  16. File file = new File (path + "/Users.txt");
  17. System.out.println(file.getCanonicalPath());
  18. in = new BufferedReader(new FileReader(file));
  19. readUsers (in);
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23. finally {
  24. if (in != null) {
  25. try {
  26. in.close();
  27. } catch (Exception e) {
  28. e.printStackTrace();
  29. }
  30. }
  31. }
  32. }
  33.  
  34. private void readUsers (BufferedReader in) {
  35. String line;
  36. String username = "";
  37. String password = "";
  38. StringTokenizer st;
  39.  
  40. try {
  41. while ((line = in.readLine()) != null) {
  42. line = line.trim();
  43. if (line.equals("") || line.indexOf("#") == 0)
  44. continue;
  45. st = new StringTokenizer(line, ";");
  46. while (st.hasMoreTokens()) {
  47. username = st.nextToken().trim();
  48. password = st.nextToken().trim();
  49. }
  50. allUsers.add(new User (username, password));
  51. }
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. }
  56.  
  57. public List<User> getAllUsers() {
  58. return allUsers;
  59. }
  60.  
  61. public void setAllUsers(ArrayList<User> allUsers) {
  62. this.allUsers = allUsers;
  63. }
  64.  
  65.  
  66. }
Add Comment
Please, Sign In to add comment