简体   繁体   中英

how to import contents from a method to a new file in java

I have created an api method in which through iterations im getting values of some specific columns.Now i want to import/send this information through an attachment in the mail. Below is my method

        Map<String, CostCodeOccupancyDetails> details= new HashMap<>();

        for(CostCodeCount each : occupiedDeptMap) {
            if(details.containsKey(each.getCostCode())) {
                CostCodeOccupancyDetails costCodeOccupancyDetails = details.get(each.getCostCode());
                costCodeOccupancyDetails.setOccupied(each.getCount());
                costCodeOccupancyDetails.setCostCodeName(each.getCostCodeName());
            }else {
                details.put(each.getCostCode(), new CostCodeOccupancyDetails(each.getCostCodeName(),
                        each.getCount(),0,0));
            }
        }
        for(CostCodeCount each : resrvedDeptMap) 
        {
            CostCodeOccupancyDetails costCodeOccupancyDetails = details.get(each.getCostCode());
            costCodeOccupancyDetails.setReserved(each.getCount());
            details.put(each.getCostCode(), costCodeOccupancyDetails);
        }
        StringBuilder builder = new StringBuilder();
        builder.append("CostCode,CostCodeName,OccupiedCount,ReservedCount,VacantCount");
        Set<Entry<String, CostCodeOccupancyDetails>> entrySet = details.entrySet();
        for(Entry<String, CostCodeOccupancyDetails> entry : entrySet) {
            builder.append("\n");
            builder.append(entry.getKey());
            builder.append(",");
            builder.append(entry.getValue().getCostCodeName());
            builder.append(",");
            builder.append(entry.getValue().getOccupied());
            builder.append(",");
            builder.append(entry.getValue().getReserved());
            builder.append(",");
            builder.append(entry.getValue().getVacant());
            builder.append("\n");
        }
        LOG.info(builder.toString());

To write your data to file you can use FileUtils from commons-io library and it will looks like:

        FileUtils.writeStringToFile(file, builder.toString());

If you want to learn how to send mails in java with attachments then you should learn about JavaMailSender, MimeMessage, MimeMessageHelper. Also for creating a file and populating it with your data learn about FileInputStream and FileOutputStream.

Once you are done you will be capable of sending mails in java with any kind of attachment. If then also you face problems after learning about the above classes then you ask.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM