I have a programming question that calls for me to write a program that concatenates the contents of several files into one file. For example,
java CatFiles chapter1.txt chapter2.txt chapter3.txt book.txt
makes a long file, book.txt, that contains the contents of the files chapter1.txt, chapter2.txt, and chapter3.txt. The output file is always the last file specified on the command line.

The main class from my instructor

import java.io.IOException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

/**
   This program concatenates contents of several files into one file.
*/
public class CatFiles
{
   public static void main(String[] args) throws IOException
   {
      if (args.length < 2)
      {
         System.out.println(
               "Usage: CatFiles sourcefile1 sourcefile2 . . . targetfile");
         return;
      }

      String target = args[args.length - 1] ;

      . . . //Write code

      for (int i = 0; i < args.length - 1; i++)
      {
         String source = args[i];
         . . . //Write code
      }

      . . . //Write code

   }
}

What I am having trouble with trying to figure out where exactly to put my Filereader, PrintWriter, and Scanner. Have not had many problems using the args in this class so that is why it is confusing. Any help would be appreciated.

are you sure your code inside your for loop is correct?

ok, I'm sorry. that line is correct but a little redundant...

Ok I think I may have it solved

import java.io.IOException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

/**
   This program concatenates contents of several files into one file.
*/
public class CatFiles
{
   public static void main(String[] args) throws IOException
   {
      if (args.length < 2)
      {
         System.out.println(
               "Usage: CatFiles sourcefile1 sourcefile2 . . . targetfile");
         return;
      }

      String target = args[args.length - 1] ;
      FileReader reader =null;
      PrintWriter out = new PrintWriter(target);

      for (int i = 0; i < args.length - 1; i++)
      {
         String source = args[i];
         reader = new FileReader(source);
         
      }

      Scanner in = new Scanner(reader);
      while (in.hasNextLine())
      {
    	  try
    	  {
    	  String line = in.nextLine();
    	  out.println(line);
    	  }
    	  
      finally
      {
      in.close();
      out.close();
      }

      }
   }
}

have you tried it to see if it works?

Possibly. Can you run the following please;

Download OTL to your Desktop.

* Double click on the icon to run it. Make sure all other windows are closed and to let it run uninterrupted.
* Under the Custom Scan box paste this in:


netsvcs
%SYSTEMDRIVE%\*.exe
/md5start
eventlog.dll
scecli.dll
netlogon.dll
cngaudit.dll
sceclt.dll
ntelogon.dll
logevent.dll
iaStor.sys
nvstor.sys
atapi.sys
IdeChnDr.sys
viasraid.sys
AGP440.sys
vaxscsi.sys
nvatabus.sys
viamraid.sys
nvata.sys
nvgts.sys
iastorv.sys
ViPrt.sys
eNetHook.dll
ahcix86.sys
KR10N.sys
nvstor32.sys
/md5stop
%systemroot%\*. /mp /s
%systemroot%\system32\*.dll /lockedfiles
%systemroot%\System32\config\*.sav
CREATERESTOREPOINT

* Click the Quick Scan button. Do not change any settings unless otherwise told to do so. The scan wont take long.

  • When the scan completes, it will open two notepad windows: OTL.txt and Extras.txt. These are saved in the same location as OTL.
  • Please copy (Edit->Select All, Edit->Copy) the contents of these files, one at a time, and post them back here.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.