aspose file tools*
The moose likes Java in General and the fly likes How do i go about this... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do i go about this..." Watch "How do i go about this..." New topic
Author

How do i go about this...

Karl Barnard
Greenhorn

Joined: May 15, 2009
Posts: 20
Write a method called sizeFile() declared as follows:

public int sizeFile (String label) {
...
}

which counts the number of characters in the file whose name is supplied as the "label" parameter. This method should return the number of characters in the file, or -1 if an IOException occurs.


I'm not looking for someone to answer this for me, i'm just looking for hints, tips or examples of how i can go about this.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12785
    
    5
Start by study of the java.io.File class, learn how to open a file by name.
Next look at the various classes in java.io for reading files.

Bill
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Bill, it's not clear to me why you say the File class. Wouldn't the FileReader class be more appropriate? regards.

http://java.sun.com/docs/books/tutorial/essential/io/streams.html

If you are going to use files, you'll need to know a little about handling exceptions. the link discusses this.

Also, if I was keen to get started on the algorithm, I'd probably set up a string to mirror the file, and read characters from the string one at a time. You could switch to files later.

I can see you making use of the contains() method of the String class.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
You can use the Scanner class, but you will have difficulty getting your IOException.

I think people have told you enough for you to get it working.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
And please read this FAQ about thread titles.
Karl Barnard
Greenhorn

Joined: May 15, 2009
Posts: 20
Thanks a lot guys, all your post have been noted.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
You're welcome
Karl Barnard
Greenhorn

Joined: May 15, 2009
Posts: 20
Written up a code, just need to test it.
Karl Barnard
Greenhorn

Joined: May 15, 2009
Posts: 20


This is the code, but is says "cannot find symbol - method length()".

Anyone know what i'm doing wrong?
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Karl Barnard wrote:

This is the code, but is says "cannot find symbol - method length()".

Anyone know what i'm doing wrong?


file is defined as an object in the FileReader class. In order for this to work, the FileReader class would have a length() method in it, or in a parent class. It does not. It does however have a read() method. If you follow the link I put up in an earlier post, you will see an example of how to use it.

length() will tell you the length of a string, but it won't tell you the length of a file.
Karl Barnard
Greenhorn

Joined: May 15, 2009
Posts: 20
Okay i got it.
Karl Barnard
Greenhorn

Joined: May 15, 2009
Posts: 20
delete
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Please don't delete text like that. People coming to this thread later won't know whether you have deleted something important or not. I happen to remember you said something like

"Thank you, am working on it"

but if the message is deleted, then we don't know whether there was something important which has been removed.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

Fred Hamilton wrote:length() will tell you the length of a string, but it won't tell you the length of a file.

length() will tell you the length of a file - if you call it on an instance of java.io.File, not java.io.FileReader.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Fred Hamilton
Ranch Hand

Joined: May 13, 2009
Posts: 679
Rob Prime wrote:
Fred Hamilton wrote:length() will tell you the length of a string, but it won't tell you the length of a file.

length() will tell you the length of a file - if you call it on an instance of java.io.File, not java.io.FileReader.


duly noted, thanks, my suggestion of FileReader may not have been optimal either, I think I was confused about the exact nature of the task. Will endeavor to be more careful in the future.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do i go about this...