Java---Batch modification file name---IO

Source: Internet
Author: User
Tags stringbuffer

Get some files after the filename is just number, also a txt description

Hey, who let us be programmers, make a small program change is old


Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileReader;


Import java.io.IOException; public class Chengefilename {/** * Title: Using IO Stream to implement file batch modification name * Copyright:copyright (c) * @author: Beacon * Version 1.0 2012-09-24 * @throws ioexception * * public static void Main (string[] args) throws IOException {//
		Gets the stream object of the file containing the file name FileReader fr = new FileReader ("F:\\programview\\java\\jdbc\\a.txt");
		
		BufferedReader bfr = new BufferedReader (FR);
		
		StringBuffer sb = new StringBuffer ();
		Reads a string fname = NULL in a file line by row;
			while ((Fname=bfr.readline ())!= null) {Sb.append (Fname.trim () + ",");
		System.out.println (fname);
		String sbname = sb.tostring ();
		
		Gets the array of file names string[] Nfnames = Sbname.split (",");
		/*for (int i = 0; i < nfnames.length; i++) {System.out.println (nfnames[i));
		
		}*///Get the Folder object file = New file ("F:\\programview\\java\\jdbc\\view") that contains the file to be modified;
		string[] fnames = null; if (!file.isfile ();& file.isdirectory ()) {//Get the filename to be modified fnames = File.list (); 
			/*for (int i = 0; i < fnames.length; i++) {System.out.println (fnames[i].substring (0, Fnames[i].lastindexof (".")); }*/}else{System.out.println ("non-file.")
		"); for (int i = 0; i < fnames.length i++) {int index = Integer.parseint (fnames[i].substring 0, Fnames[i].lastin Dexof ("."))
			-1;
			Get a named file Object file Oldf = new file ("f:\\programview\\java\\jdbc\\view\\" +fnames[i));
			
			Gets the file object for the new name file: Newf = new File ("f:\\programview\\java\\jdbc\\view\\" +nfnames[index]+ ". avi");
		To rename Oldf.renameto (NEWF); } System.out.println ("rename completed.")
	");
 }
}

The code for the annotation section is just for testing;

Tip: If you want to modify the file name in this way, it's best to back up the file first. Otherwise it would be troublesome to make a mistake.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.