In: Computer Science
Create a new Java program named AllAboutMe (For JAVA we use blue J)
Write code to have the program print your name, favorite color, and three hobbies to a new text file called “AllAboutMe” using PrintStream.
Submit code.
Java program :
AllAboutMe.java :
//import package
import java.util.*;
import java.io.*;
//Java class
public class AllAboutMe {
// main() method
public static void main(String[] args) throws
FileNotFoundException {
// This line will create object of
Scanner class
Scanner sc = new
Scanner(System.in);
// asking user name
System.out.print("Enter name :
");
// reading name
String name = sc.nextLine();
// asking user favorite
color,
System.out.print("Enter favorite
color : ");
// reading favorite color,
String favoriteColor =
sc.nextLine();
// asking user hobbies
System.out.print("Enter hobbie 1 :
");
// reading hobbie1
String hobbie1 =
sc.nextLine();
// asking user hobbies
System.out.print("Enter hobbie 2 :
");
// reading hobbie2
String hobbie2 =
sc.nextLine();
// asking user hobbies
System.out.print("Enter hobbie 3 :
");
// reading hobbie3
String hobbie3 =
sc.nextLine();
//Object of FileOutputStream
FileOutputStream fileOutStream=new
FileOutputStream("AllAboutMe.txt");
//Object of PrintStream class
PrintStream printStream=new
PrintStream(fileOutStream);
printStream.println("Name :
"+name);//write name
printStream.println("Favorite color
: "+favoriteColor);
//write hobbies
printStream.println("Hobbies are :
"+hobbie1+" , "+hobbie2+" , "+hobbie3);
printStream.close(); //close
PrintStream
try {
fileOutStream.close();//close FileOutputStream
} catch (IOException e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
}
======================================
Screen taking input from user :
File :AllAboutMe.txt