Question

How can I create Loops and Files on Java?

• Create a program that reads a list of names from a source file and writes those names to a CSV file. The source file name a

Example Scenario 1 - Normal run: Enter source file name: source.txt Enter destination file name: destination.csv Your file is

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File; //getb properties of file
import java.io.FileWriter; //write the data to the file
import java.io.IOException;
import java.io.FileNotFoundException;
import java.util.Scanner; //read the data from input stream i.e,keyboard
public class CSVFileWriter
{
public static void writeDataToCSVFile(File fileSource,File fileDestination) throws IOException,FileNotFoundException
{
   Scanner fileInput=new Scanner(fileSource);
   FileWriter fileOutput=new FileWriter(fileDestination);
   int n=1;
   fileOutput.write("Number,Name\n");
   while(fileInput.hasNextLine())//read line by line data from file
   {
      fileOutput.write(n+","+fileInput.nextLine()+"\n");
      ++n;
   }
   fileInput.close();
   fileOutput.close();
   System.out.println("Your file is ready!");
}
public static void main(String[] args) throws IOException,FileNotFoundException
{
   String source,destination;
   Scanner input=new Scanner(System.in);
   System.out.print("Enter source file name: ");
   source=input.nextLine();
   System.out.print("Enter destination file name: ");
   destination=input.nextLine();
   File fileSource=new File(source);
   File fileDestination=new File(destination);
   if(!fileSource.exists()) //check if file exists or not
   System.out.println("Source file doesn't exist!");
   else if(fileDestination.exists()) //destination file already exists
   {
     char status;
     System.out.print("Target file already exists.Overwrite existing file?(Y/N):");
     status=input.next().charAt(0);
     while(status=='N')
     {
         input.nextLine();
         System.out.print("Enter destination file name: ");
         destination=input.nextLine();
         fileDestination=new File(destination);
         if(fileDestination.exists()) //if file exists
         {
          System.out.print("Target file already exists.Overwrite existing file?(Y/N):");
          status=input.next().charAt(0);
         }
         else
         break; //file does not exists
       }
     writeDataToCSVFile(fileSource,fileDestination); //write data to csv file
   }
   else
   writeDataToCSVFile(fileSource,fileDestination);
}   
}

Output

destination.csv

Add a comment
Know the answer?
Add Answer to:
How can I create Loops and Files on Java? • Create a program that reads a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program which is called ‘multiple_copy’. This program copies one source file to two...

    Write a C program which is called ‘multiple_copy’. This program copies one source file to two destination files as follows: $./multiple_copy....... source_file....... destination_file1......... destination_file2 multiple_copy program copies the contents of source file (i.e., source_file) to any named two destination files in the command line. The number of arguments should be 4 including multiple_copy. If the number of arguments is not 4, the program should display error message saying: “Usage: multiple_copy source_file destination_file1 destination_file2”. When the source_file does not exist, the...

  • Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text...

    Exercise 3) Creating a QT program to search for Customer Phone Number : Create a text file (customer.txt) and put names and phone numbers into it. each name has to have its own phone number right under it. In the QT window, the user has to enter a name and the program prints the phone number related to it. NB: If the user input a name that doesn't exist, the program should print an error message. Text file example: QT...

  • In C++, Write a program that retrieves a phone number from a txt files of names...

    In C++, Write a program that retrieves a phone number from a txt files of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one...

  • Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin....

    Pig Latin Translator in Java The goal here is to read in the characters stored in a text file and create a second text file that contains the original text but with every word converted to “Pig-Latin.” The rules used by Pig Latin are as follows: • If a word begins with a vowel, just as "yay" to the end. For example, "out" is translated into "outyay". • If it begins with a consonant, then we take all consonants before...

  • Create a python script to manage a user list that can be modified and saved to...

    Create a python script to manage a user list that can be modified and saved to a text file. Input text file consisting of pairs of usernames and passwords, separated by a colon (:) without any spaces User choice: (‘n’-new user account, ‘e’-edit existing user account, ‘d’- delete existing user account, ‘l’- list user accounts, ‘q’-quit) List of user accounts, error messages when appropriate Output text file consisting of pairs of username and passwords, separated by a colon (:) without...

  • Using Java how would I write a program that reads and writes from binary or text...

    Using Java how would I write a program that reads and writes from binary or text files and gives me an output similar to this? Example Output: --------------------Configuration: <Default>-------------------- Enter the file name: kenb Choose binary or text file(b/t): b Choose read or write(r/w): w Enter a line of information to write to the file: lasdklj Would you like to enter another line? Y/N only n Continue? (y/n)y Enter the file name: kenb Choose binary or text file(b/t): b Choose...

  • Java program Program: Grade Stats In this program you will create a utility to calculate and...

    Java program Program: Grade Stats In this program you will create a utility to calculate and display various statistics about the grades of a class. In particular, you will read a CSV file (comma separated value) that stores the grades for a class, and then print out various statistics, either for the whole class, individual assignments, or individual students Things you will learn Robustly parsing simple text files Defining your own objects and using them in a program Handling multiple...

  • JAVA Write a program that prompts the user to enter a file name, then opens the...

    JAVA Write a program that prompts the user to enter a file name, then opens the file in text mode and reads it. The input files are assumed to be in CSV format. The input files contain a list of integers on each line separated by commas. The program should read each line, sort the numbers and print the comma separated list of integers on the console. Each sorted list of integers from the same line should be printed together...

  • in java Complete Program that simulates a login procedure. The program reads a list of names,...

    in java Complete Program that simulates a login procedure. The program reads a list of names, email addresses and passwords from a file p3.txt. Store the information in parallel array lists names, emails and passwords. Your program will prompt the user for their email address. If the email is not in the system, prompt the user to try again. Provide the option to quit. If the email is found in the system, prompt the user to enter their password. After...

  • 4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java...

    4.3Learning Objective: To read and write text files. Instructions: This is complete program with one Java source code file named H01_43.java (your main class is named H01_43). Problem: Write a program that prompts the user for the name of a Java source code file (you may assume the file contains Java source code and has a .java filename extension; we will not test your program on non-Java source code files). The program shall read the source code file and output...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT