Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read text containing multiple line using bufferedreader

Tags:

java

I would like to know how to read a text file containing multiple lines in java using BufferedStreamReader. Every line has two words separated by (;) and I want to use split() String operation to separate the 2 words. I also need to compare each word to a word in a master arraylist. I'm having problems to continue.

Here's my code:

{

FileInputStreamReader f = new FileInputStreamReader(C://Desktop/test.txt);

InputStreamReader reader = new InputStreamReader(f);

BufferedReader Buff = new BufferedReader (reader);

String Line = buff.readLine();

String t[] = Line.split(;);

}   
like image 774
user3141707 Avatar asked Nov 29 '25 19:11

user3141707


2 Answers

Replace

String Line = Buff.readLine();

with

// buffer for storing file contents in memory
StringBuffer stringBuffer = new StringBuffer("");
// for reading one line
String line = null;
// keep reading till readLine returns null
while ((line = Buff.readLine()) != null) {
    // keep appending last line read to buffer
    stringBuffer.append(line);
}

Now, you have read the complete file into StringBuffer, you do whatever you want.

Hope this helps.

like image 66
jagmohan Avatar answered Dec 02 '25 08:12

jagmohan


Try

while((line=buff.readLine())!=null){
   System.out.println(line);
}
like image 20
Rakesh KR Avatar answered Dec 02 '25 09:12

Rakesh KR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!