• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

readfile

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ());

}
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic