Reading a file

Hi,Can anyone tell me how to read a file line by line.I am confused between so many Readers and writers!!Please and urgent Thanks in advanceBye
[185 byte] By [hi_all] at [2007-11-26 12:16:32]
# 1
> Can anyone tell me how to read a file line by line.> I am confused between so many Readers and writers!!> Please and urgent BufferedReader, FileReader, Google, API docs.
CeciNEstPasUnProgrammeur at 2007-7-7 14:52:43 > top of Java-index,Archived Forums,Socket Programming...
# 2

This worked for me.

Mike.

import java.io.*;

public void readFile(){

String line;

URL url=null;

try {

url = new URL (getCodeBase(), "file.txt" );

}

catch (MalformedURLException e ) {

System.out.println("Malformed URL ");

stop();

}

try {

InputStream is=url.openStream();

BufferedReader br = new BufferedReader(new InputStreamReader(is));

while ((line = br.readLine()) != null){

// do whatever with line.

}

is.close();

}

catch (IOException e ) {}

}

MikeTheDinosaur at 2007-7-7 14:52:43 > top of Java-index,Archived Forums,Socket Programming...
# 3
What's that? Why URL? That only makes sense for applets, and even then I'd package the text file as a resource. And why smother the IOException?
CeciNEstPasUnProgrammeur at 2007-7-7 14:52:43 > top of Java-index,Archived Forums,Socket Programming...
# 4

> Hi,

> Can anyone tell me how to read a file line by line.

> I am confused between so many Readers and writers!!

> Please and urgent

>

> Thanks in advance

you can use BufferedReader and FileReader beacuse it is the easy way to handle file....

class fileRead{

public static void main(String args[]){

BufferedReader br = new BufferedReader(new FileReader("inputFile.txt"));

String str;

while((str = br.readLine())!=-1){

System.out.println("Filecontent:"+str);

}

}

}

Message was edited by:

ggopi

ggopi at 2007-7-7 14:52:43 > top of Java-index,Archived Forums,Socket Programming...
# 5
> while((str = br.readLine())!=-1){Ow ow ow owWhat's up today?
CeciNEstPasUnProgrammeur at 2007-7-7 14:52:43 > top of Java-index,Archived Forums,Socket Programming...
# 6

> > while((str = br.readLine())!=-1){

>

> Ow ow ow ow

>

>

> What's up today?

Sorry ya I made a mistake on that line now I cleared that.....

while((str = br.readLine()) != null) {

ggopi at 2007-7-7 14:52:43 > top of Java-index,Archived Forums,Socket Programming...
# 7
I assumed it was an applet.Mike.
MikeTheDinosaur at 2007-7-7 14:52:43 > top of Java-index,Archived Forums,Socket Programming...
# 8
> I assumed it was an applet.> > Mike.Why?
ggopi at 2007-7-7 14:52:43 > top of Java-index,Archived Forums,Socket Programming...
# 9
> > I assumed it was an applet.> > > > Mike.> > Why?Because I'm new to the forum (and java) and it doesn't state otherwise.Mike.
MikeTheDinosaur at 2007-7-7 14:52:43 > top of Java-index,Archived Forums,Socket Programming...