kel
Posts: 2
Nickname: cks
Registered: Oct, 2003
Read from a file in same directory
Posted: Oct 7, 2003 9:28 PM
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(); } }