Tutorial :how to add two words from text file using java program


I am new to java. I think this is the simplest problem but even i dont know how to solve this problem. I have one text file. In that file i have some words like below :

good bad efficiency

I want to add list of words into another by using java program. My output want to be like this

good bad good efficiency bad efficiency

How to get this using java program. I tried search for some ideas. But i wont get any idea. Please suggest me any ideas. Thanks in advance.


If you do not want to learn it from scratch I would recommend using the Apache Commons io library. The FileUtils class has a simple interface to read from and write to a file.


A good place to start learning Java IO would be to look over Sun's Java Tutorials on File IO. If you're looking into how to read in individual lines, I would particularly look at Scanners. And if at some point you're looking to manipulate Strings like this without IO being heavily involved, I'd look at Java's StringBuilder.


import java.io.*;    class Test {       //--------------------------------------------------< main >--------//       public static void main (String[] args) {        Test t = new Test();        t.readMyFile();     }         //--------------------------------------------< readMyFile >--------//        void readMyFile() {           String record = null;         String rec=null;         int recCount = 0;           try {              FileReader fr     = new FileReader("c:/abc/java/prash.txt");            FileReader fr1    = new FileReader("c:/abc/java/pras.txt");            BufferedReader br = new BufferedReader(fr);            BufferedReader br1 = new BufferedReader(fr1);            record = new String();            rec = new String();            while ((record = br.readLine()) != null && (rec=br1.readLine())!=null) {              // recCount++;               System.out.print(record +" "+  rec);               //System.out.print(rec);            }           } catch (IOException e) {            // catch possible io errors from readLine()            System.out.println("Uh oh, got an IOException error!");            e.printStackTrace();         }        } // end of readMyFile()     } // end of class  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »