Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

unsure about my error here

  Asked By: Lloyd    Date: Feb 11    Category: Java    Views: 451
  

I keep getting a FileCopySift class error or something and i justu
can't figure it any assistantce would be greatly appreciated.

import java.io.*;

public class FileCopySift
{
public static void main(String args[])
{
if (args.length != 3)
{
System.err.println("How to use correct syntax with this
program:");
System.err.println("java FileCopySift <source file>
<destination> <filter>");
}
else
try
{
copy(args[0], args[1], args[2]);
}
catch(IOException ioexception)
{
System.err.println(ioexception.getMessage());
}
}

public static void copy(String one, String two, String three)
throws IOException
{
File file1 = new File(one);
File file2 = new File(two);
if(!file1.exists())
abort("ERROR: no such source file: " + one);
if(!file1.isFile())
abort("ERROR: can't copy directory: " + one);
if(!file1.canRead())
abort("ERROR: source file is unreadable: " + one);
if(file2.isDirectory())
file2 = new File(file1, file1.getName());
if(file2.exists())
{
if(!file2.canWrite())
abort("ERROR: destination file is unwriteable: " +
two);
System.out.print("Overwrite existing file " + two + "?
(Y/N): ");
System.out.flush();
BufferedReader bufferedreader = new BufferedReader(new
InputStreamReader(System.in));
String five = bufferedreader.readLine();
if(!five.equals("Y") && !five.equals("y"))
abort("ERROR: existing file was not overwritten.");
} else
{
String four = file1.getParent();
if(four == null)
four = System.getProperty("user.dir");
File file3 = new File(four);
if(!file3.exists())
abort("ERROR: destination directory doesn't exist: "
+ four);
if(file3.isFile())
abort("ERROR: destination is not a directory: " +
four);
if(!file3.canWrite())
abort("ERROR: destination directory is unwriteable: "
+ four);
}
FileReader filereader = null;
FileWriter filewriter = null;
try
{
filereader = new FileReader(file1);
filewriter = new FileWriter(file2);
BufferedReader bufferedreader1 = new BufferedReader
(filereader);
PrintWriter printwriter = new PrintWriter(filewriter);
String six = bufferedreader1.readLine();
for(; six != null; six = bufferedreader1.readLine())
{
StringBuffer stringbuff1 = new StringBuffer(six);
StringBuffer stringbuff2 = new StringBuffer();
stringbuff2.setLength(six.length());
if(six.indexOf(three) != -1)
{
int i = 0;
int j = 0;
while(i < stringbuff1.length())
if(stringbuff1.charAt(i) == '<')
{
for(; i < stringbuff1.length() &&
stringbuff1.charAt(i) != '>'; i++);
i++;
} else
{
stringbuff2.setCharAt(j,
stringbuff1.charAt(i));
i++;
j++;
}
six = stringbuff2.toString();
printwriter.println(six);
}
}

}
finally
{
if(filereader != null)
try
{
filereader.close();
}
catch(IOException ioexception) { }
if(filewriter != null)
try
{
filewriter.close();
}
catch(IOException ioexception1) { }
}
}

private static void abort(String one) throws IOException
{
throw new IOException(one);
}
}

Share: 

 

3 Answers Found

 
Answer #1    Answered By: Djoser Massri     Answered On: Feb 11

i am not sure whats ur exact problem ...........but i used your code,
compiled it and tried to execute it. i think it works fine
ur program copies file  from the source file to the destination file
based upon the filter. so if ur filter is "public" all the lines from
the source file which have the word public  in it are copied into the
destination file
if this is what was intended then boy u r right on target.

 
Answer #2    Answered By: Sherrie Thomas     Answered On: Feb 11

How did you invoke the programs execution? i kept using my.html
my.txt none with none fro the filter or doi use public/private?

 
Answer #3    Answered By: Anselma Schmidt     Answered On: Feb 11

i compiled the code, and then executed it passed 3 params source file
dest file  and the filter

all the info has to be in the command line. i used the java  source file
as the source file and some file as dest and public  as the filter word

so it looked like

java FileCopySift FileCopySift.java 1.txt public

and it seemed to work

 
Didn't find what you were looking for? Find more on unsure about my error here Or get search suggestion and latest updates.




Tagged: