samedi 19 mars 2016

I want to Implement my java I/O code to get the specific name in text file

Here is my text file And I my java code to read this.

text file -
name: john birthday: 1991
name: brayn birthday: 1994

java code-

import java.io.*;

class Detail{
    public static void main(String[] args) throws Exception {
        users u=new users();
        if(args.length==1){
            System.out.println("No Details");
        }else{
            FileReader reader = new FileReader("users.txt");
            BufferedReader buffer= new BufferedReader(reader);

            String line = null;

            while ((line = buffer.readLine()) != null) {
                u.addToSearchString(line+'\n');
            }
            buffer.close();
            u.printName();
            }
        }
} 

class users{

    String names;

    users(){
        names=new String();
    }

    void addToSearchString(String add){
        names += add;
    }

    void printName() {
        int i = 0;
        int find, start, end;
        while ((find = names.indexOf("name:", i))!=-1){
            start = find + 5;
            end = names.indexOf("\t", start);
            System.out.println(names.substring(start, end));
            i = end + 1;
        }
    }
}


I want to read only name brayn how can I implement my code to get the name:brayn.

Aucun commentaire:

Enregistrer un commentaire