-2

how to generate text file named as a user's name while generating receipt for him/her when he purchases a book,in java console based application.Help me out.

1 Answers1

0
import java.io.File;
import java.io.FileOutputStream;
import java.util.Scanner;

public class BookApplication {

    public static void main(String[] args) {
        try {
        System.out.println("Hi , Enter Your Name ?");
        Scanner scanner =new Scanner(System.in);
        String username= scanner.nextLine();
         String MY_OUTPUT_PATH="";//your  path where you want your file 
        boolean bookPurchased=true; // Do your code for bookpurchase
       if(bookPurchased){

           File receipt= new File(MY_OUTPUT_PATH+ File.separator+username+".txt");
           FileOutputStream outputStream =new FileOutputStream(receipt);

           String sampleData=username+" has purchased a book for rs.500";
           outputStream.write(sampleData.getBytes());
           outputStream.close();

       }
        } catch (Exception e) {
                System.out.println(e);
        }


       }

}

This is what I can give you from your Question. Try working on what you want, then ask Questions.

Arun Xavier
  • 763
  • 8
  • 47