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:


2001total visits,3visits today

Leave a Reply

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