cancel
Showing results for 
Search instead for 
Did you mean: 

Methods to read a file?

Former Member
0 Kudos

Hello,

I was wondering if there are any methods to directly read a file into the PI system. I have an installation where partners push files to a directory that is on my CI via SFTP and it appears the only way to access the files via the File Adapter is to either FTP back to ourselves, or use NFS. I was wondering if there were more "graceful" methods to bring the file and payload into the system which is more normal for Java, such as buffering, etc... to not overload memory.

For example:

File file = new File("/data/myfile.txt");

FileInputStream fis = null;

BufferedInputStream bis = null;

DataInputStream dis = null;

Any advice or recommendations is welcomed.

Thanks,

Mike

Accepted Solutions (0)

Answers (3)

Answers (3)

Former Member
0 Kudos

Hi,

From a Java mpping in PI, you can use any of the Java file i\o methods to read a file into PI. However, this would mean that you need a dummy process to start the interface flow, say something like a dummy file in a directory.

Other way can be to use a Java proxy, where you will push the file into the PI integration engine pipleline url. So in the Java proxy code, you have the process to push the fil einto PI.

However, the file adapter itself is a very good option, since it can be monitored from the RWB, and incase you want any added functionality, you can always go for a custom module.

The only draw back that I can see for the file adapter is that we can not do SFTP using this. For SFTP, we need to use a third party adapter like Seeburger.

Regards

baskar_gopalakrishnan2
Active Contributor
0 Kudos

If you want sample code see below. Highly recommending to use BufferedInputStream for the efficient file reading operation and for even larger size files.

import java.io.*;
class FileReader 
{
 public static void main(String args[])
  {
  try{
  FileInputStream fstream = new FileInputStream("YourFile.txt");
  DataInputStream in = new DataInputStream(fstream);
  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  StringBuffer  buffer = new StringBuffer();
  String strLine;
  //Read File Line By Line
  while ((strLine = br.readLine()) != null)   {
    buffer.append(strLine);
  }
  in.close();
   }catch (Exception e){
      System.err.println("Error: " + e.getMessage());
    }
  return buffer.toString();
  }
}

former_member854360
Active Contributor
0 Kudos

Hi Michel,

You can use the below code to read file from NFS.

import java.io.*;

public class FileRead {
  public static String main(String[] args) {
    File file = new File("C:/file.txt");
    int ch;
    StringBuffer strContent = new StringBuffer("");
    FileInputStream fin = null;
    try {
      fin = new FileInputStream(file);
      while ((ch = fin.read()) != -1)
        strContent.append((char) ch);
      fin.close();
    } catch (Exception e) {
      System.out.println(e);
    }
    return strContent.toString();
  }
}