Re: Help Help, I am intermediate in Java...need help in follow case
On Oct 1, 3:49 pm, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
ElementX wrote:
On Oct 1, 1:24 pm, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
ElementX wrote:
On Oct 1, 12:42 pm, RedGrittyBrick <RedGrittyBr...@spamweary.invalid>
wrote:
ElementX wrote:
There is two java classes
second class is for execute ant file.
First Class does the most.
first create a plain class which accepts two java.io.File object in
constructor as input and output and a method which performs
replacement.
- the replacement must take into account the file ending of the inp=
ut
file:
+ for .java, .cpp, .c and .css remove the FIRST occurrence of "=
/
* ... */"
+ for .xml, .xhtml, .html and .xsl remove the FIRST occurrence =
of
"<!-- ...
-->"
has any body have an example class to do it?
Thanks a lot...if you can help...I create a class but is not
working...maybe I can post it here...
Do post it here. It would help if you explain *exactly* what you mea=
n by
"is not working".
--
RGB
the following code was wrote for mappings in jar file...maybe can
help...
[code omitted]
If you wrote that then you are skilled enough in Java that you don't
need my help.
If I were tackling the assignment you describe, I would not start with
the code you posted. I'd start from scratch.
Assuming you didn't in fact write the code you posted, you might find
Patricia's guide helpful:http://home.earthlink.net/~patricia_shanahan/=
beginner.html
I'd start with the requirement "first create a plain class which accep=
ts
two java.io.File object in constructor as input". From this I'd write
class ReplaceFirstComment {
ReplaceFirstComment(File input, File output) {
// TODO
}
}
Then I'd compile it and test it. I'd not move on until I was entirely
happy with the test results and had eliminated any compiler warnings.
Then I'd think about the next tiny change that I could make that I cou=
ld
compile and test.
--
RGB
Do you have an idea...or an example file...which can help more
My idea was you take my starting point (above) and build on it step by st=
ep.
Here's how I imagined proceeding ...
----------------------------------------------------
C:\temp>type ReplaceFirstComment.java
class ReplaceFirstComment {
ReplaceFirstComment(File input, File output) {
// TODO
}
}
C:\temp>javac ReplaceFirstComment.java
ReplaceFirstComment.java:2: cannot find symbol
symbol : class File
location: class ReplaceFirstComment
ReplaceFirstComment(File input, File output) {
^
ReplaceFirstComment.java:2: cannot find symbol
symbol : class File
location: class ReplaceFirstComment
ReplaceFirstComment(File input, File output) {
=
^
2 errors
----------------------------------------------------
OK so we need to fix those errors ...
----------------------------------------------------
C:\temp>type ReplaceFirstComment.java
import java.io.*;
class ReplaceFirstComment {
ReplaceFirstComment(File input, File output) {
// TODO
}
}
C:\temp>javac ReplaceFirstComment.java
C:\temp>
----------------------------------------------------
It compiles, now to test it, it should do nothing, does it?
----------------------------------------------------
C:\temp>java ReplaceFirstComment.java
Exception in thread "main" java.lang.NoClassDefFoundError:
ReplaceFirstComment/java
Caused by: java.lang.ClassNotFoundException: ReplaceFirstComment.java
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native =
Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown =
Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Sou=
rce)
C:\temp>
----------------------------------------------------
Oh, we are missing a "main" method. We need to add some sort of test
harness, so I'll deal with both issues the simplest way.
----------------------------------------------------
C:\temp>type ReplaceFirstComment.java
import java.io.*;
class ReplaceFirstComment {
ReplaceFirstComment(File input, File output) {
// TODO
}
public static void main (String[] args) {
File input = new File(args[0]);
File output = new File(args[1]);
new ReplaceFirstComment(input, output);
}
}
C:\temp>javac ReplaceFirstComment.java
C:\temp>java ReplaceFirstComment ReplaceFirstComment.java Replaced.java
C:\temp>
----------------------------------------------------
OK, test sucessful? - your turn.
At this point you may be thinking "Hey RGB, I'm an intermediate Java
programmer and I know all this stuff, it's trivial, why are you wasting
your and my time with it?"
The point is you haven't followed this process else you'd have some
small example code that is more developed than this and shows us where
you got stuck.
So what have you come up with so far?
--
RGB
At first thanks a lot...for your help...
so I create some code...what do yu think...what is the next step...?
Now, I can replace // comments from a text file...how I need a
solution for follow problem...
+ for .java, .cpp, .c and .css remove the FIRST occurrence of "/* ...
*/"
+ for .xml, .xhtml, .html and .xsl remove the FIRST occurrence of
"<!-- ...
I create flowing code....
public class RemoveHeader {
private File toDir;
private List<FileSet> filesets = new ArrayList<FileSet>();
private List<String> includes = new ArrayList<String>();
private List<String> excludes = new ArrayList<String>();
@SuppressWarnings("unused")
private String charset;
public RemoveHeader() {
// TODO Auto-generated constructor stub
}
/**
* @param charset
* @param excludes
* @param filesets
* @param includes
* @param mappingFile
* @param mappings
* @param toDir
*/
// File input, File output
public RemoveHeader(String charset, List<String> excludes,
List<FileSet> filesets, List<String> includes, File Input) {
super();
this.charset = charset;
this.setExcludes(excludes);
this.setFilesets(filesets);
this.setToDir(Input);
}
public static void main(String[] args) {
// String charset = "UTF-8";
// String[] exclude = {"*.java","*.c","*.cpp"};
// String[] include = {"*.xml","*.xhtml"};
// List<String> includesa = new ArrayList<String>();
// List<String> excludesa = new ArrayList<String>();
// includesa.addAll(includesa);
// excludesa.addAll(exclude);
// FileSet fs = new FileSet();
// List<FileSet> lfs = new ArrayList<FileSet>();
// File in = new File ("");
// RemoveHeader rh = new
RemoveHeader(charset,excludesa,lfs,includesa,in);
// DirectoryScanner ds = new DirectoryScanner();
// ds.addExcludes(exclude);
// ds.setIncludes(include);
// ds.scan();
// rh.removeCommentFromFile("test.txt", "/*");
RemoveHeader rh = new RemoveHeader();
rh.removeCommentFromFile("test.txt", "//");
}
/**
* Defines the directory where to save the replaced files. This is
a
* mandatory parameter.
*
* @param toDir the directory where to save files
*/
public void setTodir(File toDir) {
this.setToDir(toDir);
}
/**
* Defines the character encoding used for reading and writing
files.
*
* @param charset a valid character encoding for the current Java
platform
*/
public void setCharset(String charset) {
this.charset = charset;
}
public void removeCommentFromFile(String file, String comment) {
try {
File inFile = new File(file);
if (!inFile.isFile()) {
System.out.println("Parameter is not an existing
file");
return;
}
//Construct the new file that will later be renamed to the
original filename.
File tempFile = new File(inFile.getAbsolutePath() +
".tmp");
BufferedReader br = new BufferedReader(new
FileReader(file));
PrintWriter pw = new PrintWriter(new
FileWriter(tempFile));
// String lineToRemove = line;
//Read from the original file and write to the new
//unless content matches data to be removed.
String line = null;
while ((line = br.readLine())!=null) {
if (!line.trim().startsWith(comment)) {
pw.println(line);
pw.flush();
}
}
pw.close();
br.close();
// //Delete the original file
// if (!inFile.delete()) {
// System.out.println("Could not delete file");
// return;
// }
//Rename the new file to the filename the original file
had.
if (!tempFile.renameTo(inFile))
System.out.println("Could not rename file");
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
// getter and setter
public void setIncludes(List<String> includes) {
this.includes = includes;
}
public List<String> getIncludes() {
return includes;
}
public void setExcludes(List<String> excludes) {
this.excludes = excludes;
}
public List<String> getExcludes() {
return excludes;
}
public void setFilesets(List<FileSet> filesets) {
this.filesets = filesets;
}
public List<FileSet> getFilesets() {
return filesets;
}
public void setToDir(File toDir) {
this.toDir = toDir;
}
public File getToDir() {
return toDir;
}
}