473,657 Members | 2,385 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 1520
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
2026
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
2645
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, these combo boxes will have a *large* number of values. There could be any number of values in them from 5 to 5 million (unlikely it would be this large but possible). Obviously 5 million is far too much to populate a <select> control with. Does...
5
1358
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 : while in the original file there is <XXX>, the response file should contain <XXXResponse> with the same content : It's quite easy to modify the InnerText of a node, but it's impossible to rename the node : Is there a smart way to do this ? Thanks for...
6
2228
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 manufacture and distribution. (03/1983) \n\n- SERIES: \n\n1. Mappy \n2. Hopping Mappy \n\n0.26 \n\nBugs: \n- \"000\" is displayed on the highscore. If you get more than 30000 pts, it is displayed normally. This happens whether there are...
5
3901
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 = fileSize; byte b = new byte;
15
8337
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
26556
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 month they are for. I can get the month number by using =DatePart("m",) in a text box, but I can't find a way to convert it to the month name. I've experimented with a couple of things I found in the forum, but I'm not talented enough to get them to...
5
14447
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 using switch this is what i did case 'm': printf( "What month? Enter a number between 1 and 12: " ); scanf( "%d%c", &number, &tmp ); printMonth( number ); break;
12
23192
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 (number) and all month extracts worked correctly (they returned the correct numerical month). However, when I attempted adding a field in the query to format that extracted month to a month name, it only returned January and December - for all 200+...
4
14545
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
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8617
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2743
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 we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.