问题:编写一个Java程序,该程序从命令行args接受s现有文本文件的列表,并将“ Master.txt”中所有文件的内容串联起来。


测试4 endsWith(".txt")时,我的代码给出了错误。请让我知道如何纠正它。

import java.io.*;
class FileConcat
{
 public static void main(String[] args)
 {
  FileOutputStream fout;
  FileInputStream fin,fin1;
  File f;
  int b;
  try
  {
   //open Master file
   try
   {
    fout=new FileOutputStream("Master.txt");
   }
   catch(Exception e)
   {
    System.out.print(e.getMessage());
   }
   //traverse all args, check if valid text file, if yes, concatinate
   for(int j=0;j<args.length;j++)
   {
    f=new File(args[j]);
    if(f.isFile()==true)
    {
     if((args[j].endsWith(".txt"))==true)
     {
      try
      {
       fin=new FileInputStream(args[j]);
      }
      catch(Exception e)
      {
       System.out.print("Error Opening "+args[j]);
      }
      while((b=fin.read())!=-1)
      {
       char ch=(char) b;
       fout.write(ch);
      }
     }
    fin.close();
    }
   }
   fout.close();
   fin1=new FileInputStream("Master.txt");
   while((b=fin1.read())!=-1)
   {
    char ch=(char) b;
    System.out.print(ch);
   }
   fin1.close();
  }
  catch(Exception e)
  {
   System.out.println(e.getMessage());
  }
 }
}


输出:

C:\j2sdk1.4.1_01\bin>javac FileConcat.java
FileConcat.java:38: variable fin might not have been initialized
                                                while((b=fin.read())!=-1)
                                                         ^
FileConcat.java:41: variable fout might not have been initialized
                                                        fout.write(ch);
                                                        ^
FileConcat.java:44: variable fin might not have been initialized
                                fin.close();
                                ^
FileConcat.java:47: variable fout might not have been initialized
                        fout.close();
                        ^
4 errors


如何检查fin是否分配了值?

最佳答案

import java.io.BufferedWriter; <br>
import java.io.File;<br>
import java.io.FileWriter;<br>
import java.util.Scanner;<br>

public class ConcatFiles
{
    public static String masterFile;


public static void main(String[] args) throws Exception
{
    MasterFile();
}

public static void MasterFile() throws Exception
{
    System.out.println("Enter Master File Name:");
    Scanner readMasterFileName = new Scanner(System.in);
    masterFile = readMasterFileName.next();
    if (new File(masterFile).exists() && masterFile.contains(".txt"))
    readFileName();
    else
    {
        if (masterFile.contains(".txt"))
        {
            new File(masterFile).createNewFile();
            System.out.println("Master File Created");
            readFileName();
        }
        else
        {
            System.out.println("Invalid File or Input");
            MasterFile();
        }
    }
}
public static void readFileName() throws Exception
{
    System.out.println("Enter File Names:");
    Scanner readOtherFiles = new Scanner(System.in);
    String cmd = readOtherFiles.next();

    if (cmd.equalsIgnoreCase("Exit"))
        System.exit(0);
    else
        if (cmd.equalsIgnoreCase("New"))
            MasterFile();
        else
            if (cmd.contains(".txt") && new File(cmd).exists())
            concatFile(cmd);
            else
            {
                System.out.println("Invalid Input or File");
                readFileName();
            }
}
public static void concatFile(String otherFiles)throws Exception
{
    BufferedWriter out = new BufferedWriter(new FileWriter(masterFile, true));
    File readFiles = new File(otherFiles);
    Scanner reader = new Scanner(readFiles);
    while(reader.hasNext())
    {
        out.write(reader.nextLine()+"\n");
    }
    out.close();
    readFileName();
}
}

08-08 00:20