Question: write a Java program which accepts s list of existing text files from command line args and concatenates the contents of all files in "Master.txt".
It gives error while testing 4 endsWith(".txt"). Kindly correct it
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
How do I check if fin
is assigned a value or not?