Showing posts with label javaprojects. Show all posts
Showing posts with label javaprojects. Show all posts

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();
}

   
}

Sample Code to convert text to camel case using java

02 December 2017

public static String toTitleCase(String input) {
     StringBuilder titleCase = new       StringBuilder();
    boolean nextTitleCase = true;
    for (char c : input.toCharArray()) {
           if (Character.isSpaceChar(c)) { 
                  nextTitleCase = true;
           }
           else if (nextTitleCase) {
                    c = Character.toTitleCase(c);   
                    nextTitleCase = false;
           }
           titleCase.append(c);
     }
return titleCase.toString();
}

Source code for Blowfish Algorithm,Code for Blowfish Algorithm,Download Blowfish Algorithm,Free Download Blowfish Algorithm

04 January 2010

Source code for Blowfish Algorithm,Code for Blowfish Algorithm,Download Blowfish Algorithm,Free Download Blowfish Algorithm

Blowfish is a keyed, symmetric block cipher, designed in 1993 by Bruce Schneier and included in a large number of cipher suites and encryption products. Blowfish provides a good encryption rate in software and no effective cryptanalysis of it has been found to date. However, the Advanced Encryption Standard now receives more attention. Schneier designed Blowfish as a general-purpose algorithm, intended as a replacement for the aging DES and free of the problems and constraints associated with other algorithms. At the time Blowfish was released, many other designs were proprietary, encumbered by patents or were commercial/government secrets. Schneier has stated that, "Blowfish is unpatented, and will remain so in all countries. The algorithm is hereby placed in the public domain, and can be freely used by anyone." Notable features of the design include key-dependent S-boxes and a highly complex key schedule.

Blowfish Algorithm :

Blowfish has a 64-bit block size and a variable key length from 32 up to 448 bits. It is a 16-round Feistel cipher and uses large key-dependent S-boxes. It is similar in structure to CAST-128, which uses fixed S-boxes. The diagram to the left shows the action of Blowfish. Each line represents 32 bits. The algorithm keeps two subkey arrays: the 18-entry P-array and four 256-entry S-boxes. The S-boxes accept 8-bit input and produce 32-bit output. One entry of the P-array is used every round, and after the final round, each half of the data block is XORed with one of the two remaining unused P-entries. The diagram to the upper left shows Blowfish's F-function. The function splits the 32-bit input into four eight-bit quarters, and uses the quarters as input to the S-boxes. The outputs are added modulo 232 and XORed to produce the final 32-bit output. Decryption is exactly the same as encryption, except that P1, P2,..., P18 are used in the reverse order. This is not so obvious because xor is commutative and associative. A common mistake is to use inverse order of encryption as decryption algorithm (i.e. first XORing P17 and P18 to the ciphertext block, then using the P-entries in reverse order).
Blowfish's key schedule starts by initializing the P-array and S-boxes with values derived from the hexadecimal digits of pi, which contain no obvious pattern (see nothing up my sleeve number). The secret key is then XORed with the P-entries in order (cycling the key if necessary). A 64-bit all-zero block is then encrypted with the algorithm as it stands. The resultant ciphertext replaces P1 and P2. The ciphertext is then encrypted again with the new subkeys, and P3 and P4 are replaced by the new ciphertext. This continues, replacing the entire P-array and all the S-box entries. In all, the Blowfish encryption algorithm will run 521 times to generate all the subkeys - about 4KB of data is processed.

For Free Download :

Source code for PORT SCANNER,Download PORT SCANNER code,Free Download PORT SCANNER,Code for PORT SCANNER,Download PORT SCANNER

Source code for PORT SCANNER,Download PORT SCANNER code,Free Download PORT SCANNER,Code for PORT SCANNER,Download PORT SCANNER

Port Scanner can keeps your pc port gaurd. Keep an eye on the open ports in your local pc. A java written port scanner which looks for the open ports in the given server and logs a report of them.Click here to see a screenshot of this code!
For free download the Source code :

 
 
HTML Hit Counter