Java Files: Important Interview Coding Questions

One of our consultants recently went for an interview at “a very big company” in San Francisco, and she was asked to write Java implementation of File System function, such as list, create file, create folder etc.

We strongly recommend you to prepare yourself to be able to whiteboard these solutions for an interview.

Here is the solution (If you like this solution, please leave a comment – Thanks in advance):

 

/**
 * 
 */
package com.training.package3.q6;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * Emulate the following File system functions using File class: A. Create a
 * Folder (similar to mkdir command) B. Create a File C. Delete a Folder or File
 * (similar to rm command) D. Rename a File E. Read the content of a File and
 * Display the content (similar to cat command) F. Display the list of files and
 * folder under a Folder(similar to ls command)
 *
 * 
 */
public class FileSystemEmulator {

	/**
	 * A. Create a new folder at the base URL
	 * 
	 * @param baseURL
	 * @param folderName
	 * @return
	 */
	public boolean createFolder(String baseURL, String folderName) {
		File file = new File(baseURL, folderName);
		boolean status = false;
		try {
			status = file.mkdir();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return status;
	}

	/**
	 * B. Create a File
	 * 
	 * @param baseURL
	 * @param folderName
	 * @return
	 */
	public boolean createFile(String baseURL, String fileName) {
		File file = new File(baseURL, fileName);
		boolean status = false;

		try {
			status = file.createNewFile();

		} catch (IOException e) {

			e.printStackTrace();
		}

		return status;
	}

	/**
	 * C. Delete a Folder or File (similar to rm command)
	 * 
	 * @param baseURL
	 * @param folderName
	 * @return
	 */
	public boolean deleteFile(String baseURL, String fileName) {
		File file = new File(baseURL, fileName);
		boolean status = false;

		try {
			status = file.delete();

		} catch (Exception e) {

			e.printStackTrace();
		}

		return status;
	}

	/**
	 * D. Rename a File
	 * 
	 * @param baseURL
	 * @param folderName
	 * @return
	 */
	public boolean renameFile(String baseURL, String fileName, String newName) {
		File file = new File(baseURL, fileName);
		boolean status = false;

		try {
			status = file.renameTo(new File(baseURL, newName));

		} catch (Exception e) {

			e.printStackTrace();
		}

		return status;
	}

	/**
	 * E. Read the content of a File and Display the content (similar to cat
	 * command)
	 * 
	 * @param baseURL
	 * @param folderName
	 * @return
	 */
	public boolean readAndDisplayContent(String baseURL, String fileName) {
		File file = new File(baseURL, fileName);
		boolean status = false;

		try {
			FileReader myFR = new FileReader(file);
			BufferedReader in = new BufferedReader(myFR);
			String str;

			while ((str = in.readLine()) != null) {

				System.out.println(str);
			}

			in.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return status;
	}

	/**
	 * F. Display the list of files and folder under a Folder(similar to ls
	 * command)
	 * 
	 * @param baseURL
	 * @param folderName
	 * @return
	 */
	public boolean listFilesFolder(String baseURL, String fileName) {
		File file = new File(baseURL, fileName);
		boolean status = false;

		try {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				System.out.println(files[i].getName());
			}

		} catch (Exception e) {

			e.printStackTrace();
		}

		return status;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		String baseURL = "/Users/amitesh.sinha/data";
		new FileSystemEmulator().createFolder(baseURL, "myFolder");
		new FileSystemEmulator().createFolder(baseURL, "myFile.txt");

		new FileSystemEmulator().readAndDisplayContent(baseURL, "myFile.txt");
		new FileSystemEmulator().listFilesFolder(baseURL, "myFile.txt");

		new FileSystemEmulator().deleteFile(baseURL, "myFile.txt");
		new FileSystemEmulator().renameFile(baseURL, "myFile.txt", "newName.txt");

	}
}

Advertisements

About sansatechnology

Sansa Technology is an IT staffing, Consulting and Training company located in the heart of Silicon Valley. If you are a consultant looking for your new gig, send us your resume to us. If you are looking for training in the field of White Box Testing, Java, Java EE, Selenium, DevOps, Android and others, connect with us as well. Visit us at www.sansatechnology.com or email contact@sansatechnology.com
This entry was posted in Coding Challenges, Computers and Internet, Interview Preparation, IT Jobs and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s