tags:

views:

267

answers:

5

Hi,

What I am doing is I am reading in a html file and I am looking for a specific location in the html for me to enter some text. So I am using a bufferedreader to read in the html file and split it by the tag . I want to enter some text before this but I am not sure how to do this. The html would then be along the lines of ...(newText)(/HEAD) (The brackets round head are meant to be angled brackets. Don't know how to insert them)

Would I need a PrintWriter to the same file and if so, how would I tell that to write it in the correct location. I am not sure which way would be most efficient to do something like this. Please Help.

Thanks in advance.


Here is part of my java code:

     File f = new File("newFile.html");
     FileOutputStream fos = new FileOutputStream(f);
     PrintWriter pw = new PrintWriter(fos);


  BufferedReader read = new BufferedReader(new FileReader("file.html"));
  String str;
  int i=0;
  boolean found = false;
  while((str= read.readLine()) != null)
  {

   String[] data = str.split("</HEAD>");


   if(found == false)
   {
    pw.write(data[0]);
    System.out.println(data[0]);
    pw.write("</script>");
    found = true;
   }
   if(i < 1)
   {
   pw.write(data[1]);
   System.out.println(data[1]);
   i++;
   }
   pw.write(str);
   System.out.println(str);


     }
   }
   catch (Exception e) {
     e.printStackTrace( );
     }

When I do this it gets to a point in the file and I get these errors:

FATAL ERROR: MERLIN: Unable to connect to EDG API,

Cannot find .edg_properties file.,

java.lang.OutOfMemoryError: unable to create new native thread,

Cannot truncate table,

EXCEPTION:Cannot open connection to server: SQLExceptio,

Caught IOException: java.io.IOException: JZ0C0: Connection is already closed, ...

I'm not sure why I get these or what all of these mean?

please Help.

+1  A: 

Most people suggest writing to a temporary file and then copying the temporary file over the original on successful completion.

Tim Bender
+3  A: 

Should be pretty easy:

  • Read file into a String
  • Split into before/after chunks
  • Open a temp file for writing
  • Write before chunk, your text, after chunk
  • Close up, and move temp file to original

Sounds like you are wondering about the last couple steps in particular. Here is the essential code:

File htmlFile = ...;
...
File tempFile = File.createTempFile("foo", ".html");
FileWriter writer = new FileWriter(tempFile);
writer.write(before);
writer.write(yourText);
writer.write(after);
writer.close();
tempFile.renameTo(htmlFile);
Sean Owen
A: 

The forum thread has some ideas of how to do it. GL.

dcruz
A: 

For reading and writing you can use FileReaders/FileWriters or the corresponding IO stream classes.

For the editing, I'd suggest to use an HTML parser to handle the document. It can read the HTML document into an internal datastructure which simplifies your effort to search for content and apply modification. (Most?) Parsers can serialize the document to HTML again.

At least you're sure to not corrupt the HTML document structure.

Andreas_D
A: 

Following up on the list of errors in your edit, a lot of that possibly stems from the OutOfMemoryError. That means you simply ran out of memory in the JVM, so Java was unable to allocate objects. This may be caused by a memory leak in your application, or it could simply be that the work you're trying to do does need more memory transiently than you have allocated it.

You can increase the amount of memory that the JVM starts up with by providing the Xmx argument to the java executable, e.g.:

-Xmx1024m

would set the maximum heap size to 1024 megabytes.

The other issues might possibly caused by this; when objects can't reliably be created or modified, lots of weird things tend to happen. That said, there's a few things that look like you can take action. In particular, whatever MERLIN is it looks like it can't do it's work because it needs a property file for EDG, which it's unable to find in the location it's looking. You'll probably need to either put a config file there, or tell it to look at another location.

The other IOExceptions are fairly self-explanatory. Your program could not establish a connection to the server because of a SQLException (the underlying exception itself will probably be found in the logs); and some other part of the program tried to communicate to a remote machine using a closed connection.

I'd look at fixing the properties file (if it's not a benign error) and the memory issues first, and then seeing if any of the remaining problems still manifest.

Andrzej Doyle
The "OutOfMemoryError" is not due to running out of heap space.It is because of allocating a bunch of threads (see it complains about not being able to create a new thread). You'd have to create a whole lot of threads to run out of (non-heap) memory, even though the standard thread stack size is like 2MB. You are probably making too many threads. You can reduce the thread stack size with -Xss512k or something, but that is not really addressing the problem and probably causes other issues.Indeed the rest is related to some app-specific framework that's barfing.
Sean Owen