Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I generate a report where I read data from postgresql and then populate a Java object
Below is kind of sample skeleton where I want to show there are many collection object within Main object i.e ReportMessageStructure
public class ReportMessageStructure {
protected MessageHeader messageHeader;
protected MessageDuration messageDuration;
protected ObjectA sampleListA;
protected ObjectB sampleListB;
protected ObjectC smapleListC;
}
Now My requirement is to write data into TSV file . Could you please suggest me best way to do this. I know I can use JAXB if I had to convert into XML. However need way to convert into TSV. Any tips/suggestion would be great help.
Max size the data will produce would be around 700MB from object to TSV
You can use CSVPrinter from Apache Commons CSV library with CSVFormat equal to TDF.
try (CSVPrinter printer = new CSVPrinter(new FileWriter("csv.txt"), CSVFormat.TDF)) {
printer.printRecord("id", "userName", "firstName", "lastName", "birthday");
printer.printRecord(1, "john73", "John", "Doe", LocalDate.of(1973, 9, 15));
printer.println();
printer.printRecord(2, "mary", "Mary", "Meyer", LocalDate.of(1985, 3, 29));
} catch (IOException ex) {
ex.printStackTrace();
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I have a model object which contains nested arrays and i want to retrieve some details within that model .How to collect property msg from subscriberCriteriaList into an array where subscriberCriteriaList.status is FAIL. I would expect java 8 solution for the same ? Below is the sample model objects and the corresponding json structure .
public class Data{
private List<subscriberList> subscriberCriteriaList;
}
public class subscriberList{
private String mdn;
private List<SubscriberCriteriaList> subscriberCriteriaList;
}
public class SubscriberCriteriaList{
private String status;
private String msg;
}
Sample json structure
{
"subscriberList": [
{
"mdn": "string",
"subscriberCriteriaList": [
{
"status": "FAIL",
"msg": "error message"
}
]
}
]
}
Assuming that the top-level object has type Data, and appropriate getters are available in all the mentioned classes, it is possible to apply flatMap to the nested lists and filter by status value:
String[] failureMessages = data.getSubscriberCriteriaList()
.stream() // Stream<subscriberList>
.flatMap(sl -> sl.getSubscriberCriteriaList().stream()) // Stream<SubscriberCriteriaList>
.filter(scl -> "FAIL".equals(scl.getStatus()))
.map(SubscriberCriteriaList::getMsg) // map to messages
.distinct() // (optionally) remove duplicates if necessary
.toArray(String[]::new);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm new learner in programming, and i have a logic problem.
I would like to initialize a object class to settle it.
I have 2 Entities object Class:
Entity1:
private Date date;
private List<Entity2> entity2;
.... Getters and Setters ....
Entity2:
private String description;
.... Getters and Setters ....
Now I've init the entities
Entity1 entity1 = new Entity1();
List<Entity2> Entity2 = new ArrayList<Entity2>();
entity1.setEntity2(entity2);
I have no error message by doing this, in debugging mode I see in the entity1 and the ArrayList of entity2, but empty. I see no object inside, just this [ ] and I would like to see "description" object (which will be normal), like this [description = null];
Someone can explain to me what I do wrong ?
Thanks so much for your help
You're really close. Let's consider what you've got thus far.
Entity1 entity1 = new Entity1();
You've created yourself an instance of Entity1. So far, this looks like this:
{
Date: null,
entity2: null
}
Then you've issued the following commands:
List<Entity2> Entity2 = new ArrayList<Entity2>();
entity1.setEntity2(entity2);
So now your entity1 variable looks like this:
{
Date: null,
entity2: [],
}
The next thing you need to do is add a new object into your new list. First, you need to make yourself an instance of Entity2.
Entity2 myEntity = new Entity2();
Then you need to put it into your list, using the add command on the List interface. I won't write this out for you, you need to work it out yourself. Have fun!
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
im creating minecraft sectors system..
I want to get worldguard region what player is actually.
All regions are saved into files like (Regions/region1.yml, region2.yml)
Now my question is:
How can i list all files in folder regions to String list?
i need it to do something like this
if(e.getregion.getiid.contains(list1) {
//do something
}
you can use list function of File class http://docs.oracle.com/javase/7/docs/api/java/io/File.html#list()
File regionFolder = new File("path/to/Regions/folder");
String[] regionFile = regionFolder.list();
If you want to leverage the capabilities of ArrayList to check if a String is in the list, you can do this:
File regionFolder = new File("/path/to/files");
FileFilter filter = new FileFilter() {
#Override
public boolean accept(File pathname) {
return pathname.getPath().endsWith(".yml");
}
};
ArrayList<String> myYmlFiles = new ArrayList<String>(Arrays.asList(regionFolder.list(filter)));
You can then use .contains on the myYmlFiles object, and it will only contain your .yml files.
if ( myYmlFiles.contains(e.getRegion().getId()) ) {
// do something
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
So, in a test that i took few days ago we were supposed to make a method that returns a list, that contained all info about a class we had made before that.
The code looked like this:
public List<File> getall{
}
whereas File is the class that contains some variables and a toString() method.
So, what is a good way to write a method that returns this data type?
I may be missing the point here, but it's really quite simple:
public List<File> getall() {
// create a List object to return
List<File> returnValues = new ArrayList<>();
// add File objects to returnValues
....
// and return it
return returnValues;
}
The nature of the class contained in the List is of no importance - however in this particular case you should be aware that there's already a File class in the standard java.io package, so File may not be the best name to give to a custom class...
As you need to return a List<File> ,it depends on how you have implemented the File class.
public List<File> getAll() {
// create a List object to return
List<File> listobj= new ArrayList<File>();
//Do your stuff here you want to do and return that object
return listobj;
}
Hope it will help you.
Try :
public List<File> getAll() {
// create a List object
List<File> files = new ArrayList<>();
files.add(new file); // Keep doing this
return files;
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
If I wanted to build a constructor for a class to import a file, which I have passed in a String nameOfFile, how can I initialize the object's state, then open the document file, and process each line of the document? References and clear explanation would be appreciated. I'm just beginning to learn java.
To clear up my question, how can you build an object for a document file you have imported? What I'm doing right now is that I have wrote a class but i'm struggling with building an object for a SPECIFIC file. What I have thus far is
public class theImport
{
theImport(String nameOfFile)
{
(Here is where I want to achieve all the listing I have above.)
}
.
.
.
}
I believe you would do this in a two-step process.
Step one: The actual constructor.
private String nOF;
public ClassName(String nameOfFile) {
nOF = nameOfFile;
}
Step two: Evaluation of the file. Since this can fail for a variety of reasons (for example the file not existing, it shouldn't go to the constructor (you can't catch these errors from a return type you aren't having in the constructor)
public boolean Evaluate() {
//Evaluate your file and return false if it fails for whatever reason.
}
My Java isn't the best at the moment, but this should tip you in the right direction.
Propably you mean something like this:
public class theImport
{
theImport(String nameOfFile)
{
try {
FileReader input = new FileReader(nameOfFile);
BufferedReader bufRead = new BufferedReader(input);
String line;
line = bufRead.readLine();
//this will loop thought the lines of the file
while (line != null){
line = bufRead.readLine();
//do whatever you want with the line
}
bufRead.close();
}
catch (Exception e){
e.printStackTrace();
}
}
}