Results 1 to 12 of 12
Like Tree2Likes
  • 1 Post By Junky
  • 1 Post By stchman

Thread: I want to know how many lines are in a File I open

  1. #1
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default I want to know how many lines are in a File I open

    I want to know how many lines are in a File I open:

    fc is a JFileChooser.
    fileDispatch is a File.
    iDispatchFileLineCount is an int.

    Java Code:
    fileDispatch = fc.getSelectedFile();
    iDispatchFileLineCount = (int)fileDispatch.length();
    I guess "length()" is the count of characters in the file. It's close, anyway (298, whereas MSWord says it's 260).

    How do I get the count of lines in the File? Count the instances of "crlf" or something?

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    As far aas I know the only way is to read the file line by line and increment a counter. Alternatively, if this is a file created, read, written entirely by your program and not used outside of it then you can add the line count as the first line.
    blackbird likes this.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default

    (298, whereas MSWord says it's 260).
    38/2 = 19
    Does the file have 19 lines? crlf not being counted by MSWORD

  4. #4
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Norm View Post
    38/2 = 19
    Does the file have 19 lines? crlf not being counted by MSWORD
    20 (Twenty)

  5. #5
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Junky View Post
    As far aas I know the only way is to read the file line by line and increment a counter. Alternatively, if this is a file created, read, written entirely by your program and not used outside of it then you can add the line count as the first line.
    Yes, that's what I want to do - but I need to know how many lines the file contains before I can do that - because once I get to the end, I want to "wrap around" by setting the counter back to 0 (it loops reading the file until the user tells it to stop).

    BTW: Is that a rabid rabbit?
    Last edited by blackbird; 07-22-2011 at 04:10 PM. Reason: Nur um es richtig zu gestalten

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Do a loop? While its not null, set this variable to this variable+1. When it is null, set this variable to another variable and do what else you wanted to do?

    While readLine != null
    increment counter
    if its null, set linesInFileVarto increment counter.
    Set increment counter to 0
    Do whatever else you planned on doing afterwards.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Alright, it's working, and here's the code, in all it's mud, blood, and beer-covered "glory":

    Java Code:
    		timerDispatchFileSendAndReceive = new Timer(1000, new ActionListener() {
    			public void actionPerformed(ActionEvent actev) {
    				String s = "";
    				try {
    					s = bufReaderDispatchFile.readLine();
    					if (s == null) {
    						ReloadDispatchFile();
    						s = bufReaderDispatchFile.readLine();
    					}
    				} catch (IOException e2) {
    					e2.printStackTrace();
    				}
    				
    				byte[] byteBuffer = null;
    				byteBuffer = s.getBytes(); 
    				try {
    					outDispatchFile.write(byteBuffer);
    					taSent.append("FROM DISPATCH FILE: " + new String(byteBuffer) + "\n");
    					inDispatchFile.readFully(byteBuffer);
    					taReceived.append("DISPATCH RESPONSE: "
    							+ new String(byteBuffer) + "\n");
    				} catch (IOException e) {
    					taReceived.append("ERROR\n");
    				}
    			}
    		});
    BTW, I bet y'all didn't know that the title of this post was taken from a hit song by Foreigner from "back in the daze"
    Last edited by blackbird; 07-22-2011 at 06:01 PM. Reason: just coz

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    They came before me, and I ignore them.

    So the point of posting you code was what? That you figured it out or that it doesn't work?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    They came before me, and I ignore them.

    So the point of posting you code was what? That you figured it out or that it doesn't work?
    To post my solution; IMO everybody should post the working code after a resolved issue, so others searching for the same or similar issues can find them.

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I disagree, I would simply suggest a manner of hints to figure out how to solve it. Not the actual method, I don't believe in spoon feeding. I've never accepted it as an answer.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  11. #11
    blackbird is offline Senior Member
    Join Date
    Jul 2011
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by Dark View Post
    I disagree, I would simply suggest a manner of hints to figure out how to solve it. Not the actual method, I don't believe in spoon feeding. I've never accepted it as an answer.
    Different strokes for different folks.

  12. #12
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    The .length() method of the File class will return the length of the file in bytes. Refer to the Java API.

    I use the BufferedReader class. There is a method called ready(), it returns a boolean if EOF is encountered and you can issue a break from a while loop while counting:

    Psuedo code

    Java Code:
    BufferedReader infile
    int i
    
    while true
        if infile.ready == false
            break
    
        infile.readline
        i++
    
    end while
    
    infile.close
    Hope this helps.
    Last edited by stchman; 07-22-2011 at 10:04 PM.
    blackbird likes this.

Similar Threads

  1. Load file with a lot or lines.
    By juamd in forum Advanced Java
    Replies: 9
    Last Post: 04-26-2011, 07:41 PM
  2. how to remove duplicate lines from a file
    By trkece in forum New To Java
    Replies: 11
    Last Post: 02-16-2011, 06:01 AM
  3. Different Files Open in File -> Open Workspace
    By mgm2010 in forum JCreator
    Replies: 0
    Last Post: 04-11-2009, 02:14 PM
  4. How to remove 2 last lines in a text file?
    By Marius in forum New To Java
    Replies: 2
    Last Post: 11-30-2008, 03:54 PM
  5. Replies: 0
    Last Post: 04-06-2008, 07:45 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
  •