aspose file tools*
The moose likes Applets and the fly likes read input from text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Applets
Bookmark "read input from text file" Watch "read input from text file" New topic
Author

read input from text file

karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi guys,
can someone tell me how to take input from a txt file into an applet.i tried it with filereader and the applet worked fine with appletviewer but when i executed it in IE then it loaded allright but it could not read any input from the text files(which were in my hard disc).why is this so..please help,its very urgent..
regards
karan
java puneri
Greenhorn

Joined: Jun 04, 2001
Posts: 14
Hi
this may help you
-------------------------------------
Start Of Code
URL fileURL = null;
String filename = "readme.txt"
// Get File URL
try{
fileURL = new URL(getCodeBase(),filename);
}catch(MalformedURLException e){
getAppletContext().showStatus(e.toString());
}
String readLine="";
Vector data=null;
//Read the FILE
try{
BufferedReader aReader = new BufferedReader(new
InputStreamReader(fileURL.openStream()));
while((readLine = aReader.readLine())!= null){
data.addElement(readLine);
}//end of while
aReader.close();
}catch(IOException e){
getAppletContext().showStatus(e.toString());
}
// process the vector to retrive the information....
End of Code
----------------------------------------------------------------

Play around to get thru !
dont give filename as hardcoded path .
i.e. e.g "C:\data\readme.txt" =========> should be avoided !

Cheers
PuneGuy !
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
thanks a lot for the reply,but the problem was solved already..actually i guess the problem was with the plugin because when i used a html converter to convert the html file for the applet and then loaded the html file in IE then the applet worked fine..actually converter made some changes in the html file so that i would download the jdk plugin from the sun site evreytime its loaded.so basically i think it was a problem of the plugin after all and not the input..
thanks anyways for ur help
regards
karan
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hey puneri i need some more help from you..i was wondering if u could bail me out...actually the problem is that my applet ia a JApplet and in that i have some buttons.so the problem is that when i load the applet(either in appletviewer or in a browser) then the buttons dont show up automatically but only when i scroll my mouse over the region where they are supposed to be displayed.why is this so?
for your information,i am using a contentpane and setting the layout to null.,following is the code for the same..
public class Simple extends JApplet implements ActionListener{
String msg = "The Design Network";
static int pointWidth = 4;
static int pointHeight = 4;
public static double[][] arr1 = new double[100][100]; //stores the respective node number and its X & Y co-ord.
public double[] arr2 = new double[10]; //stores the total no of nodes in the file and the scale factors
public static double[][] arr11 = new double[100][100]; //stores the respective link number and its details
public static double[] arr22 = new double[10]; //stores the total no of links in the file and the scale factors
public static double[][] inc = new double[100][100];
public int[][] h = new int[100][10];
public int[][] in = new int[100][10];
public static double tokens;
static int i;

public int test=0;
private JButton But1, But2, But3;
private boolean laidOut = false;
public int width,height;
public int v1,v2,v3,v4,v5;


public void init(){
setBackground(Color.black);
Container contentPane = getContentPane();
contentPane.setLayout(null);
width = Integer.parseInt(getParameter("width"));
height = Integer.parseInt(getParameter("height"));
v1 = (int)(height/9);
v2 = (int)(0.8*width);
v3 = v1+10;
v4 = v2/2-10;
v5 = v3+50;

But1 = new JButton("Text Format Input");
But2 = new JButton("Mdb Format Input");
contentPane.add(But1);
contentPane.add(But2);
But1.addActionListener(this);
But2.addActionListener(this);


//if (!laidOut) {
Insets insets = contentPane.getInsets();
But1.setBounds(v2+50+insets.left,v3+20+insets.top,130,30);
But2.setBounds(v2+50+insets.left,v3+70+insets.top,130,30);
//laidOut = true;
//}
But1.setBackground(Color.lightGray);
But2.setBackground(Color.lightGray);
But1.setForeground(Color.blue);
But2.setForeground(Color.blue);
But1.setFont(new Font("TimesRoman",Font.BOLD,11));
But2.setFont(new Font("TimesRoman",Font.BOLD,11));
}

public void actionPerformed(ActionEvent ae){
String str = ae.getActionCommand();
if(str.equals("Text Format Input")){
test=1;
}
else{
test=2;}
repaint();
}
public void paint(Graphics g){
g.setColor(Color.lightGray);
g.fillRect(0,v1,1300,10);
g.fillRect(v2,v3,10,1000);
try{
drawPoint(g);
}catch(Exception e){System.out.println("U Dumbo!");}
try{
drawlink(g);
}catch(Exception e){System.out.println("screwed up!");}
try{
g.setColor(Color.red);
inclink(g);
}catch(Exception e){System.out.println("screw u!");}
draw(g);
}
}
this is just the part where i am using the buttons..
plz help me out..
thanks in advance
karan
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Karen,
I have run your code in AppletViewer and the buttons appeared just fine! I'm using JDK1.2 ...
Regards,
Manfred.
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi manfred..is that so???well thats very disheartning coz i am using jdk 1.3..what do u think is the possible reason of this discrepancy?
regards
karan
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hey puneri,
i take back my previos reply..actually the applet worked fine when i loaded it in the browser but only when everything(ie, my java file,my class file,my input text file and my applet html file) were on my hard drive.but when i tried to upload the applet on the web using frontpage then i loaded allright but it didnt take any input from the text files.i tried to run ur code of the URL thing but it didnt work..i dunno why,it kept throwing exceptions..can i have ur email id so that i can mail my entire code with the input files to u so that u can take a look and help me ou with this problem..i would really appreciate that..
plz plz plz help
regards
karan
karan, chopra
Ranch Hand

Joined: Jun 05, 2001
Posts: 115
hi puneri,sorry again..ur idea worked and now my applet is working fine except the buttons part which u might have read in my previous replies..any idea how to take care of that?
regards
karan
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: read input from text file
 
Similar Threads
reading text file in java
Netbeans and Scanner.in
can you
string input for txt file..
Date type conversion from dd/mm/yyyy to yyyy-mm-dd