SAP PI UDF to Read File Contents
In this blog, SAP PI UDF example is been provided for below functionalities:
- To read file contents of a file from sap application directory,
- here read only those files which names starts with some key words
- To archive same file to other sap application directory
- Post Archival, delete the file from source path
Java Imports required in UDF as
- java.io.BufferedReader
java.io.FileReader
java.io.IOException
Java UDF Execution Type is “All Values of Queue”
Java UDF Code is as below:
public void file_Read_Move_Delete(ResultList result, Container container)
throws StreamTransformationException{
String RequestDataStr = "XYZ"; //File Name Starting Key
String FilePath_str = "/folder1/folder2/"; //File directory
String FileArchival_Path = "/folder1/folder2/folder2/"; //File Archival Path
String fileContentStr = "";
boolean StartsWithStr_Found = false;
File[] dirFiles = new File(FilePath_str).listFiles();
if (dirFiles.length > 0){ //Check if Directory is not empty
for (int i=0; i<dirFiles.length; i++){ //Loop Iteration for Each File
String fileNameStr = dirFiles[i].getName(); //Get File Name
if (fileNameStr.startsWith(RequestDataStr)){ //Check if Filename starts with Key 'XYZ'
StartsWithStr_Found = true;
//Start of reading file content --------------------------
BufferedReader br = null;
try{
String sCurrentLine;
br = new BufferedReader(new FileReader(FilePath_str + fileNameStr));
while ((sCurrentLine = br.readLine()) != null){
fileContentStr = fileContentStr + sCurrentLine;
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace(); }
}
result.addValue(fileContentStr); //return each file content
//End of reading file content --------------------------
//Start of File Archival (i.e. move file from one Folder to other folder -----------
File inputFile = new File(FilePath_str + fileNameStr);
//Start of Method-01 ---------------
//inputFile.renameTo(new File(FileArchival_Path + file.getName()));
//End of Method-01 -----------------
//Start of Method-02 ---------------
InputStream inStream = null;
OutputStream outStream = null;
try{
File bfile =new File(FileArchival_Path + dirFiles[i].getName());
inStream = new FileInputStream(inputFile);
outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
//copy the file content in bytes
while ((length = inStream.read(buffer)) > 0){
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
}catch(IOException e){
e.printStackTrace();
}
inputFile.delete(); //Delete same file from Source Folder (i.e. FilePath_str)
//End of Method-02 ---------------
//End of File Archival (i.e. move file from one Folder to other folder -------------
}//End of "fileName check
}// End of Folder for loop
}// End of directory length check
//If no file found
if (StartsWithStr_Found = false){
result.addValue("File Not found !");
}
}
Hi Dilip!
Thanks for sharing this. Just a couple of questions:
Regards, Evgeniy.
Dear Evgeniy Kolmakov
About above UDF, which is part of a SAP-PI's "SOAP-TO-FILE Synchronous Inbound" interface scenario, please find below details:
Business requirement was like,
Now please find below answers of your queries:
Hope, above details will make more clarity on UDF's use.
Thanks & Regards,
Dilip