1

I'm working on a Java code to copy the data from one csv file into another csv file .

The requirement is that files (multiple files) that are uploaded in a particular path has to be copied one at a time into another csv file(say tag.csv) in a different location.Later tag.csv will be picked up by a shell script and connect to Oracle DB to run a stored procedure .All of this is done repeatedly until all the uploaded files is processed and shell script is triggered for each file separately.

Now I'm in stuck in copying the csv data . I have tried using buffered reader,filewriter etc. but i'm unable to copy the data to tag.csv,but I could just read them .

Since Im new to java im finding it hard to understand where im going wrong. Help is much appreciated.

1

1 Answer 1

1

You can simply use the Java 7 NIO2:

Eg:
If you want to copy a file from one location to another, simply call:

Files.copy(fromPath, toPath);

If you want to move:

Files.move(fromPath, toPath);

With Java 7 features, you don't need to write hard code for files handling.
Hope it help.

Java 7 NIO2 Tutorial Link

Edited:
But your requirement is not file copy but you want to write uploaded file contents to existing file, you can also simply use the Java 7 NIO2 features.

Eg:

private static void writeFileAsAppend() throws IOException {
        List<String> lines = readFileAsSequencesOfLines();
        Path path = getWriteFilePath();         
        Files.write(path, lines, StandardOpenOption.APPEND);
}

private static List<String> readFileAsSequencesOfLines() throws IOException {
        Path path = getReadFilePath();          
        List<String> lines = Files.readAllLines(path);    
        return lines;
}

private static Path getReadFilePath() {
        Path path = Paths
                .get(".\\ReadMe.csv");    
        return path.normalize();
}

private static Path getWriteFilePath() {
        Path path = Paths
                .get(".\\WriteMe.csv");    
        return path;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sounds great. Please consider for correct answer if you satisfied with that.

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.