• Post Reply Bookmark Topic Watch Topic
  • New Topic

readfile

 
jason warsowicz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok this is part of my code right now, and i have an issue.
this reads the entire file, how do i get it to go line by line


public void init (){
if (fInBrowser) {
// Get setup parameters from applet html
String param = getParameter ("FileToRead");
if (param != null) {
fFileToRead = new String (param);
}
}

}//Init


/** Use a URL object to read the file **/
public void readFile () {
String line;
URL url=null;

// Get the URL for the file.
try {
if (fInBrowser)
url = new URL (getCodeBase (), fFileToRead );
else {
File file = new File (fFileToRead);
if (file.exists () )
url = file.toURL ();
else {
fTextArea.append ("No file found");
System.out.println ("No file found");
System.exit (0);
}
}
}
catch (MalformedURLException e) {
fTextArea.append ("Malformed URL = "+e);
System.out.println ("Malformed URL= "+e);
return;
}

// Open a stream to the file using the URL.
try {
InputStream in = url.openStream ();
BufferedReader dis =
new BufferedReader (new InputStreamReader (in));
fBuf = new StringBuffer () ;

while ( (line = dis.readLine ()) != null) {
fBuf.append (line + "\n");
}

in.close ();
}
catch (IOException e) {
fTextArea.append ("IO Exception = "+e);
System.out.println ("IO Exception = "+e);
return;
}


} // readFile

/**
* Can use the start () method, which is called after
* init () and the display has been created.
**/


public void start (){
string temp[] = new String[6];
// Now read the file.

readFile ();


for (int x= 0; x < 40; x ++){

tiles[x].setText(fBuf.toString ());

}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does read the file line by line, here:



it's just that all you're doing with each line is appending it to a buffer. If you want to do something else with the individual lines, then do it in this loop.

Note, as an aside, that you never have to use the constructor String(String) -- because Strings are immutable, and Java is a garbage-collected language, you never, ever need to copy a String. This is just a waste.


Finally, as this is an I/O question, not an Applet question, I'm moving this to our I/O and Streams forum.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!