Search posts, tags, users, and pages
Decode I am not quite sure since flat file does not mean so much to me. and the definition techopedia.com/definition/25956/flat-file is also pretty vague. It could be a CSV for example
could you specify what your flat file means? just parsing strings? or a serialization of some sort?
Transforming this into json after creating the data struct usually is marshal and i think one of the most common ones is jackson. baeldung.com/jackson
for parsing a flat file you could use a thread pool where you stream the chunks to the thread. But I really don't understand the question well enough to say something useful. Maybe the java idiomatics define it better? Mark do you know something?
j i have update my question.
j currently I am using ffpojo to read flat file data and try to convert it to json. github.com/ffpojo/ffpojo
Decode ah I see so you already have your objects / datastructure you just need to transform them to a json? right?
baeldung.com/jackson-object-mapper-tutorial
does this do the trick?
Can you just give me an example? I am not sure link above...
lets asume you parsed your file and you do a translation between the formats using objects
25654 Gilberto Holms gibaholms@hotmail.com
likely will translate to
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Person {
private int id;
private String fullName;
private String email;
public Person(int id, String fullName, String email) {
this.id = id;
this.fullName = fullName;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
public class Test {
public static void main(String[] args) {
final String fileName = "test.txt";
ObjectMapper objectMapper = new ObjectMapper();
try (Stream<String> stream = Files.lines(Paths.get(fileName))) {
final List<String> jsons = stream.map(line -> {
return line.split(" ");
}).map(set -> {
return new Person(
Integer.parseInt(set[0]),
set[1],
set[2]
);
}).map(person -> {
try {
return objectMapper.writeValueAsString(person);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "";
}).filter(jsonString -> {
return !jsonString.isEmpty();
}).collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
}
}
something like that?
this is just a theoretical way to do it. important is
.map(person -> {
try {
return objectMapper.writeValueAsString(person);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return "";
})
this transforms all Person objects to Json strings that's what the object mapper does via reflections.
does this help / make sense?