0

My explanation might not be clear, sorry about that!

I am using NetBeans IDE 8.0 to create Java projects. I'm a beginner to Java programming and only started last Wednesday for work experience. Also for my CSV I'm using the following library: CSVReader

My mentor told me to create a program which outputs data as CSV so I can go to the CSV file and make a graph of the data. What it does is obtain the data size(Long) and create time(XML Gregorian Calendar) of the files in an online database using JPQL. If you want to have a read here is the schema: [SCHEMA](http://icatproject.org/mvn/site/icat/server/4.3.2/icat.core/schema.html#Datafile"ICAT schema").

Seems like the program is able to find the datafiles(I checked using debugger: 8 files on a 2 day range), but it does not seem to output the data as CSV, even though it creates the CSV files. In my code there will be a part for data count, I haven't done that yet. Also the user can input the date range in which they want the data from, although for testing purpose, I have made that part a comment so that I have a small range. Could you please check my code(I have edited out any unnecessary info. like the login part):

    String outputFileCount = "C:\\Users\\me\\Documents\\CSV\\dataByCount.csv";
    String outputFileSize = "C:\\Users\\me\\Documents\\CSV\\dataBySize.csv";
    boolean alreadyExistsCount = new File(outputFileCount).exists();
    boolean alreadyExistsSize = new File(outputFileSize).exists();

    String FileSize;
    Long DatafileCount;
    Calendar calendar;
    SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
    String Date;

    List<Object> dfSearch = null;

    CsvWriter csvOutputCount = new CsvWriter(new FileWriter(outputFileCount, true), ',');
    CsvWriter csvOutputSize = new CsvWriter(new FileWriter(outputFileSize, true), ',');

    if (typeOutput.equals("count")) {
            if (!alreadyExistsCount) {
                csvOutputCount.write("DataFile Create Time");
                csvOutputCount.write("Datafile Count");
                csvOutputCount.endRecord();
            }

    } else if (typeOutput.equals("size")){
            if (!alreadyExistsSize) {
                csvOutputSize.write("DataFile Create Time");
                csvOutputSize.write("DataFile Size");
                csvOutputSize.endRecord();
            }

                inputDates = "SELECT d FROM Datafile d WHERE d.datafileCreateTime BETWEEN {ts  2013-12-01 00:00:00}  AND {ts 2013-12-02 23:59:59}";
                dfSearch = icat.search(sessionId, inputDates);
                for (Object datafile : dfSearch) {

                    Datafile dFile = (Datafile) datafile;

                    calendar = dFile.getDatafileCreateTime().toGregorianCalendar();
                    formatter.setTimeZone(calendar.getTimeZone());
                    Date = formatter.format(calendar.getTime());
                    csvOutputSize.write(Date);  //Convert XMLGregorianCalendar to String

                    FileSize = Long.toString(dFile.getFileSize());
                    csvOutputSize.write(FileSize); //Convert long to String

                    csvOutputSize.endRecord();


            }

    }
}

}

3
  • Personally I'm not willing to read a wall of code like this. I think that's part of your problem: poor decomposition. Big problems are easier to solve when you break them into smaller pieces that are easier to test and debug. Commented Aug 12, 2014 at 10:12
  • Please put together an sscce Commented Aug 12, 2014 at 10:13
  • are you closing the writer in the end? like csvOutputCount.close(); sometimes, writers need to be closed to flush their internal buffers Commented Aug 12, 2014 at 10:14

2 Answers 2

2

Call flush() (and close()) on the CsvWriters after you are done.

Sign up to request clarification or add additional context in comments.

2 Comments

What is flush? close() seems to have done the job. Could someone explain to me why you need to close it before ending program?
In the documentation of the class:javacsv.sourceforge.net/com/csvreader/… (close() apparently flushes internally)
0

use try-catch-finally block:

CsvWriter csvOutputCount = null; 
CsvWriter csvOutputSize = null;
try
{
    csvOutputCount = new CsvWriter(new FileWriter(outputFileCount, true), ',');
    csvOutputSize = new CsvWriter(new FileWriter(outputFileSize, true), ',');
    ...
}
catch(Exception e)
{
    e.printStackTrace();
}
finally
{
    if(csvOutputCount != null)
    {
       csvOutputCount.close();
    }
    if(csvOutputSize != null)
    {
       csvOutputSize.close();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.