Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File access

 
Ranu Kundu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a folder which contains the index.html file describing an applet name newapplet.java.In this newapplet.java it accesses a file which is in same folder and its contents are to be read.I have uploaded this folder in webserver.But I cannot see the contents.But when I test this applet from my machine I can see the contents.Where is the problem?
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the part of the code that accesses the file. Also see HelloServerFile for an example applet that demonstrates how to do it.
 
Ranu Kundu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea,but it cannot find the file specified
 
Ranu Kundu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.applet.Applet;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.Label;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

/**
*
* @author Adminstrator
*/
public class NewApplet extends Applet implements ActionListener{

/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
TextArea ta;
public void init() {
Button b=new Button("Read File");
b.addActionListener(this);;
add(b);
ta=new TextArea();
add(ta);
}
public void actionPerformed(ActionEvent e)
{
String line = null;

try
{
URL textURL = new URL( getCodeBase() , "./Mech/mech.dat" );
BufferedReader reader = new BufferedReader( new InputStreamReader( textURL.openStream() ) );

// now read the lines one at a time
while ( ( line = reader.readLine() ) != null )
{
add( new Label( line ) );
} // close while loop

reader.close();
} // close try
catch (Exception ex )
{
ex.printStackTrace();
}



}
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what happens if you run the code? Any exceptions? If so, which ones?

If the file is in a subdirectory "Mech" of the directory where the applet's HTML file is in, remove the "./" from the path.
 
Ranu Kundu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HTML file is outside this directory.Yes I have removed ./
import java.applet.Applet;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.Applet;
import java.awt.FlowLayout;
import java.awt.Label;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

/**
*
* @author Adminstrator
*/
public class NewApplet extends Applet implements ActionListener{

/**
* Initialization method that will be called after the applet is loaded
* into the browser.
*/
TextArea ta;
public void init() {
Button b=new Button("Read File");
b.addActionListener(this);;
add(b);
ta=new TextArea();
add(ta);
}
public void actionPerformed(ActionEvent e)
{
String line = null;

try
{
URL textURL = new URL( getCodeBase() , "Mech/mech.dat" );
BufferedReader reader = new BufferedReader( new InputStreamReader( textURL.openStream() ) );

// now read the lines one at a time
while ( ( line = reader.readLine() ) != null )
{
add( new Label( line ) );
} // close while loop

reader.close();
} // close try
catch (FileNotFoundException ex )
{
System.out.println("NULL");
}



It prints NULL.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the server-side log files to see which file it's trying to access, and make sure that the file permissions permit the file to be downloaded; you can try this by entering the URL in the browser.
 
Ranu Kundu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am uploading the html file from my PC to the server.In fact I have uploaded the project created in NEtbeans IDE which has src folder containing the source file i.e, .java file.The html file is outside the folder of the project.Now for the applet to access the data file where the .dat file should be kept.??I am confused.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The source files should not be on the server, only the class files. You can keep the .dat file anywhere on the server, provided it's publicly accessible and the applet uses the correct path. The easiest is probably to keep it in the same directory as the HTML file.
 
Ranu Kundu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok,so I have to upload the jar file,html file,data file.Will this work if from client I access the html file?And I have posted a code above also.Please look at the code,whether it can be done with this ? If I am wrong please correct me.

 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the code you have does not work, and you don't want to follow the suggestion I made in my third post, then the easiest is probably to start with the example I gave in my first post.
 
Ranu Kundu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea,Now it's reading from file but it is very slow while giving the output.Y is it so?
 
Ranu Kundu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to refresh 2-3 times to view the contents.Y?
 
Ranu Kundu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yea,this issue is resolved now.thanks for your help..
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic