Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

java  file copy and rename

Posted on 2014-12-24
2
Medium Priority
?
885 Views
Last Modified: 2015-01-07
Hi

I had a file named "testnow.txt" and i want to make this file 95 copies.. in the same folder
I know how to rename the file and how to copy an existing file, how do you do both at a time.
here's what i want....
i want to duplicate the "testnow.txt" and rename it like
testnow_1.txt
testnow_2.txt
testnow_3.txt
.
.
.
.
testnow_95.txt

Thanks,
0
Comment
Question by:shragi
2 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 40516859
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 40516898
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
public class Q_28586536 {
    public static void main(String[] args) {
          Path src = Paths.get("testnow.txt");
          for( int i=1;i<=95; ++i ){
            Path dest =  Paths.get("testnow_" + i + ".txt");
            try {
                Files.copy(src,dest);
            } catch (IOException e) {
                e.printStackTrace();
            }
          }
   }
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A solution for Fortify Path Manipulation.
Make the most of your online learning experience.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question