हम एक फ़ाइल कहते हैं नाम बदल सकते हैं test.txt
करने के लिए test1.txt
?
यदि test1.txt
मौजूद है तो क्या इसका नाम बदल जाएगा?
मैं इसे पहले से मौजूद test1.txt फ़ाइल का नाम कैसे बदल दूं ताकि बाद में उपयोग के लिए test.txt की नई सामग्री इसमें जोड़ी जाए?
हम एक फ़ाइल कहते हैं नाम बदल सकते हैं test.txt
करने के लिए test1.txt
?
यदि test1.txt
मौजूद है तो क्या इसका नाम बदल जाएगा?
मैं इसे पहले से मौजूद test1.txt फ़ाइल का नाम कैसे बदल दूं ताकि बाद में उपयोग के लिए test.txt की नई सामग्री इसमें जोड़ी जाए?
जवाबों:
Http://exampledepot.8waytrips.com/egs/java.io/RenameFile.html से कॉपी किया गया
// File (or directory) with old name
File file = new File("oldname");
// File (or directory) with new name
File file2 = new File("newname");
if (file2.exists())
throw new java.io.IOException("file exists");
// Rename file (or directory)
boolean success = file.renameTo(file2);
if (!success) {
// File was not successfully renamed
}
नई फ़ाइल में संलग्न करने के लिए:
java.io.FileWriter out= new java.io.FileWriter(file2, true /*append=yes*/);
संक्षेप में:
Files.move(source, source.resolveSibling("newname"));
ज्यादा जानकारी:
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
निम्नलिखित को सीधे http://docs.oracle.com/javase/7/docs/api/index.html से कॉपी किया गया है :
मान लीजिए कि हम फ़ाइल को उसी निर्देशिका में रखते हुए, "newname" में बदलना चाहते हैं:
Path source = Paths.get("path/here");
Files.move(source, source.resolveSibling("newname"));
वैकल्पिक रूप से, मान लें कि हम एक फ़ाइल को नई निर्देशिका में ले जाना चाहते हैं, एक ही फ़ाइल नाम रखते हैं, और उस नाम की किसी भी मौजूदा फ़ाइल को निर्देशिका में प्रतिस्थापित करते हैं:
Path source = Paths.get("from/path");
Path newdir = Paths.get("to/path");
Files.move(source, newdir.resolve(source.getFileName()), REPLACE_EXISTING);
आप किसी फ़ाइल ऑब्जेक्ट पर renameTo पद्धति का उपयोग करना चाहते हैं ।
सबसे पहले, गंतव्य का प्रतिनिधित्व करने के लिए एक फ़ाइल ऑब्जेक्ट बनाएं। यह देखने के लिए जांचें कि क्या वह फ़ाइल मौजूद है। यदि यह मौजूद नहीं है, तो फ़ाइल को स्थानांतरित करने के लिए एक नया फ़ाइल ऑब्जेक्ट बनाएं। स्थानांतरित करने के लिए फ़ाइल पर renameTo विधि को कॉल करें, और कॉल सफल रहा था, यह देखने के लिए renameTo से दिए गए मान की जाँच करें।
यदि आप एक फ़ाइल की सामग्री को दूसरे में जोड़ना चाहते हैं, तो बहुत सारे लेखक उपलब्ध हैं। विस्तार के आधार पर, ऐसा लगता है कि यह सादा पाठ है, इसलिए मैं FileWriter को देखूंगा ।
जावा 1.6 और उससे कम के लिए, मेरा मानना है कि इसके लिए सबसे सुरक्षित और सबसे साफ एपीआई अमरूद की Files.move है ।
उदाहरण:
File newFile = new File(oldFile.getParent(), "new-file-name.txt");
Files.move(oldFile.toPath(), newFile.toPath());
पहली पंक्ति यह सुनिश्चित करती है कि नई फ़ाइल का स्थान एक ही निर्देशिका है, यानी पुरानी फ़ाइल की मूल निर्देशिका ।
EDIT: मैंने जावा 7 का उपयोग शुरू करने से पहले यह लिखा था, जिसने बहुत ही समान दृष्टिकोण पेश किया था। इसलिए यदि आप जावा 7+ का उपयोग कर रहे हैं, तो आपको kr37 के उत्तर को देखना और बढ़ाना चाहिए।
फ़ाइल को नए नाम पर ले जाकर नाम बदलना। (FileUtils Apache Commons IO lib से है)
String newFilePath = oldFile.getAbsolutePath().replace(oldFile.getName(), "") + newName;
File newFile = new File(newFilePath);
try {
FileUtils.moveFile(oldFile, newFile);
} catch (IOException e) {
e.printStackTrace();
}
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static java.nio.file.StandardCopyOption.*;
Path yourFile = Paths.get("path_to_your_file\text.txt");
Files.move(yourFile, yourFile.resolveSibling("text1.txt"));
मौजूदा टेक्स्ट को "text1.txt" नाम से बदलने के लिए:
Files.move(yourFile, yourFile.resolveSibling("text1.txt"),REPLACE_EXISTING);
इसे इस्तेमाल करे
File file=new File("Your File");
boolean renameResult = file.renameTo(new File("New Name"));
// todo: check renameResult
नोट: हमें यह सुनिश्चित करने के लिए कि हमेशा नाम बदलें फ़ाइल सफल है, क्योंकि यह प्लेटफ़ॉर्म आश्रित (अलग ऑपरेटिंग सिस्टम, विभिन्न फ़ाइल सिस्टम) है और यह नाम बदलने के लिए IO अपवाद को नहीं फेंकता है, तो हमें हमेशा renameTo रिटर्न मान की जाँच करनी चाहिए।
हां, आप File.renameTo () का उपयोग कर सकते हैं। लेकिन याद रखें कि एक नई फ़ाइल का नाम बदलने के दौरान उसका सही रास्ता होना चाहिए।
import java.util.Arrays;
import java.util.List;
public class FileRenameUtility {
public static void main(String[] a) {
System.out.println("FileRenameUtility");
FileRenameUtility renameUtility = new FileRenameUtility();
renameUtility.fileRename("c:/Temp");
}
private void fileRename(String folder){
File file = new File(folder);
System.out.println("Reading this "+file.toString());
if(file.isDirectory()){
File[] files = file.listFiles();
List<File> filelist = Arrays.asList(files);
filelist.forEach(f->{
if(!f.isDirectory() && f.getName().startsWith("Old")){
System.out.println(f.getAbsolutePath());
String newName = f.getAbsolutePath().replace("Old","New");
boolean isRenamed = f.renameTo(new File(newName));
if(isRenamed)
System.out.println(String.format("Renamed this file %s to %s",f.getName(),newName));
else
System.out.println(String.format("%s file is not renamed to %s",f.getName(),newName));
}
});
}
}
}
यदि यह केवल फ़ाइल का नाम बदल रहा है, तो आप File.renameTo () का उपयोग कर सकते हैं ।
उस स्थिति में जहां आप दूसरी फ़ाइल की सामग्री को पहले से जोड़ना चाहते हैं, FileOutputStream को एपेंड कंस्ट्रक्टर विकल्प या FileWriter के लिए समान चीज़ पर एक नज़र डालें । आपको आउटपुट स्ट्रीम / राइटर का उपयोग करके उन्हें लिखने और लिखने के लिए फ़ाइल की सामग्री को पढ़ना होगा।
Files.move(file.toPath(), fileNew.toPath());
काम करता है, लेकिन केवल जब आप बंद करते हैं (या आटोक्लोज़) सभी इस्तेमाल किए गए संसाधन ( InputStream
, FileOutputStream
आदि) मुझे लगता है कि एक ही स्थिति के साथ file.renameTo
या FileUtils.moveFile
।
यहाँ एक फ़ोल्डर में कई फ़ाइलों को सफलतापूर्वक नाम बदलने के लिए मेरा कोड है:
public static void renameAllFilesInFolder(String folderPath, String newName, String extension) {
if(newName == null || newName.equals("")) {
System.out.println("New name cannot be null or empty");
return;
}
if(extension == null || extension.equals("")) {
System.out.println("Extension cannot be null or empty");
return;
}
File dir = new File(folderPath);
int i = 1;
if (dir.isDirectory()) { // make sure it's a directory
for (final File f : dir.listFiles()) {
try {
File newfile = new File(folderPath + "\\" + newName + "_" + i + "." + extension);
if(f.renameTo(newfile)){
System.out.println("Rename succesful: " + newName + "_" + i + "." + extension);
} else {
System.out.println("Rename failed");
}
i++;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
और इसे एक उदाहरण के लिए चलाएं:
renameAllFilesInFolder("E:\\Downloads\\Foldername", "my_avatar", "gif");
रनिंग कोड यहाँ है।
private static void renameFile(File fileName) {
FileOutputStream fileOutputStream =null;
BufferedReader br = null;
FileReader fr = null;
String newFileName = "yourNewFileName"
try {
fileOutputStream = new FileOutputStream(newFileName);
fr = new FileReader(fileName);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
fileOutputStream.write(("\n"+sCurrentLine).getBytes());
}
fileOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fileOutputStream.close();
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}