Skip to toolbar

File Handling || Multiple file creation using template based input data.

In our previous chapter, we have learned “How to read and write data into a text file?”. In this chapter, we learn how to create multiple files using a Template. The data in these templates would be populated from another file which is called Input datafile.

Template based file handling
Template based file handling

Understand template-based file handling

  1. Getting the content and structure of template.txt.
  2. Getting the content from the dataFile.txt.
  3. Merging the content based on the variable data. In the above template we kept “{name}” and “{subject}” as a variable. It means we have to replace these content with the data present in the dataFile.txt.
  4. Creating multiple files using the new content which we get after merging above content.



Code Example
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author ashok.kumar
 *
 */
public class Test {

	public static void main(String[] args) throws IOException 
	{
		// First of all we have to extract content from dataFile.txt
		// Here we have created two string arrays to store values returned from dataFile.txt
		String dataFileName[]=null;
		String dataFileSubject[]=null;
		
		// Locating dataFile.txt on local.
		String dataFile="inputs\\dataFile.txt";
		FileReader dfr=new FileReader(dataFile);
		BufferedReader dbr=new BufferedReader(dfr);
	
		String currLine=dbr.readLine();
		while(currLine!=null)
		{
			if(currLine.contains("name="))
			{
		    // Getting list of names from data file after removing text "name="
			currLine=currLine.substring(5);
			dataFileName=currLine.split(",");
			}
			
			if(currLine.contains("subject="))
			{
			 // Getting list of subjects from data file after removing text "subject="
			currLine=currLine.substring(8);
			dataFileSubject=currLine.split(",");
			}
			
			currLine=dbr.readLine();
		}
		
		int totalNames=dataFileName.length;
		for(int i=0;i<totalNames;i++)
		{
			String filePath="Output\\File_"+(i+1)+".txt";
			String name=dataFileName[i];
			String subject=dataFileSubject[i];
			
			readTemplate(filePath, name, subject);
		}
	}
	
	public static void readTemplate(String outputFileName,String name, String subject) throws IOException
	{
		// Create new file
		FileWriter fw=new FileWriter(outputFileName);
		
		// Next step is to read the content of template.txt file
				String nameVar="{name}";
				String subjectVar="{subject}";
				
				String inputTemplate="inputs\\template.txt";
				FileReader fr=new FileReader(inputTemplate);
				BufferedReader br=new BufferedReader(fr);
				
				String currentLine=br.readLine();
				while( currentLine!= null)
				{
					
					// Merging template data and values from dataFile.
					if(currentLine.contains(nameVar))
					{
						// Replacing variables data with the values recieved from dataFile.txt
						currentLine=currentLine.replace(nameVar, name);
					}
					if(currentLine.contains(subjectVar))
					{
						// Replacing variables data with the values recieved from dataFile.txt
						currentLine=currentLine.replace(subjectVar, subject);
					}
					
					// Writing updated/merged data into new text file.
					fw.write(currentLine+"\n");
					currentLine=br.readLine();
				}
				
				// Once all the updated data written on new file, close the file.
				fw.close();
	}
}

Related Links:

OOPs Concept:

OOPs Concept:

Java Question And Answer:


2843total visits,1visits today

Leave a Reply

Your email address will not be published. Required fields are marked *

Site Statistics

  • Users online: 0 
  • Visitors today : 2
  • Page views today : 2
  • Total visitors : 48,855
  • Total page view: 67,273

   YouTube ChannelQuora

            Ashok Kumar is working in an IT Company as a QA Consultant. He has started his career as a Test Trainee in manual testing in August 2010. Then he moves towards the automation testing after 4 years. He started learning JAVA and Selenium by self to get the knowledge of automation.

       While learning these tools and working on multiple projects, he found that sometimes people get stuck in live scenarios in their project and they have to do lots of RnD to get out of it. So he decided to start blogging only for such scenarios, where anyone facing any problem in their project, can ask any question or give a solution or you can say an alternate solution to achieve the goal successfully.

Later on, he observed that some people want to learn Java but they have few questions in their mind like how to start Java, whether we should go for the online or offline course. So he started writing tutorials on Java, Jira, Selenium, Excel etc.