Read text file in java, index in by lines then write it again

Good night

My fiend asked me a question in java

she want to read an article from text file (for example eadokingInputFile.txt )

then manipulate it (read all word in each line an index it )

put each word with the line number and output them to text file (for example eadokingOutputFile.txt)

input examples :

Eadoking Blog
By Ead AlDoukanje
email1 eadoking@yahoo.com
email1 info@eadoking.com
mob +9363-932728084

http://eadoking.com

output example :

Eadoking , 1
Blog , 1
By , 2
Ead , 2
AlDoukanje , 2
email1 , 3
eadoking@yahoo.com , 3
email1 , 4
info@eadoking.com , 4
mob , 5
+9363-932728084 , 5
http://eadoking.com , 6

now the code. any question just post it in comment

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
*
* @author  Eadoking
* @website http://eadoking.com
* @email   eadoking@yahoo.com
* @email   info@eadoking.com
* @mobile  +963 932728084
*/
public class eadoking_com_Index_Test {
static public String eadokingReadFile(File inputFile) {
StringBuilder indexedContents = new StringBuilder();
try {
BufferedReader input = new BufferedReader(new FileReader(inputFile));
try {
String line = null;
int lineCounter = 0;
while ((line = input.readLine()) != null) {
lineCounter++;
String token[] = line.split(" ");
for (String s : token) {
indexedContents.append(s + " , " + lineCounter);
indexedContents.append(System.getProperty("line.separator"));
}
}
} finally {
input.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
return indexedContents.toString();
}
static public void eadokingWriteIndexed(File outputFile, String indexedContent) throws FileNotFoundException, IOException {
if (outputFile == null) {
throw new IllegalArgumentException("File should not be null.");
}
Writer output = new BufferedWriter(new FileWriter(outputFile));
try {
// create file if not exists
if (!outputFile.exists()) {
outputFile.createNewFile();
}
output.write(indexedContent);
} finally {
output.close();
}
}
public static void main(String[] args) throws FileNotFoundException, IOException {
String inF = null;
String ouF = null;
if (args.length != 0) {
inF = args[0];
ouF = args[1];
} else {
inF = "d:\\eadoknigInputFile.txt";
ouF = "d:\\eadoknigOutputFile.txt";
}
// read an index the file
String indexedContent = eadokingReadFile(new File(inF));
// write the indexed file
eadokingWriteIndexed(new File(ouF), indexedContent);
System.out.println("All Done OK");
System.out.println("By Eadoking Blog\nhttp://eadoking.com");
}
}

Tags: , , , , , , , , , , , , , , , , ,

About eadoking

Ead AlDoukanje - EADOKING - 3edoOo ( عيد الدكنجي Studied Informatics Engineering at Tishreen University ( Software Engineering Department ) Work as freelancer Web Developer / Software Developer You can contact me to me@eadoking.com mobile : ( +963 ) 0932728084 Programming Skills : Java, C# , C++ , Java Server Pages (JSP ) with SERVLETS, PHP ,HTML , CSS ,JAVA SCRIPT with Ajax , ASP.NET , Web Services .

Stay in touch

subscribe with us using social media or email to stay updated
you can also contact us if you need some request or any thing to ,
http://me.eadoking.com/contact-me

Discuss: “Read text file in java, index in by lines then write it again”

  1. January 10, 2010 at 10:40 pm #

    Morooj: للباث اللي

     

    انا ما كتير فهمت القصد بس شوف هيك اذا بيمشي

    هلا الباث لازم يكون من عدة اشكال وابسط شي هوي كما يلي

    c:\test\file.ext

    وهون قصدنا الملف file.ext

    ألموجود في الفولدر test
    على السواقة c

    ولنحط المسا للملف لازم نكتب
    String path = “c:\\test\\file.ext”
    وهون حطينا \\
    لانو اول \ هيي عبارة عن مفتاح هروب
    وال \ التانية هيي عبارة عن تبع المسار

    يعني انو الرمز \ التاني اعتبروا محرف عادي مو محرف خاص

    بكفي الشرح هيك اخوي ولا بدك اكتر بس ياريت توضح تماما شو بتريد من السؤال او شو بتريد مني اشرحلك

    Posted by eadoking
  2. January 10, 2010 at 8:40 pm #

    Morooj: للباث اللي

     

    انا ما كتير فهمت القصد بس شوف هيك اذا بيمشي

    هلا الباث لازم يكون من عدة اشكال وابسط شي هوي كما يلي

    c:testfile.ext

    وهون قصدنا الملف file.ext

    ألموجود في الفولدر test
    على السواقة c

    ولنحط المسا للملف لازم نكتب
    String path = “c:\test\file.ext”
    وهون حطينا \
    لانو اول هيي عبارة عن مفتاح هروب
    وال التانية هيي عبارة عن تبع المسار

    يعني انو الرمز التاني اعتبروا محرف عادي مو محرف خاص

    بكفي الشرح هيك اخوي ولا بدك اكتر بس ياريت توضح تماما شو بتريد من السؤال او شو بتريد مني اشرحلك

    Posted by eadoking
  3. January 10, 2010 at 8:11 pm #

    بليززززز لو سمحت أبغى شرح للباث اللي في الفايل ؟؟

    dierct bath

    Posted by Morooj
  4. January 10, 2010 at 6:11 pm #

    بليززززز لو سمحت أبغى شرح للباث اللي في الفايل ؟؟

    dierct bath

    Posted by Morooj
  5. December 25, 2009 at 5:39 pm #

    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

    Posted by pacienta
  6. December 25, 2009 at 3:39 pm #

    I want to quote your post in my blog. It can?
    And you et an account on Twitter?

    Posted by pacienta
  7. August 24, 2009 at 9:22 pm #

    Nicole: THANK YOU SO MUCH EADOOO IT IS A BIT HARD FOR ME THOUGH.. U NEED TO EXPLAIN SOME STUFF!! SOME WORDS I DIDN’T HEAR OF BEFORE.. I HOPE THAT IS NORMAL!! THANK U SO MUCH UR THE BEST

    Ok,

    You can put the things that you didn’t understand then i’ll explain :)

    Posted by eadoking
  8. August 24, 2009 at 6:22 pm #

    Nicole: THANK YOU SO MUCH EADOOO IT IS A BIT HARD FOR ME THOUGH.. U NEED TO EXPLAIN SOME STUFF!! SOME WORDS I DIDN’T HEAR OF BEFORE.. I HOPE THAT IS NORMAL!! THANK U SO MUCH UR THE BEST

    Ok,
    You can put the things that you didn’t understand then i’ll explain :)

    Posted by eadoking
  9. August 24, 2009 at 8:06 pm #

    THANK YOU SO MUCH EADOOO
    IT IS A BIT HARD FOR ME THOUGH.. U NEED TO EXPLAIN SOME STUFF!!
    SOME WORDS I DIDN’T HEAR OF BEFORE.. I HOPE THAT IS NORMAL!!
    THANK U SO MUCH UR THE BEST

    Posted by Nicole
  10. August 24, 2009 at 5:06 pm #

    THANK YOU SO MUCH EADOOO
    IT IS A BIT HARD FOR ME THOUGH.. U NEED TO EXPLAIN SOME STUFF!!
    SOME WORDS I DIDN’T HEAR OF BEFORE.. I HOPE THAT IS NORMAL!!
    THANK U SO MUCH UR THE BEST

    Posted by Nicole
  11. August 24, 2009 at 4:23 am #

    اي سؤال انا جاهز

    بس حطو بالتعليقات شو السؤال وتكرموا

    Posted by eadoking
  12. August 24, 2009 at 1:23 am #

    اي سؤال انا جاهز
    بس حطو بالتعليقات شو السؤال وتكرموا

    Posted by eadoking

Leave a Reply


*