How to Copy Files from One Location to Another Using a Java Transformation © 2013 Informatica Corporation. No part of this document may be reproduced or transmitted in any form, by any means (electronic, photocopying, recording or otherwise) without prior consent of Informatica Corporation. All other company and product names may be trade names or trademarks of their respective owners and/or copyrighted materials of such owners. Abstract You can copy files from a flat file source to a flat file target using a Java transformation in PowerCenter. Supported Versions PowerCenter 8.5.x and later Table of Contents Abstract ............................................................................................................................................ 2 Supported Versions ......................................................................................................................... 2 Table of Contents ............................................................................................................................ 2 Overview .......................................................................................................................................... 2 Sample Mapping .............................................................................................................................. 2 Java Transformation ........................................................................................................................ 2 Java Code ........................................................................................................................................ 3 Overview You can use a Java transformation to copy files from a source location to a target location in PowerCenter. You use a Java transformation to evaluate conditions and copy files during midstream mapping processing. Sample Mapping The following figure shows a sample mapping that copies a flat file from a source location to a target location using a Java transformation: Java Transformation To copy a file from a source location to target location, perform the following tasks: 1. Include the Java .jar file in the following path: [PowerCenter Installation Directory]/server/bin/javalib 2. Insert the Java code in the On Input Row tab of the Java transformation. 2 The following figure shows the sample Java code in the On Input Row tab: Java Code Enter the following Java code in the On Input Row tab: try{ File fromFile = new File(File_filePath); File toFile = new File(DXFilePath); if (!fromFile.exists()) throw new IOException("FileCopy: " + "no such source file: " + File_filePath); if (!fromFile.isFile()) throw new IOException("FileCopy: " + "can't copy directory: " + File_filePath); if (!fromFile.canRead()) throw new IOException("FileCopy: " + "source file is unreadable: " + File_filePath); if (toFile.isDirectory()) toFile = new File(toFile, fromFile.getName()); if (toFile.exists()) { if (!toFile.canWrite()) 3 throw new IOException("FileCopy: " + "destination file is unwriteable: " + File_filePath); String response = "Y"; if (!response.equals("Y") && !response.equals("y")) throw new IOException("FileCopy: " + "existing file was not overwritten."); } else { String parent = toFile.getParent(); if (parent == null) parent = System.getProperty("user.dir"); File dir = new File(parent); if (!dir.exists()) throw new IOException("FileCopy: " + "destination directory doesn't exist: " + parent); if (dir.isFile()) throw new IOException("FileCopy: " + "destination is not a directory: " + parent); if (!dir.canWrite()) throw new IOException("FileCopy: " + "destination directory is unwriteable: " + parent); } FileInputStream from = null; FileOutputStream to = null; try { from = new FileInputStream(fromFile); to = new FileOutputStream(toFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = from.read(buffer)) != -1) to.write(buffer, 0, bytesRead); // write } finally { if (from != null) try { from.close(); } catch (IOException e) { ; } if (to != null) try { to.close(); } catch (IOException e) { ; } } } catch(Exception ex){ ErrorMessage = "unable to move file from "+ File_filePath +" to location: "+ DXFilePath +". "+ ex.getMessage(); } Authors Tanmoy De Senior Consultant, Informatica Professional Services 4 Acknowledgements The Informatica Documentation team would like to acknowledge Tanmoy De, Senior Consultant, Informatica Professional Services for the technical content of this article. 5