Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
dilipkkp2412
Contributor
0 Kudos
In this blog, SAP PI UDF example is been provided for below functionalities:

  1. To read file contents of a file from sap application directory,

  2. here read only those files which names starts with some key words

  3. To archive same file to other sap application directory

  4. 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 !");
}

}

 
2 Comments
Labels in this area