Guest User

Untitled

a guest
Jul 26th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. Splitting a input file by colons? Java
  2. public void loadAccts(){
  3. try{
  4. // Open the file that is the first
  5. // command line parameter
  6.  
  7. FileInputStream fstream = new FileInputStream(chooseFile());
  8.  
  9. // Get the object of DataInputStream
  10. DataInputStream in = new DataInputStream(fstream);
  11. BufferedReader br = new BufferedReader(new InputStreamReader(in));
  12. String strLine;
  13. //Read File Line By Line
  14. while ((strLine = br.readLine()) != null) {
  15. String[] values = strLine.split("n");
  16.  
  17. for (String str : values) {
  18.  
  19.  
  20. txtUsers.append(strLine + "n");
  21. System.out.println(values.toString());
  22.  
  23. }
  24.  
  25.  
  26. }
  27. //Close the input stream
  28. in.close();
  29.  
  30. }catch (Exception e){//Catch exception if any
  31. System.err.println("Error: " + e.getMessage());
  32. }
  33. }
  34.  
  35. ...
  36. String namepass[] = strLine.split(":");
  37. String name = namepass[0];
  38. String pass = namepass[1];
  39. // do whatever you want to name and pass
  40. ...
  41.  
  42. import java.util.ArrayList;
  43. import java.util.List;
  44.  
  45. class Account {
  46. String username;
  47. String password;
  48.  
  49. public Account(String username, String password) {
  50. super();
  51. this.username = username;
  52. this.password = password;
  53. }
  54.  
  55. public String getUsername() {
  56. return username;
  57. }
  58.  
  59. public void setUsername(String username) {
  60. this.username = username;
  61. }
  62.  
  63. public String getPassword() {
  64. return password;
  65. }
  66.  
  67. public void setPassword(String password) {
  68. this.password = password;
  69. }
  70.  
  71. }
  72.  
  73. class Solution {
  74. public static void main(String[] args) {
  75. while(.....){//read till the end of the file
  76. String input = //each line
  77. List<Account> accountsList = new ArrayList<Account>();
  78. String splitValues[] = input.split(":");
  79. Account account = new Account(splitValues[0], splitValues[1]);
  80. accountsList.add(account);
  81. }
  82. //perform your operations with accountList
  83. }
  84. }
Add Comment
Please, Sign In to add comment