Interview Question for Software Engineer in Tests


Team: QA
Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

For large files memory mapping of the file using FileChannel can be used. This is more efficient than opening and reading files. Following code can be used to map a file directly to memory buffer. This is an excerpt from FileChannel class document
"A region of a file may be mapped directly into memory; for large files this is often much more efficient than invoking the usual read or write methods."

private String readFile(String path) throws IOException {
		FileInputStream stream = new FileInputStream(new File(path));
		try {
			FileChannel fc = stream.getChannel();
			MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
			return Charset.defaultCharset().decode(bb).toString();
		} finally {
			stream.close();
		}
	}

- Prasanth June 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if your file is text file then use BufferedReder else use InputStream.
Break the large file into small files based on the number of lines. Then read one by one.
Please comment if i am wrong.

- Sharma June 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is a good approach if you want to read the file sequentially. Actually the perfect answer (imho) is "it depends". When we want random access we use Prasanth's way, if we want sequential (or know nothing about the file) then yours.

- Anonymous June 12, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What will be the good data structure to read file efficiently in java ?

- zammer June 08, 2015 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More