Java Change File-Extension

Java Change File-Extension - Hallo sahabat Download APK, Pada Artikel yang anda baca kali ini dengan judul Java Change File-Extension, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel JAVA, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Java Change File-Extension
link : Java Change File-Extension

Baca juga


Java Change File-Extension




In this program you will learn how to change the file extension of the file. Here you will be asked to enter the file name whose extension is to be changed and then you will get the file name with the changed extension..

JAVA


Description of the code:
In the program code given below, you will be asked to enter the file name with the extension as can be observed from the method of java.io.*; package which is BufferedReader();. The compiler will read the file name as oldfileExtension once you enter it through readLine(); method. Then it will check for that filename if it exists or not. If it will find the mentioned filename then change it by asking you to enter a new fileextension. For this we have used a constructor and we have passed the newfileExtension to that. Then we have applied renameTo(); to change the filename. Remember we haven't changed the filename here but file extension only.
The code of the program is given below:
import java.io.*;

public class ChangeFileExt{
  public static void main(String[] args) throws IOException{
  BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  System.out.print("Please enter the filename with extension to be changed: ");
  String oldfileExtension = in.readLine();
  File oldfile = new File(oldfileExtension);
  if(!oldfile.exists())
  {
  System.out.println("File does not exist.");
  System.exit(0);
  }
  int dotPos = oldfileExtension.lastIndexOf(".");
  String strExtension = oldfileExtension.substring(dotPos + 1);
  String strFilename = oldfileExtension.substring(0, dotPos);
  String newfileExtension = in.readLine();
  String strNewFileName = strFilename + "." + newfileExtension;
  File newfile = new File(strNewFileName);
  boolean Rename = oldfile.renameTo(newfile);
  if(!Rename)  {
  System.out.println("FileExtension hasn't been changed successfully.");
  }
  else {
  System.out.println("FileExtension has been changed successfully.");
  }
  }
}
Output of the program:
C:\java-examples>javac ChangeFileExt.java

C:\java-examples>java ChangeFileExt
Please enter the filename with extension to be changed: amit.txt
Enter file extension to change the file type: java
FileExtension has been changed successfully.

C:\sourcecontrol\roseindia\public_html\java\string-examples>




Demikianlah Artikel Java Change File-Extension

Sekianlah artikel Java Change File-Extension kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Java Change File-Extension dengan alamat link https://aplikasimateri.blogspot.com/2017/01/java-change-file-extension.html

0 Response to "Java Change File-Extension"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel