Java Code For Read .CSV File And FTP Server But Data Writing In Single Line

- 1 answer

I have case Java code for read .CSV file which contains multiple lines data with headers and write into FTP server but data writing in single line.

Can any one guide how to get proper output same like input file.


    try {

        // Cookcounty FTP start
        if (EnrollmentEDI.Status != "FAILURE") {

            if (CountyPlanNumbersList.contains(samp.PlanNumber())) {
                CommonMessages.logGenericTrace("CountyFTp start");
                com.vitria.connectors.ftp.DynamicFTPTargetInfo CountyFTP = com.vitria.connectors.ftp.FTPConnectorUserLib.createDynamicFTPTargetInfo();
                FileInputStream fins = null;
                BufferedInputStream bins = null;
                DataInputStream dins = null;

                String line1 = "";
                File CookCoutyOut1 = new File(utility.CountyOutputFolderPath + "/" + ccformattedDate + ".csv");

                CommonMessages.logGenericTrace("CountyFileName=" + CookCoutyOut1);
                fins = new FileInputStream(CookCoutyOut1);
                bins = new BufferedInputStream(fins);
                dins = new DataInputStream(bins);
                while (dins.available() != 0) {
                    line1 = line1 + dins.readLine();
                if (!(line1.trim().equals(""))) {
                    CookCountyOriginal = line1;
                //  CommonMessages.logGenericTrace("CookCountyOriginal =" + CookCountyOriginal);
                String cookyFileName = ccformattedDate + ".csv";
                CommonMessages.logGenericTrace("cookyFileName =" + cookyFileName);
                EventDef newEventFile2 = (EventDef) (vtFileConnectorEvents.dataFileEventInterfaceHelper.getMetaObject().findDef("dataFileEvent"));
                Object[] outParamsFile2 = {CookCountyOriginal.getBytes(), cookyFileName};
                EventBody out3 = null;
                out3 = JctLib.createEventBody(newEventFile2, outParamsFile2);
            CommonMessages.logGenericTrace("CountyFTp END");

    } catch (Exception c) {
        CommonMessages.logGenericTrace(" *** INFORCE ENROLLMENT PROCESS : ERR IN County :-" + c.toString());
        FTPtrack = "" + c.toString();
        FTPtrack = "FTP TRANSFER TO MAINFRAME :" + FTPtrack.replaceAll(":", ",") + " - FAILED";
        EnrollmentEDI.Track = EnrollmentEDI.Track + " FTPEXCEPTION : " + FTPtrack + "\n";
        //CommonMessages.logGenericTrace("Exception in ACK FTPtrack======="+FTPtrack);
        EnrollmentEDI.Status = "FAILURE";
        EnrollmentEDI.Intouchtrack = EnrollmentEDI.Intouchtrack + "\n" + "FTPEXCEPTION :" + c.toString() + "\n-RESOLUTION:" + props.getProperty("5");
        com.glic.excp.handlers.GLICPCExcpHandler glicpc = new com.glic.excp.handlers.GLICPCExcpHandler();
        glicpc.handleComponentException(c, ctx);


The readLine() method only reads up to the newline. The string returned does not include the newline character. See the contract of DataInput#readLine.

To add the newline back on, you could replace this loop:

while (dins.available() != 0) {
    line1 = line1 + dins.readLine();;

with one that adds either "\n" or "\r\n" or System.getProperty("line.separator") to the end of each line read, depending on what kind of line endings the mainframe expects.

But there are a few other problems with this code. You should be checking for a null result from readLine() to detect end-of-file, not looking at dins.available(). And you should be using BufferedReader, not DataInputStream as the documentation suggests. DataInputStream#readLine has been deprecated since JDK 1.1.

The loop would look like this if you continue using available() and DataInputStream:

String lineSeparator = System.getProperty("line.separator");
while (dins.available() != 0) {
    line1 = line1 + dins.readLine() + lineSeparator;

Or like this in a more up-to-date style of Java, using BufferedReader, a try-with-resources statement, and just using "\n" for a newline instead of line.separator:

try (BufferedReader br = new BufferedReader(
        new InputStreamReader(new FileInputStream(CookCoutyOut1)))) {
    String line;
    while ((line = br.readLine()) != null) {
        line1 += line + "\n";

You should find out what kind of line endings the machine you're uploading this to expects. Using System.getProperty("line.separator") is going to give you the default line separator for the machine you're running on, but that's not necessarily the same as what the machine you're sending it to wants. In the last example I just wrote an explicit "\n". You could replace that with "\r\n" if the mainframe wants carriage return/linefeed (CRLF).