Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Splitting a input file by colons? Java
- public void loadAccts(){
- try{
- // Open the file that is the first
- // command line parameter
- FileInputStream fstream = new FileInputStream(chooseFile());
- // Get the object of DataInputStream
- DataInputStream in = new DataInputStream(fstream);
- BufferedReader br = new BufferedReader(new InputStreamReader(in));
- String strLine;
- //Read File Line By Line
- while ((strLine = br.readLine()) != null) {
- String[] values = strLine.split("n");
- for (String str : values) {
- txtUsers.append(strLine + "n");
- System.out.println(values.toString());
- }
- }
- //Close the input stream
- in.close();
- }catch (Exception e){//Catch exception if any
- System.err.println("Error: " + e.getMessage());
- }
- }
- ...
- String namepass[] = strLine.split(":");
- String name = namepass[0];
- String pass = namepass[1];
- // do whatever you want to name and pass
- ...
- import java.util.ArrayList;
- import java.util.List;
- class Account {
- String username;
- String password;
- public Account(String username, String password) {
- super();
- this.username = username;
- this.password = password;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
- class Solution {
- public static void main(String[] args) {
- while(.....){//read till the end of the file
- String input = //each line
- List<Account> accountsList = new ArrayList<Account>();
- String splitValues[] = input.split(":");
- Account account = new Account(splitValues[0], splitValues[1]);
- accountsList.add(account);
- }
- //perform your operations with accountList
- }
- }
Add Comment
Please, Sign In to add comment