user3141707
user3141707

Reputation: 31

Read text containing multiple line using bufferedreader

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(;);

}   

Upvotes: 3

Views: 29122

Answers (4)

jagmohan
jagmohan

Reputation: 2052

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.

Upvotes: 4

StoopidDonut
StoopidDonut

Reputation: 8617

You can use BufferedReader to loop through each of the line encountered within the specified file. In order to get your words split by a ";", you can use .split and can store the resulting array in a list.

Finally, combine all the lists to a single list which would inturn hold all the words present in your file.

List<String> words = Arrays.asList(line.split(";"));
list.addAll(words);

Now you would want to compare the retrieved list against a Master list containing all your records.

// Compare the 2 lists, assuming your file list has less number of
// records
masterList.removeAll(list);

The above statement can be used in reverse too; in case the file holds the master list of words. Alternatively, you can store the 2 lists in temporary lists and compare in whatsoever way your require.

Here is the complete code:

public static void main(String[] args) {
        String line;

        // List of all the words read from the file
        List<String> list = new ArrayList<String>();
        // Your original mast list of words against which you want to compare
        List<String> masterList = new ArrayList<String>(Arrays.asList("cleaner",
                "code", "java", "read", "write", "market", "python", "snake",
                "stack", "overflow"));

        BufferedReader reader;

        try {
            reader = new BufferedReader(new FileReader("testing.txt"));

            while ((line = reader.readLine()) != null) {
                // Add all the words split by a ; to the list
                List<String> words = Arrays.asList(line.split(";"));
                list.addAll(words);
            }

            // Compare the 2 lists, assuming your file list has less number of
            // records
            masterList.removeAll(list);

            System.out.println(masterList);

            reader.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

File which I have created looks like:

cleaner;code
java;read
write;market
python;snake

The output of the above code:

[stack, overflow]

Upvotes: 0

Rakesh KR
Rakesh KR

Reputation: 6527

Try

while((line=buff.readLine())!=null){
   System.out.println(line);
}

Upvotes: 1

sanket
sanket

Reputation: 789

You need a while loop to read all the lines. Here is an example http://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/

Upvotes: 0

Related Questions