473,503 Members | 972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert a large number of file to another name in java

6 New Member
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.util.regex.Matcher;
  11. import java.util.regex.Pattern;
  12.  
  13.  
  14.  
  15.  
  16. /*Step1 :-nake sure that u have ur project loc path other wise it ll show exception
  17.  Step2:run this java code 
  18.  */
  19.  
  20.  
  21. public class fileNameChanger {
  22.  
  23.  
  24.  
  25.  
  26.     public static void main(String[] args) {
  27.         String oldName, newName, num = null;
  28.         File folder = new File("C:\\demo");
  29.  
  30.         String projectLoc="C:\\demo\\"; //write your project location 
  31.  
  32.  
  33.         File[] listOfFiles = folder.listFiles();
  34.         for (int i = 0; i < listOfFiles.length; i++) {
  35.  
  36.             if (listOfFiles[i].isFile()) {
  37.  
  38.                 File oldFile = new File(projectLoc + listOfFiles[i].getName());
  39.  
  40.                 makeNewFile(oldFile);
  41.  
  42.             }
  43.  
  44.         }
  45.     }
  46.  
  47.     public static boolean makeNewFile(File oldFile) {
  48.         String newProjectLoc="C:\\demo\\new\\";
  49.         String oldName = oldFile.getName();
  50.         System.out.println("OldFileName :" + oldName);
  51.         String subFileName = oldName.substring(0, oldName.indexOf('.'));
  52.         int num = subFileName.charAt(subFileName.length() - 1);
  53.         String newFileName = subFileName.replace((char) num, (char) ++num);
  54.         System.out.println("NewFileName :" + newFileName + ".html");
  55.                 File newName = new File(newProjectLoc + newFileName + ".html");
  56.         try {
  57.             copyFile(oldFile, newName);
  58.         } catch (IOException e) {
  59.             // TODO Auto-generated catch block
  60.             e.printStackTrace();
  61.         }
  62.         return true;
  63.  
  64.     }
  65.  
  66.     private static void copyFile(File source, File dest)
  67.  
  68.     throws IOException {
  69.  
  70.         InputStream input = null;
  71.  
  72.         OutputStream output = null;
  73.  
  74.         try {
  75.  
  76.             input = new FileInputStream(source);
  77.  
  78.             output = new FileOutputStream(dest);
  79.  
  80.             byte[] buf = new byte[1024];
  81.  
  82.             int bytesRead;
  83.  
  84.             while ((bytesRead = input.read(buf)) > 0) {
  85.  
  86.                 output.write(buf, 0, bytesRead);
  87.  
  88.             }
  89.             dest.createNewFile();
  90.  
  91.         } finally {
  92.  
  93.             input.close();
  94.  
  95.             output.close();
  96.  
  97.         }
  98.  
  99.     }
  100.  
  101. }
  102.  
  103.  
  104.  
  105.  
Feb 11 '14 #1
2 1513
audrey45
2 New Member
in C# we have GUID but i think in java you have UUID . UUID will generate filename for you depends on your filename you want to change :)
Feb 13 '14 #2
Nepomuk
3,112 Recognized Expert Specialist
What exactly is your question? The code you provided certainly looks like you've thought about it quite a bit.
Feb 14 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2020
by: Mauro Baraldi | last post by:
Hello World ! Someone can help how to convert a number to it name. Well I'll explain... 11 -> leven. 48 -> forty eight. Something like it. Thanks
1
2626
by: Mike | last post by:
My users have to select an value from a fixed selection of values. The obvious choice of control for such a requirement is to use a <select> (i.e. a combo box). My problem is that sometimes,...
5
1352
by: Patrice Dargenton | last post by:
Hello, I have to convert an xml file to another xml file that is very similar, but some content are different, and the names of some nodes are different. In fact it's a response to a message :...
6
2221
by: jcrouse | last post by:
Here is a sniplet from a text file game name mapp description "Mappy (US) year 198 manufacturer "Namco history "\nMappy (c) 03/1983 Namco. \n\n- TRIVIA: \n\nLicensed to Bally Midway for US...
5
3892
by: bbb | last post by:
Hi, I need to convert XML files from Japanese encoding to UTF-8. I was using the following code: using ( FileStream fs = File.OpenRead(fromFile) ) { int fileSize = (int)fs.Length; int buffer...
15
8316
by: Lyosha | last post by:
Converting binary to base 10 is easy: 255 Converting base 10 number to hex or octal is easy: '0144' '0x64' Is there an *easy* way to convert a number to binary?
2
26544
by: CindySue | last post by:
Hello--I've got a query that uses DatePart("m",) to select records for a particular month, and then a report based on that query. I'd like to be able to create a field in the report that says what...
5
14410
by: girl23 | last post by:
I am using a function prototype to convert month number to month name. basically you ask the compiler to enter int from 1 to 12 and then convert it. printMonth ( number ); is the prototype and...
12
23162
by: Be Borth | last post by:
I saw previous solutions to convert a month number (1) to a month name (January). I have a database with 200+ dates. In a query, I use the "Part" function DatePart("m",), to extract the month...
4
14505
by: Dave Smith | last post by:
Hello and thanks you for your help I’m trying to make my date field that looks like 1 2 3 4 5 6
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7339
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5581
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4678
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.