matrix.CreateUserList.java Source code

Java tutorial

Introduction

Here is the source code for matrix.CreateUserList.java

Source

/*
 * 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 matrix;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 *
 * @author nSabri
 */
public class CreateUserList {

    private String tweetsJsonInput;
    private String userListPathOutput;

    public void tweetsToUserList()
            throws FileNotFoundException, UnsupportedEncodingException, IOException, ParseException {

        File fout = new File(userListPathOutput);
        FileOutputStream fos = new FileOutputStream(fout);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));

        BufferedReader inputTW = new BufferedReader(
                new InputStreamReader(new FileInputStream(tweetsJsonInput), "ISO-8859-9"));
        ArrayList userList = new ArrayList();
        JSONParser jsonParser = new JSONParser();
        JSONArray jsonArray = (JSONArray) jsonParser.parse(inputTW);
        int sayac = 0;
        for (Object obj : jsonArray) {

            JSONObject tweet = (JSONObject) obj;
            JSONObject user = (JSONObject) tweet.get("user");
            //            String userID = user.get("id").toString();
            //            String userName = user.get("name").toString();
            String userID = user.get("id").toString();
            String userName = user.get("name").toString();

            if (userList.contains(userID) == false) {
                userList.add(userID);
                bw.write(userID + "," + userName);
                bw.newLine();
                sayac++;
            }

        }
        System.out.println(sayac);
    }

    public String getTweetsJsonInput() {
        return tweetsJsonInput;
    }

    public void setTweetsJsonInput(String tweetsJsonInput) {
        this.tweetsJsonInput = tweetsJsonInput;
    }

    public String getUserListPathOutput() {
        return userListPathOutput;
    }

    public void setUserListPathOutput(String userListPathOutput) {
        this.userListPathOutput = userListPathOutput;
    }

}