Bihar Public Service Commission (BPSC) declared the final results, BPSC, bpsc.bih.nic.in, Bihar Public Service Commission (BPSC)

07 June 2021

 


Bihar Public Service Commission (BPSC) has declared their final results for the competitive exams. Use the following :



Sample code to remove special characters from the file name using java

17 December 2017

The scenario is to remove special characters from the file name. By specifying the package name they will find out which is having special characters in the file name. Then they pick it up and rename the file.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.util.Scanner;
import java.util.StringTokenizer;

import org.apache.commons.lang3.StringUtils;

public class RenameFile {
static final String lineSeparator = System.getProperty ( "line.separator" );

    /**
     * List all files from a directory and its subdirectories
     * @param directoryName to be listed
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws IllegalAccessException
     * @throws InstantiationException
     */
    @SuppressWarnings("static-access")
public void listFilesAndFilesSubDirectories(String directoryName) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException{
   
   
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            if (file.isFile()){               
                if(file.getName().contains("_")){
                changeFileName(file);
                }else{
                changeContent(file);
                }
            } else if (file.isDirectory()){
                listFilesAndFilesSubDirectories(file.getAbsolutePath());
            }
        }
    }
    public void changeContent(File oldFile) throws IOException{
        BufferedReader in = new BufferedReader( new FileReader( oldFile ) );
        String line;
        StringBuilder buf = new StringBuilder();
        while( (line = in.readLine()) != null )
        {
            buf.append( line.replaceAll("_", "").toString() ).append( lineSeparator );
        }
        in.close();
        BufferedWriter out = new BufferedWriter( new FileWriter( oldFile ) );
        out.write( buf.toString() );
        out.close();
              
    }
   
   
    public void changeContent(File newFile, File oldFile) throws IOException{
    FileInputStream instream = null;
    FileOutputStream outstream = null;
   
    instream = new FileInputStream(oldFile);
    outstream = new FileOutputStream(newFile);        
    byte[] buffer = new byte[1024];        
    int length;              
    String str;
    String replacedStr = "";
    BufferedReader in1 = new BufferedReader(new FileReader(oldFile));
    while ((str = in1.readLine()) != null) {                 
    replacedStr += str.replaceAll("_", "").toString() + "\n";                    
    }
    FileWriter fw = new FileWriter(newFile);
    fw.write(replacedStr);
    fw.close();               
    instream.close();
    outstream.close();               
    }
    public void changeFileName(File file) throws IOException{
    System.out.println(file.getAbsolutePath());               
    System.out.println(file.getParent());
        System.out.println(file.getParent() + '\\' + file.getName().replace("_", ""));
        File oldFile = new File(file.getAbsolutePath());
        File newFile = new File(file.getParent() + '\\' + file.getName().replace("_", ""));
        newFile.createNewFile();
        changeContent(newFile, oldFile);
    }
  
    public static void main (String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException{
   
    RenameFile listFilesUtil = new RenameFile();      
        //Windows directory example
        String directoryWindows=StringUtils.EMPTY;
   
        Scanner s=new Scanner(System.in);
       
        System.out.println("Enter Directory Path:");
        directoryWindows=s.nextLine();
       
        listFilesUtil.listFilesAndFilesSubDirectories(directoryWindows);
    }
}



code to delete file which is having special characters in it

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.StringTokenizer;

public class DeleteFile {

private static  String FILE_PATH = "";

public static void main(String[] args) throws IOException{
DeleteFile listFilesUtil = new DeleteFile();      
        //Windows directory example
        //final String directoryWindows ="C:\\Users\\varaprasad.bantaram\\Downloads\\BMS\\wr-automation-updated\\wr-automation\\src";
    //final String directoryWindows ="C:\\Users\\varaprasad.bantaram\\Desktop\\wr_Automation_BookingFlow_Dec_12\\wr-automation\\src\\com\\wr\\aem\\BookingFlow";
       
String directoryWindows=StringUtils.EMPTY;
   
        Scanner s=new Scanner(System.in);
       
        System.out.println("Enter Directory Path:");
        directoryWindows=s.nextLine();     
        listFilesUtil.listFilesAndFilesSubDirectories(directoryWindows);
   
}

    public void listFilesAndFilesSubDirectories(String directoryName) throws IOException{
   
        File directory = new File(directoryName);
        //get all the files from a directory
        File[] fList = directory.listFiles();
        for (File file : fList){
            if (file.isFile()){               
                if(file.getName().contains("_")){
               
                System.out.println(file.getAbsolutePath());               
                System.out.println(file.getParent());
                    System.out.println(file.getParent() + '\\' + file.getName().replace("_", ""));
                    FILE_PATH = file.getAbsolutePath().toString();
                    if(deleteFile(FILE_PATH) ){
                    System.out.println(file.getName());
            System.out.println("File is deleted!");
            } else {
            System.err.println("Failed to delete file.");
            }
               
                }
            } else if (file.isDirectory()){
                listFilesAndFilesSubDirectories(file.getAbsolutePath());
            }
        }
    }

public static boolean deleteFile(String filePath){

File file = new File(FILE_PATH);

return file.delete();
}

   
}

 
 
HTML Hit Counter