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
  • : Files and Exceptions: Write a program to create and use a grade book for a...

    : Files and Exceptions: Write a program to create and use a grade book for a course. The gradebook is created as a file named as “Course_Name.dat” and includes the following information about students: Student ID, First Name, Last Name, and Grade. Assume the following structure for the gradebook file:1.Students’ records are separated by a new-line character.2.No field includes any white-space character, and fields are separated by space.3.The order of the fields is the same for all the records.Based on...

  • 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...

  • Hello, I needed help on the following In Java: Note that if you intend to append...

    Hello, I needed help on the following In Java: Note that if you intend to append data to the end of a file, you need to create an instance of the "FileWriter" class rather than the "File" class: FileWriter fwriter = new FileWriter("RunningList.txt", true); PrintWriter outfile = new PrintWriter(fwriter); Remember that PrintWriter objects can throw an exception, so make sure to amend your main method header with the clause "throws IOException". (Cannot use try-catch, must include clause "throws IOException") Write...

  • 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...

  • Pig Latin Translator in Java The goal here is to read in the characters stored in...

    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...

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