Home     RSSRSS

Archives: Development

Java Utility – Rename Files in Folder

November 1, 2019 by kiranbadi1991 | Comments Off on Java Utility – Rename Files in Folder | Filed in Development, Others
import java.io.File;
import java.io.IOException;

public class FileRename {

	public static void main(String args[]) throws IOException {		
		String DIR_PATH = "MyDirectoryPath";
		String stringToReplace = "MyStringtoReplaceInFileName";
		getFileNames(DIR_PATH, stringToReplace);
	}

	public static void getFileNames(String Path, String stringToReplace)
			throws IOException {
		File folder = new File(Path);
		File[] listOfFiles = folder.listFiles();
		System.out.println("Total Files :" + listOfFiles.length);
		for (int i = 0; i < listOfFiles.length; i++) {
			if (listOfFiles[i].isFile()) {
				System.out.println("Original File name is : " + listOfFiles[i].getName());
			} else if (listOfFiles[i].isDirectory()) {
				System.out.println("Directory " + listOfFiles[i].getName());
			}
			renameFile(listOfFiles[i], stringToReplace);

		}
	}

	public static void renameFile(File fileItem, String stringToReplace)
			throws IOException {
		String fileName = fileItem.getName();
		if (fileName.contains(stringToReplace)) {
			String newName = fileName.replace(stringToReplace, "");
			File fileWithNewName = new File(fileItem.getParent(), newName);
			if (fileWithNewName.exists()) {
				System.out.println("File cannot be renamed as 2 files exist with same name so deleting 1 file " + fileWithNewName.getName());
				fileWithNewName.delete();
			}
			boolean success = fileItem.renameTo(fileWithNewName);
			System.out.println("New FileName is " + fileWithNewName.getName());
			if (!success) {
				System.out.println("File cannot be renamed " + fileWithNewName.getName());
			}
		}

	}

}

Tags:

Native Script Angular adding Images Resources – Error in downloadBitmap – java.net.MalformedURLException: no protocol:

September 4, 2019 by kiranbadi1991 | Comments Off on Native Script Angular adding Images Resources – Error in downloadBitmap – java.net.MalformedURLException: no protocol: | Filed in Browser, Development

Just in case if you are ever getting this error with Native script Angular Framework while trying to add images from local file system and trying to preview it via playground app.

Error in downloadBitmap – java.net.MalformedURLException: no protocol: ./app/images/home.jpg

Try doing it this way

<Image src=”~/app/images/home.jpg” stretch=”none”></Image>
Trick here in debugging this is to follow the logs on the device and then adjust path for resources accordingly.
LOG from device Kiran Badi: HMR:          ↻ ./app/home/home.component.ts
LOG from device Kiran Badi: HMR:          ↻ ./app/home/home.module.ts

 

Special HTTP Verb to request and view Documents online

May 28, 2019 by kiranbadi1991 | Comments Off on Special HTTP Verb to request and view Documents online | Filed in Development, Others, Process, Project Management, Security

First American Financial Corp’s recently exposed good amount of data(885 million files related to real estate data) to the external folks.Probably this leak impacted close 885 million folks assuming each file relates to one property and one person or family.

The leak mainly happened by modifying the URL parameter of the request. I have written quite a bit of code to fetch some files for viewing online, downloading etc. etc..

Most common way of writing these type of functionality is that you write a controller class to get file for viewing(render document on browser for viewing),you have some service layer which populates file template, and then some data access layer which either fetches data based on some id from database or cache store to populate the template.All these steps along document viewing happens most frequently via GET verb. Of Course you can have secured GET requests.

The whole purpose of GET verb is to fetch the resource which it does as described  in specs. However I feel document viewing is special type of use case which I believes requires a special http verb to deal which browsers most of the times can provide inbuilt protection for cases like First American Financial Corp (similar as Browsers prompt for double submits).

This verb will help to solve other issues as well like malware, excessive ads, virus problems, etc. etc.. This verb will make web much cleaner.

PS : Though I know that First American Financial Corp’s leak is due to poor development practices but I believe that generally browser or Web specs should provide first level of defense against these types of lapses.