deutsche bank Interview Question
Dev LeadsCountry: India
Interview Type: Phone Interview
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ScannerExample
{
public static void main(String[] args) throws FileNotFoundException
{
//Get scanner instance
Scanner scanner = new Scanner(new File("SampleCSVFile.csv"));
//Set the delimiter used in file
scanner.useDelimiter(",");
//Get all tokens and store them in some data structure
//I am just printing them
while (scanner.hasNext())
{
System.out.print(scanner.next() + "|");
}
//Do not forget to close the scanner
scanner.close();
}
}
/**
*Most of the code copied from howtodoinjava dot com site
*
*/
import java.io.File;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
public class MemoryMappedFileReadExample
{
private static String largeCSV = "data3GB.csv";
public static void main(String[] args) throws Exception
{
//Create file object
File file = new File(largeCSV);
//Get file channel in readonly mode
FileChannel fileChannel = new RandomAccessFile(file, "r").getChannel();
//Get direct byte buffer access using channel.map() operation
MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
// the buffer now reads the file as if it were loaded in memory.
System.out.println(buffer.isLoaded()); //prints false
System.out.println(buffer.capacity()); //Get the size based on content size of file
//You can read the file from this buffer the way you like.
for (int i = 0; i < buffer.limit(); i++)
{
processYourDataIntoDatabase((char) buffer.get());//here do whatever you like in DB
}
}
}
You can use memory-mapped files to read big files which cannot fit into RAM.
- Vit July 26, 2016