简体   繁体   中英

Read from file and write it to another file

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...

I'm not quite sure I understand the logic of the parameters-replacement in your code so I can't suggest an actual edit, but if you need to handle the first and last files in a special manner, you can try something like:

... {preceding code}
    verify = br.readLine());
    putData = verify.replace("YYYYMMDD", yyyymmdd);
    bw.append(putData + "\n");

    String strTempValue = null;
    boolean isLastLine = false;

    do {
        strTempValue = br.readLine();
        isLastLine = (null == strTempValue);
        if (verify != null)
        { 
            if(isLastLine)
            {
                // your footer logic
            }
            else
            {
                // file template body - "normal lines"
                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);

                verify = strTempValue;
            }

            bw.append(putData + "\n");
        }
    }
    while ((verify = br.readLine()) != null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM