Transforming a .txt file into two strings in java -


if have text file called "login.txt" - in same folder executable jar - contains id:username password:password best way transform id , password contents 2 strings can replace them in like: api.login("username", "password"); p.s. no spaces in username , password, bad.

edit: @ api.login("username", "password"); username , password whould had , wanted transform strings api.login(id, password); file

to reiterate: conditions:

  • the format id:username password:password
  • id static string
  • password static string
  • username never contains spaces.

you can extract data simple regex:

id:(?<username>[^\s]++)\s++password:(?<password>.*+) 

for example:

public user extractcredentials(final string data) {     final pattern pattern = pattern.compile("id:(?<username>[^\\s]++)\\s++password:(?<password>.*+)");     final matcher matcher = pattern.matcher(data);     if (!matcher.matches()) {         throw new illegalargumentexception("invalid data format");     }     return new user(matcher.group("username"), matcher.group("password")); }  public class user {     private final string name;     private final string password;      public user(string name, string password) {         this.name = name;         this.password = password;     }      //getter       @override     public string tostring() {         return string.format("user{name='%s', password='%s'}", name, password);     } } 

output:

system.out.println(extractcredentials("id:username password:password"));  user{name='username', password='password'} 

Comments