0

I have a text file which contains many phone numbers. I want to read each number from the text file and paste them into another text file with a different text format. How i will do that? please help me

I want read the text file which contains these numbers

3434214280
3044559080
3154356865
3312430124
3334491537

and paste them into another text file with this format

BEGIN:VCARD
VERSION:2.1
N:;UNKNOWN 1;;;
FN:UNKNOWN 1
TEL;CELL;PREF:3434214280
END:VCARD
Kavka
  • 4,191
  • 16
  • 33
user3416078
  • 11
  • 2
  • 8

1 Answers1

0

I suggest you to read your first file then read template file which has your format and replace for example $VCARDNO$ from this template with your number then save the new format after replacement. For read file, you can read this How can I read a text file in Android? or search on it. For write to file, you can read this How To Read/Write String From A File In Android or search on it.

You can make the following: 1- Create template file (or template string) has the following format and save it:

BEGIN:VCARD
VERSION:2.1
N:;UNKNOWN 1;;;
FN:UNKNOWN 1
TEL;CELL;PREF:$REALVCARDNO$
END:VCARD

2- Read your card number files and load them to list for example. 3- Read the template file and keep it fixed. 4- Loop on the list and take the value which you read it from the template file then replace $REALVCARDNO$ with the current value on the list and save the new value in the output file which you need.

If you decide to change the file format later based on new requirement, you will change only in the template without making any change in the code.

Example in code:

    private void processTemplate() {
    final String template = "BEGIN:VCARD" + "\n" + "VERSION:2.1" + "\n" + "N:;UNKNOWN 1;;;" + "\n" + "FN:UNKNOWN 1" + "\n" + "TEL;CELL;PREF:$REALVCARDNO$" + "\n" + "END:VCARD";
    List<String> cardNumberList = new ArrayList<String>();
    //here you should read your file which has the card number and fill cardNumberList with it instead of the following two lines
    cardNumberList.add("3456854778");
    cardNumberList.add("3545456634");
    for (String cardNo : cardNumberList) {
        String myNewStringToSave = template.replace("$REALVCARDNO$", cardNo);
        //here you can save myNewStringToSave to file if you need to save each card number with the above format in single file
    }
}
Community
  • 1
  • 1
Mina Tadros
  • 514
  • 6
  • 18
  • i know how to read from this example http://stackoverflow.com/questions/9600356/how-to-copy-my-files-from-one-directory-to-another-directory but how ifil change format when writtrn in another file – user3416078 Mar 23 '14 at 14:55
  • I added example for you. Check it. – Mina Tadros Mar 23 '14 at 15:10
  • i serch this sample code which copy one text file from one location to another http://www.codeofaninja.com/2013/04/copy-or-move-file-from-one-directory-to.html but how i will change tamplete??? – user3416078 Mar 23 '14 at 15:28
  • Think simply, If you need to replace part of the string what you will do? You will make something like the following: String templateString = "This is my template $REALVCARDNO$"; String myNewString = templateString.replace("$REALVCARDNO$", "your new card number"); Simply you can make the template as string and you can replace your card number like this example or you can make it inside file and you will read the file content then replace this pattern using replace() method. – Mina Tadros Mar 23 '14 at 15:54
  • help me their my fulll code is here http://stackoverflow.com/questions/22593183/changing-byte-when-copy-file-not-working – user3416078 Mar 23 '14 at 16:01
  • yes but so many problems u give hardcoded numbers in my senario so many hundred of number in textfile also is not load textfile frominput and save in output see my original code is here check my this post http://stackoverflow.com/questions/22593183/changing-byte-when-copy-file-not-working – user3416078 Mar 23 '14 at 16:20
  • You code logic is wrong. Just follow what I said to you. First read the cards from the file as in this post http://stackoverflow.com/questions/12421814/how-to-read-text-file-in-android and instead of text.append(line); add the line to the list. When you finish reading input line, loop on the list and do replace the cardno with the value inside the list then save the file as in this post stackoverflow.com/questions/14376807/how-to-read-write-string-from-a-file-in-android. – Mina Tadros Mar 23 '14 at 16:28
  • have u see my code?? http://stackoverflow.com/questions/22593183/changing-byte-when-copy-file-not-working – user3416078 Mar 23 '14 at 16:30
  • so confusing please hep me – user3416078 Mar 23 '14 at 16:40