Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Code to read a character in the file

  1. #1
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Smile Code to read a character in the file

    hi,

    i need a code that can read a character in the file. The example file is like this:

    The quick brown fox,,,
    field 1, 344
    request: 12344 fjsdlfkj
    bit 1
    bit 2
    fields2
    response fkljsdfljewir
    bit 1
    bit 2
    bit 3
    successfull
    request
    bit 1
    bit 2
    response
    failed
    Then, i will read that file(above) and then get the request word until before the response. Then get the response until before the word successful.

    But my code below only shows per line basis not per word or character. please help me.

    Sample output is like this:

    request: 12344 fjsdlfkj
    bit 1
    bit 2
    fields2

    response fkljsdfljewir
    bit 1
    bit 2
    bit 3

    =====
    import java.awt.image.DataBuffer;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
     
    public class ReadFile 
    {
        private String fileName = null;
        private int bufferSize = 1000;
     
        public ReadFile ()
        {}
     
        public ReadFile (String fileName, int bufferSize)
        {
            this.setFileName (fileName);
            this.setBufferSize (bufferSize);    
        }
     
        public ReadFile (String fileName)
        {
            this(fileName, 1000);
        }
     
        public void setFileName (String fileName)
        {
            this.fileName = fileName;    
        }
     
        public String getFileName ()
        {
            return this.fileName;    
        }
     
        public void setBufferSize (int bufferSize)
        {
            this.bufferSize = bufferSize;
        }
     
        public int getBufferSize ()
        {
            return this.bufferSize;    
        }
     
        public ArrayList read () throws java.io.FileNotFoundException, java.io.IOException
        {
            FileReader fr = new FileReader (this.getFileName());
            BufferedReader br = new BufferedReader (fr);
            ArrayList aList = new ArrayList (this.getBufferSize());
     
            String line = null;
            while (     (line = br.readLine()) != null)
            {
                aList.add(line);
            }
     
            br.close();
            fr.close();
     
            return aList;
        }
     
        public static void main (String args[])    //include main for testing purposes
        {
            try
            {
                ReadFile rf = new ReadFile("C:\\txn.log");
                ArrayList a = rf.read();
     
                if (a.size() > 0)
                {
                    int i, x;
                    for (i=0; i<a.size(); i++)
                    {
                    if(a.contains("request")) {
     
                         String test = (String) a.get(i+1);
     
                         System.out.println (test);
     
                    }
                   }
                    System.out.println ("EOF");
                }
            }
            catch (Exception e)
            {
                System.out.println (e.getMessage());    
            }
        }
    }


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: File Reader... help please

    Hello Truffy, welcome to the forums

    So given this input file:

    The quick brown fox,,,
    field 1, 344
    request: 12344 fjsdlfkj
    bit 1
    bit 2
    fields2
    response fkljsdfljewir
    bit 1
    bit 2
    bit 3
    successfull
    request
    bit 1
    bit 2
    response
    failed
    What is your expected output? Is this it?

    request: 12344 fjsdlfkj
    bit 1
    bit 2
    fields2

    response fkljsdfljewir
    bit 1
    bit 2
    bit 3

    request
    bit 1
    bit 2

    response
    failed
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: File Reader... help please

    Quote Originally Posted by JavaPF View Post
    Hello Truffy, welcome to the forums

    So given this input file:

    What is your expected output? Is this it?
    hi. thank you for welcoming me.


    yup, thats the output.

    i just need to get the request data and its response.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: File Reader... help please

    Hello Truffy,

    Does this application need to be generic? Will it support other text files in the same request/response format or will you only be dealing with the example above?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Member Truffy's Avatar
    Join Date
    May 2009
    Location
    Philippines
    Posts
    93
    Thanks
    2
    Thanked 9 Times in 7 Posts

    Default Re: File Reader... help please

    Quote Originally Posted by JavaPF View Post
    Hello Truffy,

    Does this application need to be generic? Will it support other text files in the same request/response format or will you only be dealing with the example above?
    it will support other extension files. that program is reading server logs, meaning it is in tail, i just listen
    to the file and if there is another transaction, i will get it again and parse it. do you think that file reader is the
    best approached to that project? the server logs is on the back end, and i will make a front end logs. c++ linux is our
    back end. one of my friend told me that i can used java using ssh but i cant figure out how to do it.