Read data from console and print in one file. That would be
in C:\temp ? Thanks, Bose

Answers were Sorted based on User's Feedback



Read data from console and print in one file. That would be in C:\temp ? Thanks, Bose..

Answer / pushpa

//Read data from console and print in one file.
import java.io.*;
class ReadDataFromConsole
{
public static void main(String[] args)
{
try{
String fileStr = ""; // declare a string variable
for(int i = 0; i<args.length; i++){ // for loop start
with argument size

fileStr = fileStr + " " + args[i];
}


//create an instance of the random acces file "rfile'.
RandomAccessFile rfile = new
RandomAccessFile("C:/Temp/text.txt","rw");
// write object into the file.
rfile.writeBytes(fileStr);


}catch(Exception ex){
ex.printStackTrace();

}
}
}

Is This Answer Correct ?    3 Yes 2 No

Read data from console and print in one file. That would be in C:\temp ? Thanks, Bose..

Answer / avi

@Pushpa...
If u r reading console data from the method other than main() then u need to use Console class and scanner.

And use StringBuffer in place of String as when u r using '+' operator it creates new string object. So use StringBuffer and use API append("").

Is This Answer Correct ?    1 Yes 0 No

Read data from console and print in one file. That would be in C:\temp ? Thanks, Bose..

Answer / ravi jain

good answer pushpa.. :)

Is This Answer Correct ?    1 Yes 2 No

Post New Answer

More Core Java Interview Questions

Explain when noclassdeffounderror will be raised ?

1 Answers  


What are access specifiers in java ?

1 Answers   Akamai Technologies,


What is classpath?

1 Answers  


What is replacefirst in java?

1 Answers  


State the difference between creating string as new () and literal.

1 Answers  


how a marker interface gets its functionality and when we implements a marker interface how it got invoked

3 Answers   Mind Tree,


What is map and hashmap? also tell the difference.

1 Answers  


What is advantage of using threads?

2 Answers   BMC,


Why deletion in linkedlist is fast than arraylist?

1 Answers  


what is thread? : Java thread

1 Answers  


What is the difference between throw and throws in java?

1 Answers  


Can list contain null in java?

1 Answers  


Categories