Sie sind auf Seite 1von 5

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 J ava 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
J ava Transformation ........................................................................................................................ 2
J ava Code ........................................................................................................................................ 3

Overview
You can use a J ava transformation to copy files from a source location to a target location in
PowerCenter. You use a J ava transformation to evaluate conditions and copy files during mid-
stream 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 J ava transformation:


Java Transformation
To copy a file from a source location to target location, perform the following tasks:
1. Include the J ava .jar file in the following path: [PowerCenter Installation
Directory]/server/bin/javalib
2. Insert the J ava code in the On Input Row tab of the J ava transformation.

2





The following figure shows the sample J ava code in the On Input Row tab:

Java Code
Enter the following J ava code in the On Input Row tab:

t r y{
Fi l e f r omFi l e = new Fi l e( Fi l e_f i l ePat h) ;
Fi l e t oFi l e = new Fi l e( DXFi l ePat h) ;

i f ( ! f r omFi l e. exi st s( ) )
t hr ow new I OExcept i on( " Fi l eCopy: " + " no such sour ce f i l e: "
+ Fi l e_f i l ePat h) ;
i f ( ! f r omFi l e. i sFi l e( ) )
t hr ow new I OExcept i on( " Fi l eCopy: " + " can' t copy di r ect or y: "
+ Fi l e_f i l ePat h) ;
i f ( ! f r omFi l e. canRead( ) )
t hr ow new I OExcept i on( " Fi l eCopy: " + " sour ce f i l e i s unr eadabl e: "
+ Fi l e_f i l ePat h) ;

i f ( t oFi l e. i sDi r ect or y( ) )
t oFi l e = new Fi l e( t oFi l e, f r omFi l e. get Name( ) ) ;

i f ( t oFi l e. exi st s( ) ) {
i f ( ! t oFi l e. canWr i t e( ) )
3




t hr ow new I OExcept i on( " Fi l eCopy: "
+ " dest i nat i on f i l e i s unwr i t eabl e: " + Fi l e_f i l ePat h) ;
St r i ng r esponse = " Y" ;
i f ( ! r esponse. equal s( " Y" ) && ! r esponse. equal s( " y" ) )
t hr ow new I OExcept i on( " Fi l eCopy: "
+ " exi st i ng f i l e was not over wr i t t en. " ) ;
} el se {
St r i ng par ent = t oFi l e. get Par ent ( ) ;
i f ( par ent == nul l )
par ent = Syst em. get Pr oper t y( " user . di r " ) ;
Fi l e di r = new Fi l e( par ent ) ;
i f ( ! di r . exi st s( ) )
t hr ow new I OExcept i on( " Fi l eCopy: "
+ " dest i nat i on di r ect or y doesn' t exi st : " + par ent ) ;
i f ( di r . i sFi l e( ) )
t hr ow new I OExcept i on( " Fi l eCopy: "
+ " dest i nat i on i s not a di r ect or y: " + par ent ) ;
i f ( ! di r . canWr i t e( ) )
t hr ow new I OExcept i on( " Fi l eCopy: "
+ " dest i nat i on di r ect or y i s unwr i t eabl e: " + par ent ) ;
}

Fi l eI nput St r eamf r om= nul l ;
Fi l eOut put St r eamt o = nul l ;
t r y {
f r om= new Fi l eI nput St r eam( f r omFi l e) ;
t o = new Fi l eOut put St r eam( t oFi l e) ;
byt e[ ] buf f er = new byt e[ 4096] ;
i nt byt esRead;

whi l e ( ( byt esRead = f r om. r ead( buf f er ) ) ! = - 1)
t o. wr i t e( buf f er , 0, byt esRead) ; / / wr i t e
} f i nal l y {
i f ( f r om! = nul l )
t r y {
f r om. cl ose( ) ;
} cat ch ( I OExcept i on e) {
;
}
i f ( t o ! = nul l )
t r y {
t o. cl ose( ) ;
} cat ch ( I OExcept i on e) {
;
}
}

}
cat ch( Except i on ex) {
Er r or Message = " unabl e t o move f i l e f r om" + Fi l e_f i l ePat h +" t o l ocat i on:
" + DXFi l ePat h +" . " + ex. get Message( ) ;
}

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

Das könnte Ihnen auch gefallen