Skip to content

Instantly share code, notes, and snippets.

@rahulroshan96
Created February 25, 2017 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rahulroshan96/a05ed860720f6024485b1714e9e8e0ce to your computer and use it in GitHub Desktop.
Save rahulroshan96/a05ed860720f6024485b1714e9e8e0ce to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
class Write{
Write(String input, String output, String T) throws FileNotFoundException, IOException{
if(T.equals("M")){
minute(input, output);
}
if(T.equals("H")){
hour(input, output);
}
}
void minute(String input, String output) throws FileNotFoundException, IOException{
try (BufferedReader br = new BufferedReader(new FileReader(input))) {
String line;
File fout = new File(output);
HashMap<String,Integer> hm = new HashMap<String,Integer>();
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
while ((line = br.readLine()) != null) {
String s = line.substring(0,12);
if(hm.containsKey(s)){
int n = hm.get(s);
n+=1;
hm.put(s, n);
}else{
hm.put(s, 1);
}
}
Map<String, Integer> map = new TreeMap<String, Integer>(hm);
for (String key : map.keySet()){
bw.write(key+","+map.get(key).toString());
bw.newLine();
}
bw.close();
}
}
void hour(String input, String output) throws FileNotFoundException, IOException{
try (BufferedReader br = new BufferedReader(new FileReader(input))) {
String line;
File fout = new File(output);
HashMap<String,Integer> hm = new HashMap<String,Integer>();
FileOutputStream fos = new FileOutputStream(fout);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
while ((line = br.readLine()) != null) {
String s = line.substring(0,9);
if(hm.containsKey(s)){
int n = hm.get(s);
n+=1;
hm.put(s, n);
}else{
hm.put(s, 1);
}
}
Map<String, Integer> map = new TreeMap<String, Integer>(hm);
for (String key : map.keySet()){
bw.write(key+","+map.get(key).toString());
bw.newLine();
}
bw.close();
}
}
}
public class Parse{
public static void main(String []args) throws IOException{
Write w = new Write("file.txt","out.csv","M");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment