2
import java.io.*;
public class CopyFile {

   public static void main(String args[]) throws IOException {
      FileReader in = null;
      FileWriter out = null;

      try {
         in = new FileReader("input.txt");
         out = new FileWriter("output.txt");

         int c;
         while ((c = in.read()) != -1) {
            out.write(c);
         }
      }finally {
         if (in != null) {
            in.close();
         }
         if (out != null) {
            out.close();
         }
      }
   }
}

Here is my code

It can only read a text named'input.txt'

when I run it. it can be

$javac CopyFile.java
$java CopyFile

But my TA need me todo something like this.

You must write a makefile document which creates a executable file named CopyFile.

Your program is required to take the input file name as an argument. Following is an example of a program that read from a file named file_name.

$CopyFile file_name

I googled 'how to write a makefile' and 'java read a file and output a file'. But found nothing like this situation. It is not $java CopyFile , it is $CopyFile file_name the CopyFile become something like java. Can someone teach me how to do this or how to google it? I even don't know what is the key word related to this situation to google it...

tonyyan
  • 21
  • 4
  • "public static void main (String[] args) " I think this solution is to get parameter from command line while the program is running. But I dont want to input anything while program is running . I want to input a 'file_name' before the program started. and I can generate a output.txt automatically... – tonyyan Oct 08 '16 at 05:15
  • It looks like, your assignment is to a) write the CopyFile program, and b) write a makefile that builds it. It is not clear that the program should be written in Java, but if you use Java, you should wrap it so that they can run it without typing `java CopyFile`. Here are some tools of trade for that: http://stackoverflow.com/questions/147181/how-can-i-convert-my-java-program-to-an-exe-file – Alex Cohn Oct 08 '16 at 06:03
  • Its a project, and will be run on school server. So I have to follow the requirement strictly. Otherwise, I can't get output. I still can't find this situation : $CopyFile file_name .... – tonyyan Oct 08 '16 at 06:18
  • I don't know what your assignment is, and what should CopyFile program do to the input file specified on the command line. I even don't know what OS you are using. But I suspect that people who gave you this assignment did not expect you to write it in Java. Maybe, they assumed that you will use C? – Alex Cohn Oct 08 '16 at 06:34
  • I use Java on Mac. the program should read a input file to get a output file. the input file name is 'file_name'. then following the requirement, I think I need to write something to run in Terminal. which can be $CopyFile file_name. Project can use C++ and Java. Do you mean C++ can achieve this format ($CopyFile file_name), but Java can't do this? – tonyyan Oct 08 '16 at 06:45
  • The wrappers that are listed in the link above, can produce an executable file from Java, but this is not a "natural" thing to do. Same holds for makefile. It is not impossible to use make to build a Java program, but we usually use different tools for Java, and typical use of make is to build a C program. – Alex Cohn Oct 08 '16 at 07:08
  • 1
    Thanks, TA said he will update the project file – tonyyan Oct 08 '16 at 15:55

0 Answers0