Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pcplus issue 155 java tutorial on missing links by David Griffiths

Status
Not open for further replies.

afalnes

Programmer
Joined
Oct 14, 2002
Messages
5
Location
NO
To cache the whole content of the web-site
" i've read through some ex in issue155 pcplus java tutorial.Seven - eight classes are utilized ()derived for streaming web-content or file directory HTML pages.THis suggest a possible use of caching web-links to the local harddisk(copying file structure on local -pc or -server).
Well ,I modified the 4th example (Example4.java) to create a textbuffer
(when HTML page is detected) and write the URLcache to a file.The code is as follows:

String path = currentdirroot + address;
String dir = path.substring(0,path.lastIndexOf("/"));
dir.replace('/', File.separatorChar);
new File(dir).mkdirs();
FileOutputStream out = new FileOutputStream(path);
out.write(TextBuffer,0,TextBuffer.length());
out.close();

This program uses the classes derived in the issue155 tutorial ,and the classes are httpwebstream.java and page.java.Thus it creates a socket and connects (both listen and talk) to a relevant web-site via DataInputStream and Printstream.
eg

sock1 = new Socket (" 80);
in = new DatainputStream(sock1.getInputStream());
out= new PrintStream (sock1.getOutputStream());

the code for this is used in page class that detects a httpwebstream or filewebstream.While compiling javac modifiedexample4.java , compiler cannot resolve symbol write in (out.write()). write takes a string and to integers as parameters.thus by inserting the first code part in example4
the error is generated .What is wrong?Anyone who can help?
I'm most grateful of any suggestions or solutions to this matter.
 
Could you please not post irrelevant data, and repost with the relevant code that is not compiling & the compiler error, a bit more clearly.

Cheers

 
Replace...

Code:
out.write(TextBuffer,0,TextBuffer.length());

by

Code:
out.write(TextBuffer.toString(), 0, TextBuffer.length());

"Cannot resolve symbol" means in this case that you try to access a function that does not exist, and true, there's no function write() with a text buffer and two integers.

By the way, using standard Java nomenclature sometimes helps (ie. don't use TextBuffer but textBuffer for the variable name). Have a read in the code conventions. And yes, reading your post was a little painful, try NOt writing like tHis, and use paragraphs, and use
Code:
 for your code sections.

Hope this helps, please post again otherwise!

[center]haslo@haslo.ch - www.haslo.ch[/center]
 
The code below will compile,however during runtime IOException is thrown!
Code:
void writeDiskCacheToFile(String Docroot,String address) throws IOException {

String path = docroot + address; 
String dir = path.substring(0,path.lastIndexOf("/")); 
dir.replace('/', File.separatorChar); 
new File(dir).mkdirs(); 
FileOutputStream out = new FileOutputStream(path); 
out.write(TextBuffer.getBytes(),0,TextBuffer.length()); 
out.close(); 

}
Thus the code will nor create directory or a file.As parameters the method takes two strings Docroot and an URL.(Eg Docroot="C:\Workdirectory" and address="I'm am most grateful of any suggestions or solutions to this matter.
 
Again, you do not post the actual runtime error/exception - please do so ...

--------------------------------------------------
Free Database Connection Pooling Software
 
Here is what I've done
Code:
public  String textBuffer;
 public  String textBuffer;
public void writeDiskCacheToFile(String docRoot,String address) {
     try {
     String path = docRoot + address; 
     String dir = path.substring(0,path.lastIndexOf("/")); 
     dir.replace('/', File.separatorChar); 
     new File(dir).mkdirs(); 
     FileOutputStream out = new FileOutputStream(path);	
    out.write(textBuffer.getBytes(),0,textBuffer.length());
    out.close();
 System.out.println("Writing to disk was successful");
  }
catch (FileNotFoundException e) {
  System.out.println("Writing to disk was unsuccessful");
  }
 }

public void spider(Page page1, int depth, int maxDepth) throws PageNotFoundException {
		ModifiedPage tmpPage;
		String tmpAddress;
        if (depth > maxDepth)
	return;
System.out.println("Checking: " + page1.getTitle() + " (" + page1 + ")");
  Enumeration links = page1.getLinks();
    boolean successful=false;
while (links.hasMoreElements()) {
   tmpAddress = (String)links.nextElement();
   if (tmpAddress.startsWith(webPath)) {
      try {
tmpPage = new ModifiedPage(tmpAddress);
if (tmpPage.isHTML())
  if (!visited.containsKey(tmpAddress)) {
  try {
    visited.put(tmpAddress, tmpAddress);
    textBuffer=tmpPage.getText();
writeDiskCacheToFile("C:\\pcplus\\java\\issue155\\dump",tmpAddress);
spider(tmpPage, depth + 1, maxDepth);
System.out.println("File is writable ");
}
catch (IOException e)  {
System.out.println("File not writable ");
  }
 }
}
catch (PageNotFoundException e) {
	System.out.println("Bad link: " + tmpAddress);
	badLinks.put(tmpAddress, page1.getTitle());
		}
	}
}
}
During running ,while recursively scanning for links on a web page,the output in dos environment, is alternating Writing to disk is unsuccessful and File is writable is messages shown on screen.I'm most grateful of any suggestions or solutions in how to use these exceptions .
thanks
audun_falnes@hotmail.com
 
One more time .... PLEASE post the runtime error message outputted to the console ...

--------------------------------------------------
Free Database Connection Pooling Software
 
afalnes: as long as developing, use
Code:
catch (XyExecption e)
{
    e.printStackTrace ();
}
When it's working, you may use something else, but System.err is meant for errormessages, to allow dividing the output
Code:
catch (PageNotFoundException e) {
    System.err.println("Bad link: " + tmpAddress);
    badLinks.put(tmpAddress, page1.getTitle());
        }
    }
like that:
Code:
java MyClass > myclass.out 2>myclass.err

seeking a job as java-programmer in Berlin:
 
Thanks for help .The error log lie deep in our minds.
"How indigenous they may be,we all wonder who you are"
[2thumbsup]
 
Whatever. You're welcome.

haslo@haslo.ch - www.haslo.ch​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top