Question

Write a program that reads a file named input.txt and writes a file that contains the...

Write a program that reads a file named input.txt and writes a file that contains the same contents, but is named output.txt. The input file will contain more than one line when I test this. Do not use a path name when opening these files. This means the files should be located in the top level folder of the project. Do not use a copy method that is supplied by Java. Your program must read the file line by line and write the file itself.

DO NOT WRITE TO input.txt! That will cost a lot of points. Double check this. There is no sin worse than wiping out the client's input file due to carelessness.

Your class name should be FileCopy.

Your program must work in Eclipse, even if you use a different IDE to develop it. It is best to use Eclipse. I am starting you with a simple Eclipse project to get you ready for more complex projects.

Upload FileCopy.java.

A large part of your grade will be how well you follow these directions.

Homework Answers

Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class FileCopy {

    public static void main(String[] args) {
        File file = new File("input.txt");
        try {
            Scanner fin = new Scanner(file);
            System.out.print("Enter output file name: ");
            PrintWriter pw = new PrintWriter("output.txt");
            while (fin.hasNextLine()) {
                pw.println(fin.nextLine());
            }
            System.out.println("file is copied successfully");
            pw.close();
            fin.close();
        } catch (FileNotFoundException e) {
            System.out.println(file.getAbsolutePath() + " is not found!");
        }
    }
}
Know the answer?
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
Similar Questions
java program; Write a brief program that writes your name to a file in text format...
java program; Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
● Write code to read the content of the text file input.txt using JAVA. For each...
● Write code to read the content of the text file input.txt using JAVA. For each line in input.txt, write a new line in the new text file output.txt that computes the answer to some operation on a list of numbers. ● If the input.txt has the following: Min: 1,2,3,5,6 Max: 1,2,3,5,6 Avg: 1,2,3,5,6 Your program should generate output.txt as follows: The min of [1, 2, 3, 5, 6] is 1. The max of [1, 2, 3, 5, 6] is...
Java File I/O Write a brief program that writes your name to a file in text...
Java File I/O Write a brief program that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Write a brief program in JAVA that writes your name to a file in text format...
Write a brief program in JAVA that writes your name to a file in text format and then reads it back. Use the PrintWriter and Scanner classes.
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare...
Write a C++ program. 1) Write a program that writes the grades for 3 students. Declare a variable of type ofstream which is used to output a stream into a file: ofstream output; // output is the name of the variable Prompt the user to input values for the grades of 3 students. Use the output operator (<<) to write the grades into grades.txt: output << grade1 << " " << grade2 << " " << grade3 << endl; You...
Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in...
Write a program for: In a text file INPUT.TXT integers separated by a space, perhaps in a few lines. In a single file view to create a list of these numbers and find the arithmetic mean of the list elements. The resulting value is recorded in a text file OUTPUT.TXT.
Write a Java program that reads words from a text file and displays all the words...
Write a Java program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. 1. You must use one of following Concrete class to store data from the input.txt file. Vector, Stack, ArrayList, LinkedList 2. To sort those words, you should use one of existing interface methods available in Collection or List class.
I can open the file in the program, but I cannot figure out how to properly...
I can open the file in the program, but I cannot figure out how to properly split the data and assign a grade to the number values in the text file. I keep getting :: ValueError: invalid literal for int() with base 10: 'Roger Jones 75\n' Below are the assignment details: This program processes grades for a small class. It reads an input file named grade_input.txt where each record contains a student’s first name, last name and numeric grade (a...
Write UNIX commands to perform the following tasks. Each task must use exactly one line of...
Write UNIX commands to perform the following tasks. Each task must use exactly one line of command. Unless stated otherwise, all files are assumed to be at your current working directory. a) (10 points) Print your current working directory into a file named file1 b) (10 points) Assume that your current working directory is NOT the same as your home directory. Print all file and subdirectory names of your home directory into a file named file2. c) (15 points) Copy...
SOLUTION IN C## Write a program that reads every line in a text file, line by...
SOLUTION IN C## Write a program that reads every line in a text file, line by line. Obtain the name of the existing file and the name of the new (copy) of the file by prompting the user for both of these on the console. Make sure that the original file exists and can be read. If not, display a warning message and allow the user to either abort the program or enter a new file name. If the name...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT