Saturday, 16 June 2018

Renaming a file and Repeat a char for the specified number of times from java programing

In java programing you do this code and do some task is completed as like renaming a file from java and Repeat a char for the specified number of times from java program.


Renaming a file from java source code


import java.io.*;

    public class Rename {
         public static void main(String[] argv) throws IOException {
        
         // Construct the file object. Does NOT create a file on disk!
         File f = new File("Rename.java'"); // backup of this source file.
        
         // Rename the backup file to "junk.dat"
         // Renaming requires a File object for the target.
         f.renameTo(new File("junk.dat"));
     }
}


Repeat a char for the specified number of times from java program


public class Test1
    {
     public static void main(String args[])
         {
         String ht = repeat('X',7);
         System.out.println(ht);
     }
    
     public static String repeat(char c,int i)
         {
         String tst = "";
         for(int j = 0; j < i; j++)
             {
             tst = tst+c;
         }
         return tst;
     }
}

0 comments:

Post a Comment