9

Read the file and write it in another file

 2 years ago
source link: https://www.codesd.com/item/read-the-file-and-write-it-in-another-file.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Read the file and write it in another file

advertisements

I need to write a file in java based on reading multiple file templates.

File template 1:

010 Date 131231 131231 131231 131231 131231 131231
020 NAME 131231 131231 131231 131231 131231 131231
030 YEAR 131231 131231 131231 131231 131231 131231
090 xxx  131231 131231 131231 131231 131231 131231

File Template 2 :

010 Date 131231 131231 131231 131231 131231 131231
040 NAME 131231 131231 131231 131231 131231 131231
050 YEAR 131231 131231 131231 131231 131231 131231
060 YEAR 131231 131231 131231 131231 131231 131231
090 xxx  131231 131231 131231 131231 131231 131231

From the above two templates its clear, no of lines may vary from template to template.

The key words in the template such as date name year etc will me replaced by the value entered by the user and ll be written as a file.

User can select one template alone or combine two templates in a single file. Ie if user selects one template then the values entered by user will replace the keywords and will writtern in a file and saved.

If user selects two templates then both the templates have to be read and saved as a single file.

Expected o/p of such scenario using both templates mentioned above :

010 uservalue 131231 131231 131231 131231 131231 131231
020 uservalue 131231 131231 131231 131231 131231 131231
030 uservalue 131231 131231 131231 131231 131231 131231
040 uservalue 131231 131231 131231 131231 131231 131231
050 uservalue 131231 131231 131231 131231 131231 131231
060 uservalue 131231 131231 131231 131231 131231 131231
090 uservalue 131231 131231 131231 131231 131231 131231

From the above example we can see lines in between the first and last lines of both templates are written together but first and last line are not repeated. Because first and last line are header and footer, they cant come twice.

Another scenario is user can select one template and request for multiple rows

Expected output using template 1(assume user requests for 2 rows(loops):

 010 uservalue 131231 131231 131231 131231 131231 131231
 020 uservalue 131231 131231 131231 131231 131231 131231
 030 uservalue 131231 131231 131231 131231 131231 131231
 020 uservalue 131231 131231 131231 131231 131231 131231
 030 uservalue 131231 131231 131231 131231 131231 131231
 090 uservalue 131231 131231 131231 131231 131231 131231

Even here header and footer is not included under the loop condition, only the line in between are repeated. My Code :

for(int i=o;i<loopcount;i++){
FileReader fr = new FileReader("C:/Templates/"
                        + template[i]);
                BufferedReader br = new BufferedReader(fr);
String putData=null,verify;

while ((verify = br.readLine()) != null) {

                            if (verify != null) {
                                putData = verify.replace("YYYYMMDD", yyyymmdd);

                                putData = putData.replace("IIIIIIIIIIIIIII",
                                        imsi);
                                putData = putData.replace("DD", duration);
                                putData = putData.replace("HHMMSS", startTime);
                                putData = putData.replace("hhmmss", endTime);
                                putData = putData.replace("XXXXXXXXX", msisdn);
                                putData = putData.replace("UUUU", uplink);
                                putData = putData.replace("LLLL", downlink);
bw.append(putData + "\n");

                            }
                        }}

                        bw.flush();
                        bw.close();

                        br.close();


You can count the lines in the template.. store the 1st and nth value loop the file content if 1 or n matches with the line number .. store them separately...

perform string manipulations... append all variable and write them into another file...


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK