Skip to content

Instantly share code, notes, and snippets.

@superzjn
Last active June 10, 2021 15:56
Show Gist options
  • Save superzjn/27d1341fe3edc0ade8a513f1c689a99d to your computer and use it in GitHub Desktop.
Save superzjn/27d1341fe3edc0ade8a513f1c689a99d to your computer and use it in GitHub Desktop.
[Read file and parse json] #FileIO
package com.superzjn;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
public class App {
public String readStringFromFile(String path) {
StringBuilder sb = new StringBuilder();
File file = new File(path);
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public String readStreamFromFileAsStream(InputStream inputStream) {
StringBuilder sb = new StringBuilder();
try (BufferedReader br = new BufferedReader(new InputStreamReader(inputStream))) {
String line;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public String readStringFromFileUsingLines(String path) {
String str = null;
File file = new File(path);
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
str = br.lines().collect(Collectors.joining(System.lineSeparator()));
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public void printDuplicateJsonItems(String jsonStr) throws Exception {
ObjectMapper mapper = new ObjectMapper();
// Way 1 HashSet and Iterator
JsonNode nodes = mapper.readTree(jsonStr);
HashSet<Integer> set = new HashSet<>();
Iterator<JsonNode> node = nodes.elements();
while (node.hasNext()) {
int currValue = Integer.parseInt(node.next().get("id").asText());
if (!set.contains(currValue)) {
set.add(currValue);
} else {
System.out.println("Dup" + currValue);
}
}
// This way is not going to work unless we move class Node outside of this file
// class Node {
// String id;
// Integer value;
// }
// List<Node> nodes = mapper.readValue(jsonStr, new TypeReference<List<Node>>()
// {
// });
// HashSet<Integer> set = new HashSet<>();
// for (Node node : nodes) {
// if (!set.contains(node.value)) {
// set.add(node.value);
// } else {
// System.out.println("Dup" + node.value);
// }
// }
}
public static void main(String[] args) throws Exception {
App app = new App();
// 1st Way - Absolute path
// String str = app.readStringFromFile(
// "/home/superzjn/Documents/Java_Test/jsontest/src/main/java/com/superzjn/duplicates.json");
// 2st Way - relative path
// String str =
// app.readStringFromFile("src/main/java/com/superzjn/duplicates.json");
// 3rd Way - Using getResource
// URL path = App.class.getResource("duplicates.json");
// String str = app.readStringFromFile(path.getFile());
// 4th Way - Using getResourceAsStream
InputStream inputStream = App.class.getResourceAsStream("duplicates.json");
String str = app.readStreamFromFileAsStream(inputStream);
// Using lines() to read file
// String str = app.readStringFromFileUsingLines(path.getFile());
// System.out.print(str);
app.printDuplicateJsonItems(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment