用于读取.CSV文件和FTP服务器的Java代码,但是用单行写入数据

时间:2018-02-08 09:41:45

标签: java ftp

我有一个用于读取.CSV文件的案例Java代码,该文件包含带有标题的多行数据并写入FTP服务器,但数据写入单行。

任何人都可以指导如何获得与输入文件相同的输出。

代码:

finally{
    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();
                CountyFTP.setHostname(props.getProperty("County_HostName"));
                CountyFTP.setUsername(props.getProperty("County_UserName"));
                CountyFTP.setPassword(props.getProperty("County_Password"));
                CountyFTP.setDirectory(props.getProperty("County_RemoteDirectory"));
                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;
                }
                dins.close();
                bins.close();
                fins.close();
                //  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);
                getOutPort8(CountyFTP).push(out3);
            }
            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);
    }
}

1 个答案:

答案 0 :(得分:0)

readLine()方法只读取换行符。返回的字符串不包括换行符。查看DataInput#readLine的合同。

要重新添加新行,您可以替换此循环:

XOR

使用while (dins.available() != 0) { line1 = line1 + dins.readLine();; } "\n""\r\n"添加到每行读取的末尾,具体取决于大型机所需的行结尾类型。

但是此代码还存在一些其他问题。您应该检查来自System.getProperty("line.separator")的{​​{1}}结果,以检测文件结尾,而不是null。如文档所示,您应该使用readLine(),而不是dins.available()。自JDK 1.1以来,DataInputStream#readLine已被弃用

如果您继续使用BufferedReaderDataInputStream

,循环将如下所示
available()

或者在更新的Java风格中使用BufferedReader,try-with-resources语句,并使用DataInputStream替换换行而不是line.separator:

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

你应该找出你正在上传的机器所期望的那种行结尾。使用"\n"将为您运行的计算机提供默认行分隔符,但这不一定与您要发送的计算机所需的相同。在最后一个例子中,我刚写了一个明确的try (BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(CookCoutyOut1)))) { String line; while ((line = br.readLine()) != null) { line1 += line + "\n"; } } 。如果大型机需要回车/换行(CRLF),您可以用System.getProperty("line.separator")替换它。