Apex Example – Reading Data from Text file

Reading Data from Text file

Example: This scenario is for inserting a file of email ids at a time by reading from a textfile. Here we used apex:inputfile for uploading a file.
In this scenario all the email ids of textfile must be separated by comma.
<apex:page sidebar=”false” controller=”FileReadingClassxxx”>
<apex:form >
<apex:inputfile value=”{!fileBody}”/>
<apex:commandButton value=”ReadDataIntoMyObject” action=”{!readContent}”/>

public with sharing class FileReadingClassxxx {
String fileContent=”;
public PageReference readContent() {
fileContent = fileBody.toString();
List<string> allemails = fileContent.split(‘,’);
EmailSpace__c myemail = new EmailSpace__c();
List<EmailSpace__c> lstemails = new List<EmailSpace__c>();
for(Integer i=0;i<allemails.size();i++)
myemail = new EmailSpace__c(name=allemails[i]);
insert lstemails;
return null;
public blob fileBody { get; set; }

One thought on “Apex Example – Reading Data from Text file”

Thank you for visiting this tutorial website!! We will review your comment and getback to you shotly.