How to read data from the keybord and write it to myfile.txt file. - LearnHowToCode SarkariResult.com Interview Questions and Answers LearnHowToCodeOnline
How to read data from the keybord and write it to myfile.txt file.

How to read data from the keybord and write it to myfile.txt file.

import java.io.*;
class CreateFile
{
public static void main(String[]arr)
throws IOException
{
// attech keyword to DataInputStream

DataInputStream dis=new DataInputStream(System.in);

// attch my file to fileOutput Stream

FileOutputStream  fos=new FileOutputStream("myfile.txt");
System.out.println("Enter text (0 to the end )");
char ch;
// read charachters from dis into ch. Then write them into fouut.
// repeat this as long as the read Character is not @

while ((ch=(char)dis.read())!='@')
            fos.write(ch);
            // close the file
            fos.close();

}
}

 That is Output

E:\ShashiJava>javac CreateFile.java
E:\ShashiJava>java CreateFile
Enter text (0 to the end)
This is my file line one
This is my file line two
@

E:\ShashiJava>myfile.txt
E:ShashiJava>

This is my file line one
This is my file line two




About EasyToCode

I'm Ethan Mariano a software engineer by profession and reader/writter by passion.I have good understanding and knowledge of AngularJS, Database, javascript, web development, digital marketing and exploring other technologies related to Software development.

0 comments:

Featured post

Political Full Forms List

Acronym Full Form MLA Member of Legislative Assembly RSS Really Simple Syndication, Rashtriya Swayamsevak Sangh UNESCO United Nations E...

Powered by Blogger.