Re: Open a file in JApplet

From: Andrew Thompson (SeeMySites_at_www.invalid)
Date: 08/26/04


Date: Thu, 26 Aug 2004 14:20:55 GMT

On 25 Aug 2004 15:46:34 -0700, Jenny wrote:

> I tried to open a file and read it into an JApplet. It does not
> display the string in the file. Could tell me which part of code does
> not run and why?

Part of the problem is that this code ignores errors..

>...Could you tell me how to fix it?

You might start by always ensuring you print an
exception even if, ..perhaps especially if, that
exception or error 'cannot' happen.

>...Thanks a lot. Here
> is the code:

I have not had time to try you complete example Jenny,
but here is the tip I have for you..

> Java file 1:
>
...
> try {
> File f = new File("hello.txt");
> FileInputStream file = new FileInputStream(f);
> byte[] b = new byte[20];
> file.read(b);
> String s = new String(b);
> comp2D.drawString(s,20,20);
> file.close();
> } catch (Exception e) {

  // here is the better way to deal with this..
  e.printStacktrace();

> }
> }
> }
> }

Once you do that, you should see errors appearing in the
Java console of your browser.. They might look complicated
and scary at first, but the info. on this page might help
you get used to reading and understanding them.
<http://www.physci.org/codes/javafaq.jsp#exact>

It is not just handy, but vital to be able to
start debugging Java problems by using the information
in the stack trace..

There is also information on a different Java group
further down that page, I suspect you might get better
help there for the moment if you are a beginner, it is
<http://www.physci.org/codes/javafaq.jsp#cljh>

People who post there generally take a little extra
time to help the people new to Java.

HTH

-- 
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology


Relevant Pages

  • Re: New String.isNumeric() function in Java 1.6 ?
    ... String.inNumeric() function in Java 1.6 (To detect if a string is ... Another one could be done with a regular expression, ... but that is not as correct as the exception check variant. ...
    (comp.lang.java.help)
  • Re: CMutex /CEvent (multiple threads)
    ... deals with exception detection. ...  If your function does not handle an exception in Java, ... designer did not understand what a Mutex was and that the notion that Lock could return ... ...roll back transaction ...
    (microsoft.public.vc.mfc)
  • Newbie question
    ... this is day 3 of using Java so ... String strFromStream = new String; ... It's complaining about the KeyboardStream.readyline. ... might throw an exception?? ...
    (comp.lang.java)
  • =?iso-8859-1?q?Re:_Beyond_Java_Tiger_-_Defekte_und_L=FCcken_in_Java?=
    ... > synchronized-Block erweitert und dann eine Bibliothek für Threads zur ... man kann nicht sicher mehrere locks requiren ... > Java machen kann, wenn man denn will. ... > eine Exception soll abgefangen werden damit ich noch ein CleanUp machen ...
    (de.comp.lang.java)
  • Re: Question regarding linking and moving - any advice greatly appreciated
    ... > user will move to the new room and display the text ... > the books I have (Head First Java, ... class BookSection {// why don't you simply call it Room? ... String text; // why don't you call it 'description'? ...
    (comp.lang.java.help)