Which two try statements, when inserted at line **, enable the code to successfully move the file info.txt to the destination directory…?

Given the code fragment:
<code>
public static void main(String[] args) {
String source = “d:\\company\\info.txt”;
String dest = “d:\\company\\emp\\info.txt”;
//insert code fragment here Line **
} catch (IOException e) {
System.err.println (“Caught IOException: ” + e.getmessage();
}
}
</code>

Which two try statements, when inserted at line **, enable the code to successfully move the file info.txt to the destination directory, even if a file by the same name already exists in the destination directory?

Given the code fragment:
<code>
public static void main(String[] args) {
String source = “d:\\company\\info.txt”;
String dest = “d:\\company\\emp\\info.txt”;
//insert code fragment here Line **
} catch (IOException e) {
System.err.println (“Caught IOException: ” + e.getmessage();
}
}
</code>

Which two try statements, when inserted at line **, enable the code to successfully move the file info.txt to the destination directory, even if a file by the same name already exists in the destination directory?

A.
try {FileChannel in = new FileInputStream(source).getChannel();
FileChannel out = new FileOutputStream(dest).getChannel ();
in.transferTo (0, in.size(), out);

B.
try {Files.copy(Paths.get(source), Paths.get(dest));
Files.delete(Paths.get(source));

C.
try {Files.copy(Paths.get(source), Paths.get(dest));
Files.delete(Paths.get(source));

D.
try {Files.move(Paths.get(source),Paths.get(dest));

E.
try {BufferedReader br = Files.newBufferedReader(Paths.get(source), Charset.forName (“UTF-8”));
BufferedWriter bw = Files.newBufferedWriter (Paths.get(dest), Charset.forName (“UTF-8”));
String record = “”;
while ((record = br.readLine()) != null){
bw.write (record);
bw.newLine();
}
Files.delete(Paths.get(source));



Leave a Reply 1

Your email address will not be published. Required fields are marked *


sandeep

sandeep

If a file already exists then B,D is not the right answer. copy and move methods need the REPLACE_EXISTING option.
Only answer is E that can successfully move the file even if it exists in the target directory.