Results 1 to 3 of 3
  1. #1
    dynamic3d is offline Member
    Join Date
    Aug 2010
    Location
    Los Angeles
    Posts
    2
    Rep Power
    0

    Default Simple Applet not reading non *.txt file

    I have a simple applet example that reads dummy.txt file located in the same server location just fine. When I change the dummy.txt file to a dummy.dat file, and change just the file name in the code, I get an error:

    java.io.FileNotFoundException: http://www.virtualbeach.com/test/dummy.dat

    The dummy.dat and dummy.txt test files are located at:


    http://www.virtualbeach.com/test/readDatFileApplet.htm



    http://www.virtualbeach.com/test/readTxtFileApplet.htm



    I've been stumped for some time on this and really appreciate the help.
    And here's the code:

    Java Code:
    import java.applet.*;
    import java.awt.*;
    import java.io.*;
    import java.net.URL;
    import java.net.MalformedURLException;
    
    public class readDatFileApplet extends Applet {
    	// dummy.dat file same as dummy.txt file.
    	String fileToRead = "dummy.dat";
    
    	StringBuffer strBuff;
    	TextArea txtArea;
    
    	public void init(){
    		txtArea = new TextArea("", 100, 100, TextArea.SCROLLBARS_BOTH );
    		txtArea.setEditable(true);
    		add(txtArea, "center");
    		String prHtml = this.getParameter("fileToRead");
    		if (prHtml != null) fileToRead = new String(prHtml);
    		readFile();
    	}
    
    
    	public void readFile(){
    		String line;
    		URL url = null;
    
    		URL codeBase = getCodeBase();
    		try{
    			url = new URL(getCodeBase(), fileToRead);
    		}
    		catch(MalformedURLException e){}
    		try{
    			java.net.URLConnection urlConnection = url.openConnection();
    			InputStream in = urlConnection.getInputStream();
    			BufferedReader bf = new BufferedReader(new InputStreamReader(in));
    			strBuff = new StringBuffer();
    			txtArea.append("File name: " + fileToRead + "\n");
    			while((line = bf.readLine()) != null){
    				strBuff.append(line + "\n");
    			}
    			txtArea.append(strBuff.toString());
    		}
    		catch(IOException e){
    			System.out.println(e.toString());
    			e.printStackTrace();
    		}
    	}
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,883
    Rep Power
    25

    Default

    Could you post the full text of the error message. When you edit it, you could leave out something important.

    For debugging, add a println("url=" + url);
    following where you define the url for the file

    Does your server have any restrictions on the kind of files it will serve?
    When I use the URL: http://www.virtualbeach.com/test/dummy.dat
    I get a return code of 404
    Last edited by Norm; 08-14-2010 at 10:59 PM.

  3. #3
    dynamic3d is offline Member
    Join Date
    Aug 2010
    Location
    Los Angeles
    Posts
    2
    Rep Power
    0

    Default

    You're 100% correct. The mime type wasn't set.
    Wow! Did that ever give me trouble.
    Thanks for the help.

Similar Threads

  1. [SOLVED] Applet reading from file on server
    By DenniGa in forum Java Applets
    Replies: 3
    Last Post: 02-27-2009, 12:33 AM
  2. Reading a file from Applet (BufferedReader)
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 06-22-2008, 11:51 PM
  3. Applet - reading text files packed into JAR file
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-08-2008, 10:15 AM
  4. I need simple java applet
    By silvia in forum Java Applets
    Replies: 4
    Last Post: 01-27-2008, 08:50 AM
  5. Reading/Writing files through Applet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-15-2007, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •