Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Reading a text file

    Hi,

    I know so far how to read a text file as input into the program, but how can I read a specific word contained in a text file and copy it as a String object and pass it onto another method? Sounds complicated :S
    Thanks

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a text file

    Read the file, line by line. Find the word and copy it to a String variable.
    How will you identify the location and length of the word?
    If you already know what you are going to find in the file, what is the purpose of reading the file?

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Reading a text file

    Sorry let me re-phrase the question. Well what im trying to do is read a text file and to find the line on which the word is, and its ID number. For an example below; where this may be contained in the text file, and say I wanted to find the PID by providing 0.0.0.0:135 to search it by. Where the program should then return 1012. And sorry, not copy it as a String variable but an integer variable and to pass it into a method?
    Thanks. Hope you can understand where i'm comming from?

    Proto Local Address Foreign Address State PID
    TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1012
    TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
    Attached Thumbnails Attached Thumbnails Reading a text file-untitled.jpg  

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a text file

    Is this what you are trying to do:
    Find a line with a specific String and then extract another String from another position on that line.

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Reading a text file

    Yes exactly, or rather find the ID number on the same line as the searched String given?
    Thanks

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a text file

    The String class has methods for detecting if a String contains another String and for getting substrings out of a String.

  7. #7
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Reading a text file

    Ok. But how do I scan the text file in order to search for a specific String? I am aware of the methods that String supplies.
    Thanks

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a text file

    I know so far how to read a text file
    how do I scan the text file
    These are confusing.
    Can you explain what these two statements mean?
    If you know how to read the lines of a text file into a String,
    that is all there would be to do to scan a text file. Read a line and scan it.

  9. #9
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Reading a text file

    Work off of the fact that each field is separated by a whitespace, so you can use the String.split() method to get an array of Strings from each record.

    Example:
    Java Code:
    String line1 = "TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1012"
    String[] fields = line1.split(" ");
    
    System.out.println(fields[0]); //output: TCP
    System.out.println(fields[1]); //output: 0.0.0.0:135
    System.out.println(fields[2]); //output: 0.0.0.0:0
    System.out.println(fields[3]); //output: LISTENING
    System.out.println(fields[4]); //output: 1012
    Now that you know how to find the values, you should store them in a hashmap to get the values easily
    Java Code:
      private final int KEY_FADDR = 1;
      private final int KEY_PID = 4;
    
      //keep track of all PIDs
      Map<String,Integer> myPIDs = new HashMap<String,Integer>();
    
      //loop through each line in the text file
      for (String line:String[] textfile) {
        String[] fields = line.split(" ");
        String foreignAddr = fields[KEY_FADDR];
        int pid = Integer.valueOf(fields[KEY_PID]);
        myPIDs.put(foreignAddr, pid);
      }
    Now you can get the value anytime like this:
    Java Code:
    int pid = myPIDs.get("0.0.0.0:135"); //pid = 1012
    Last edited by ozzyman; 03-11-2012 at 02:51 AM.

  10. #10
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Reading a text file

    Oh yeah, sorry for my confusing grammer lol. What i mean is i know how to create a FileInputSream object containing the text file and streaming the file into the program using InputStreamReader. So therefore I would like to return the ID number contained in the file and pass it onto another method. All this would be in run-time where id would input a string and return the number along that line under PID. Hope this makes sence?
    Thanks

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a text file

    Read the file line by line.
    test if the line contains the String.
    Get the substring
    convert the String to an int
    return the int

  12. #12
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Reading a text file

    If you want to pass it onto another method its pretty simple with the example I gave you

    Java Code:
    public int getPID(String foreignAddr) {
      return myPIDs.get(foreignAddr);
    }

  13. #13
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Reading a text file

    Ok thanks alot, this looks very promising. Il try this out see how it works.
    Thanks again

  14. #14
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Reading a text file

    Quote Originally Posted by ozzyman View Post
    Work off of the fact that each field is separated by a whitespace, so you can use the String.split() method to get an array of Strings from each record.

    Example:
    Java Code:
    String line1 = "TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 1012"
    String[] fields = line1.split(" ");
    
    System.out.println(fields[0]); //output: TCP
    System.out.println(fields[1]); //output: 0.0.0.0:135
    System.out.println(fields[2]); //output: 0.0.0.0:0
    System.out.println(fields[3]); //output: LISTENING
    System.out.println(fields[4]); //output: 1012
    Now that you know how to find the values, you should store them in a hashmap to get the values easily
    Java Code:
      private final int KEY_FADDR = 1;
      private final int KEY_PID = 4;
    
      //keep track of all PIDs
      Map<String,Integer> myPIDs = new HashMap<String,Integer>();
    
      //loop through each line in the text file
      for (String line:String[] textfile) {
        String[] fields = line.split(" ");
        String foreignAddr = fields[KEY_FADDR];
        int pid = Integer.valueOf(fields[KEY_PID]);
        myPIDs.put(foreignAddr, pid);
      }
    Now you can get the value anytime like this:
    Java Code:
    int pid = myPIDs.get("0.0.0.0:135"); //pid = 1012

    for (String line:String[] textfile)

    is the textfile suppose to be the actual text file? Because its not compiling?
    Thanks

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a text file

    Post the full text of the error messages.

  16. #16
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: Reading a text file

    No you were supposed to do that bit yourself... Read a text file into a Collection<String> or String Array (whichever you prefer) and then pass the field into the for loop.

    It was more of a pseudo code/example code for you than actual code.

    I thought you said you're find with reading the text file anyway?
    Last edited by ozzyman; 03-11-2012 at 09:36 PM.

  17. #17
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Reading a text file

    Oh yea I have read in the textfile.txt into an array. Ive got one problem, its giving me an error NumberFormatException, which I think its trying to convert the String into an integer, and the first 4 lines do not contain numbers until the 5th line, which is shown below:

    Active Connections

    Proto Local Address Foreign Address State PID
    TCP 0.0.0.0:135 0.0.0.0:0 LISTENING 508
    TCP 0.0.0.0:445 0.0.0.0:0 LISTENING 4
    TCP 0.0.0.0:554 0.0.0.0:0 LISTENING 5856

    The 'Active Connections' is on the second line, the 3rd is an empty line, and the 4th line containes the headings 'Proto Local Address Foreign Address State PID'. Where the 5th line then starts with the PID's etc.
    So would I have to tell the for loop to start looping from the 5th index of the textfile array? Or how would I get around this problem?
    Thanks

    Im getting the error from this line: int pid = Integer.valueOf(fields[KEY_PID]);

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a text file

    What is in: fields[KEY_PID]?
    For debugging use the Arrays toString() method to format the contents of an array for printing.

    the first 4 lines do not contain numbers until the 5th line
    If you know which line has the number, skip the preceding lines. You could have a loop that reads and ignores lines before the lines with the numbers.

  19. #19
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Reading a text file

    The process ID numbers, which is in the 4th field (column) in each line. KEY_PID is assigned to 4.
    Use Arrays to String() method to format the contents of an array for printing. Would can I do this within the following code below?
    Java Code:
    while ((strLine = br.readLine()) != null)   
    		{
      			// Print the content on the console
      			//System.out.println (strLine);
    			for(int i=0; i<textfile.length; i++)
    			{
    			        //assign each line to to textfile
    				textfile[i] = br.readLine();
                                    //Trying to format the textfile?
    				System.out.println(textfile[i].toString());
    			}
    		}
    Although when I ran it, the textfile looked the same as before. Maybe I doing it the wrong way?
    Last edited by Norm; 03-12-2012 at 04:33 PM. Reason: added code tags

  20. #20
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Reading a text file

    What array do you want to print?
    If textfile is an array of Strings you do not need to call the toString() method. The elements are Strings.

    What did you expect the code you posted to do?
    Post the code's output and add an explanation of what you want the output to look like?

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 8
    Last Post: 07-17-2011, 02:38 PM
  2. Reading in a text file
    By TheRealHoff in forum AWT / Swing
    Replies: 10
    Last Post: 02-07-2010, 11:47 PM
  3. reading text file
    By trofyscarz in forum New To Java
    Replies: 1
    Last Post: 02-05-2010, 02:24 AM
  4. Reading two text file and sum them up
    By matt_well in forum New To Java
    Replies: 36
    Last Post: 07-22-2008, 02:55 AM
  5. Reading text file
    By Lennon-Guru in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 11:38 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •