ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Write Text, XML, HTML to the IFS

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Write Text, XML, HTML to the IFS

    The following code provides a convenience class to write files to the ifs using a combination of java and RPG. In order to run the code you must download the java file and place it in a directory within the ifs. Compile the java code and add a classpath entry for the directory where the class resides.
    You can use this class to write any text based file. i.e. Text, Html, Xml.


    Java Code:

    Code:
    /*
     * Created on Feb 15, 2006
     *  FileWriterApp: is a convenience class that creates a file in the specified directory and allows the 
     *  caller to write text to said file.  Use it to write HTML, TEXT, XML...
     *  @author KpMac
     *  Vertical Software Sytems LLC.
     *  VSS.Biz 
     */
    
    //package biz.vss.tools;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    public class FileWriterApp {
    
    	private File file;
    
    	private String fileName;
    
    	private PrintWriter out;
    
    	public FileWriterApp(String fileName) {
    		this.fileName = fileName;
    		createNewFile();
    		createWriter();
    	}
    
    	/**
    	 * @return Returns the fileName.
    	 */
    	public String getFileName() {
    		return fileName;
    	}
    
    	/**
    	 * @param fileName
    	 *            The fileName to set.
    	 */
    	public void setFileName(String fileName) {
    		this.fileName = fileName;
    	}
    
    	public void writeLine(String line) {
    		out.println(line);
    		if (out.checkError()) {
    			System.err.println("An Output error has Occurred");
    		}
    	}
    
    	public void flushAndClose() {
    		out.flush();
    		out.close();
    		out = null;
    	}
    
    	/**
    	 * Method createNewFile() Checks to see if the file name already exists.
    	 * If the file exists the new fileName will be fileName(i+1) where i is a number
    	 * indicating the last file created.
    	 */
    
    	private void createNewFile() {
    		file = new File(this.fileName);
    		String string = "";
    		String extension = "";
    		int position = file.getName().lastIndexOf(".");
    
    		if (position >= 0) {
    			string = file.getName().substring(0, position);
    			extension = file.getName().substring(position);
    		} else {
    			string = file.getName();
    		}
    
    		int i = 1;
    
    		try {
    			while (!file.createNewFile()) {
    				file = new File(string + "(" + i + ")" + extension);
    				i++;
    			}
    		} catch (IOException ie) {
    			ie.printStackTrace();
    		}
    		setFileName(file.getName());
    
    	}
    
    	/**
    	 * Method createWriter():  initializes the PrintWriter. 
    	 */
    
    	private void createWriter() {
    		try {
    			out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
    		} catch (IOException ie) {
    			ie.printStackTrace();
    		}
    	}
    }
    RPG Prototypes
    Code:
    HDFTACTGRP(*NO) ACTGRP(*CALLER) THREAD(*SERIALIZE)                    
     *-------------------------------------------------------------------*
     *                                                                   *
     *  Programmer Name ......: KpMac                             *
     *  Date Created .........: XX/XX/XXXX                               *
     *  Application ..........: File Writer Procedures                   *
     *                                                                   *
     *-------------------------------------------------------------------*
     *          L O G   O F   M O D I C I C A T I O N S                  *
     *                                                                   *
     * Autor ID | Flag | Date Mod |   Description of modification        *
     * ---------|------|----------|------------------------------------- *
     * xxxxxxxx | xx01 | mm/dd/yy | xxxxxx                               *
     *-------------------------------------------------------------------*
     *                                                                    
     *-------------------------------------------------------------------*
     *                          Stand Alone                              *
     *-------------------------------------------------------------------*
     *                                                                    
    D fw              S               O   CLASS(*JAVA:'FileWriterApp')    
     *-------------------------------------------------------------------*
     *                           Procedures                              *
     *-------------------------------------------------------------------*
     *                                                                    
     * Creates FileWriterApp(String _fileName)                            
    DcrtFileWriter    PR              O   EXTPROC(*JAVA:                  
    D                                     'FileWriterApp':*CONSTRUCTOR)   
    D                                 O   CLASS(*JAVA:'java.lang.String') 
    D                                     Const                           
     * writes a Line out to the File                                      
    D writeLine       PR                  EXTPROC(*JAVA:                  
    D                                     'FileWriterApp':                
    D                                             'writeLine')            
    D                                 O   CLASS(*JAVA:'java.lang.String') 
    D                                     Const                           
    D                                                                     
     * Closes the File and Flushes the Buffer                             
    D flushAndClose   PR                  EXTPROC(*JAVA:                  
    D                                     'FileWriterApp':                
    D                                             'flushAndClose')        
     * Converts EBCIDIC TO ASCII (500 charectars)                         
    D fwStr           PR              O   EXTPROC(*JAVA:                  
    D                                             'java.lang.String':     
    D                                             *CONSTRUCTOR)           
    D                                     CLASS(*JAVA:'java.lang.String') 
    D RPGBytes                     500A   Const varying                   
     * Converts EBCIDIC TO ASCII (3000 charectars)                        
    D fwBigStr        PR              O   EXTPROC(*JAVA:                  
    D                                             'java.lang.String':     
    D                                             *CONSTRUCTOR)           
    D                                     CLASS(*JAVA:'java.lang.String') 
    D RPGBytes                    3000A   Const varying                   
     * Converts EBCIDIC TO ASCII (5000 charectars)                        
    D fwHugStr        PR              O   EXTPROC(*JAVA:                    
    D                                             'java.lang.String':       
    D                                             *CONSTRUCTOR)             
    D                                     CLASS(*JAVA:'java.lang.String')   
    D RPGBytes                    5000A   Const varying                     
    D*                                                                      
     * Converts ASCII TO EBCIDIC                                            
    D fwRPGStr        PR         32767A   Varying                           
    D                                     EXTPROC(*JAVA:                    
    D                                             'java.lang.String':       
    D                                             'getBytes')
    RPG Code Example
    Code:
     *-------------------------------------------------------------------*
     *                                                                   *
     *  Programmer Name ......: KpMac                                    *
     *  Date Created .........: XX/XX/XXXX                               *
     *  Application ..........:                                          *
     *                                                                   *
     *-------------------------------------------------------------------*
     *          L O G   O F   M O D I C I C A T I O N S                  *
     *                                                                   *
     * Autor ID | Flag | Date Mod |   Description of modification        *
     * ---------|------|----------|------------------------------------- *
     * xxxxxxxx | xx01 | mm/dd/yy | xxxxxx                               *
     *-------------------------------------------------------------------*
     *                                                                    
     *-------------------------------------------------------------------*
     *                           Prototypes                              *
     *-------------------------------------------------------------------*
     *                                                                    
    D/COPY MYLIB/QRPGLESRC,P_FILEWRTR                                     
     *                                                                    
     *-------------------------------------------------------------------*
     *                          Data Structs                             *
     *-------------------------------------------------------------------*
     *                                                                    
     *-------------------------------------------------------------------*
     *                           Constants                               *
     *-------------------------------------------------------------------*
     *                                                                    
     *-------------------------------------------------------------------*
     *                          Stand Alone                              *
     *-------------------------------------------------------------------*
     *                                                                    
    D fw              S               O   CLASS(*JAVA:'FileWriterApp')    
     *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=*
     *                             Main                                  *
     *=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=*
     *                                                                    
     /Free                                                                
                                                                          
        fw = crtFileWriter(fwStr('/MyIfsDir/Test.Txt'));                  
                                                                          
        WriteLine(fw:fwStr('I am a test String'));                        
        WriteLine(fw:fwStr('I am a also a Test String'));                 
                                                                          
        flushAndClose(fw);                                                
                                                                          
        *InLr = *On;                                                      
     /End-Free                                                            
     *=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*
     *                           Procedures                              *
     *=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*
     *


    TESTFILEWRT.txt

    FileWriterApp.txt

    P_FILEWRTR.txt
    Last edited by kpmac; February 22, 2006, 11:22 AM.
    Predictions are usually difficult, especially about the future. ~Yogi Berra

    Vertical Software Systems
    VSS.biz
Working...
X