Posts

Showing posts from 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);         ...

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; ...

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);         ...