The Artima Developer Community
Sponsored Link

Java Answers Forum
Read from a file in same directory

1 reply on 1 page. Most recent reply: Oct 7, 2003 10:56 PM by sindu

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
kel

Posts: 2
Nickname: cks
Registered: Oct, 2003

Read from a file in same directory Posted: Oct 7, 2003 9:28 PM
Reply to this message Reply
Advertisement
Hi!Can somebody help me with that.I need to read from a file and display its contents by reversing them.ex:i read John Smith form file i need to display Smith,John.
My code is not working.Is there anything wrong with it?



import java.io.*;
import java.applet.Applet;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

public class DisplayFile extends Applet
{
TextArea text = new TextArea(5,2);

String FileName = "names.txt";
String newLine = System.getProperty("line.operator");

String[] surname = new String[20];
String[] firstname = new String[20];

StringTokenizer linesplit;
int nextElement=0;

public void init()
{
//BufferedReader input;
add(text);

try
{
displayFile();


}
catch(IOException error)
{
System.out.println("Can't open file");

}

}


public void displayFile() throws IOException
{
String path = getCodeBase().getFile();
BufferedReader input = new BufferedReader(
new FileReader("names.txt")); //path+getParameter("name"))

String inputLine = input.readLine();

while(inputLine != null && nextElement < firstname.length
&& nextElement < surname.length)
{

inputLine = input.readLine();
linesplit = new StringTokenizer(inputLine,",");
firstname[nextElement]=linesplit.nextToken();
surname[nextElement]=linesplit.nextToken();
text.append(surname + "," + firstname + newLine);
inputLine = input.readLine();
nextElement++;
}
input.close();
}
}


sindu

Posts: 15
Nickname: sindu
Registered: Sep, 2003

Re: Read from a file in same directory Posted: Oct 7, 2003 10:56 PM
Reply to this message Reply
change the following line

linesplit = new StringTokenizer(inputLine,",");

to

linesplit = new StringTokenizer(inputLine);

Flat View: This topic has 1 reply on 1 page
Topic: How can i hide an applet? Previous Topic   Next Topic Topic: overriding methods in java.lang.object

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use