Tuesday, 20 December 2016

Converting JSONArray into CSV file in java

package com.loginController;

import java.io.File;
import org.apache.commons.io.FileUtils;
import org.json.CDL;
import org.json.JSONArray;
import org.json.JSONObject;
/**
 *
 * @author anand
 *
 */
public class ConvertArray {
    public static void main(String myHelpers[]){
        String jsonArrayString = "{\"fileName\": [{\"name\": \"Anand\",\"last\": \"Dwivedi\",\"place\": \"Bangalore\"}]}";

        JSONObject output;
        try {
            output = new JSONObject(jsonArrayString);


            JSONArray docs = output.getJSONArray("fileName");

            File file=new File("JSONSEPERATOR.csv");
            String csv = CDL.toString(docs);
            FileUtils.writeStringToFile(file, csv);
            System.out.println("Data has been Sucessfully Writeen to "+file);
        } catch (Exception e) {
            e.printStackTrace();
        }    
    }

}

Sunday, 18 December 2016

Custom Sorting in Java

How Custom Sorting will work in java

# Some time we may got the requirement to Sort generic Class level list into Default Sorting order in that case we will have to write own custom logic for that . below is the code to perform Custom logic in java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com;

import java.util.*;

/**
 *
 * @author anand
 */
public class CustomSorting {

    private String username;
    private String userplace;
    private String userdesignation;

    static List<CustomSorting> details = null;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUserplace() {
        return userplace;
    }

    public void setUserplace(String userplace) {
        this.userplace = userplace;
    }

    public String getUserdesignation() {
        return userdesignation;
    }

    public void setUserdesignation(String userdesignation) {
        this.userdesignation = userdesignation;
    }

    public CustomSorting(String username, String userplace, String userdesignation) {
        this.username = username;
        this.userplace = userplace;
        this.userdesignation = userdesignation;
    }

    public static void main(String[] args) {
        details = new ArrayList<CustomSorting>();

        CustomSorting customDetails = new CustomSorting("Anand", "Bangalore", "Software Developer");
        CustomSorting customDetails1 = new CustomSorting("Ajeet", "Pune", "Testing Lead");
        CustomSorting customDetails3 = new CustomSorting("Antrish", "Chennai", "C# Lead");
        CustomSorting customDetails5 = new CustomSorting("Sachin", "Rewa", "SBI Manager");
        CustomSorting customDetails6 = new CustomSorting("Anand", "Mumbai", "Software Developer");

        details.add(customDetails);
        details.add(customDetails1);
        details.add(customDetails3);
        details.add(customDetails5);
        details.add(customDetails6);

        System.out.println("==========Before Sorting==============");
        for (CustomSorting custom : details) {
            System.out.println(custom.getUsername());
        }

        Collections.sort(details, new Comparator<CustomSorting>() {

            @Override
            public int compare(CustomSorting t, CustomSorting t1) {
                if (t.getUsername().equals(t1.getUsername())) {
                    return t.getUserplace().compareTo(t1.getUserplace());
                } else {

                    return t.getUsername().compareTo(t1.getUsername());
                }

            }

        });

        System.out.println("===========After Sorting =============");
        for (CustomSorting custom : details) {
            System.out.println(custom.getUsername()+"\t"+custom.getUserplace());
        }
    }

}

Saturday, 17 December 2016

How to read CSV File in Java

Here we can learn how to read CSV file comma seprated in java below is the code

--> Use apache API to read CSV or use BufferReader
--> here i have used Buffer Reader


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
/**
 *
 * @author anand
 *
 */
public class ReadCSVFile {

    public static void main(String[] args) {

     
        BufferedReader br = null;
        String line = "";
        String cvsSplitBy = ","; //Comma seprated

        try {

            br = new BufferedReader(new FileReader("Your_CSV_FILE_NAME"));
            while ((line = br.readLine()) != null) {
                System.out.println("Content \t"+line);

            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }}

Map class field to map

 import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class AutogeneratedClassMapper {     public static M...