This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes file access within package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "file access within package" Watch "file access within package" New topic
Author

file access within package

Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
HI - why am I getting an end of file exception when this is part of a package - works find when it is not part of a package. all advice apprecitated.

[Andrew: put the source code between [code] and [/code] UBB tags]
[ April 29, 2004: Message edited by: Andrew Monkhouse ]
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
sorry sorted it, was being thick.
apols
Is RandomFileAccess the way to go with this ?
Denis Spirin
Ranch Hand

Joined: Mar 22, 2004
Posts: 72
Hi Laura
Sure, RAF is ok. The only problem I see here is hard-coded path,
System.getProperty("user.dir") is better.
Btw could you post stack trace?
Regards


Regards<br /> <br />Ph.D.<br />SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJD 2
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
ok thanks. the only problem I have with system.getproperty is this:
I am working in a package suncertify.db. (with suncertify directly under c:/)
So running from the command line in the c:/ java suncertify.db.MyApp to run an application. So the system.getproperty is returning c:/ - which is not the full path to where the file is stored.
any advice?
thanks
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
this seems kind of a stupid solution...

String dir = System.getProperty("user.dir");
String path = "suncertify//db//";
System.out.println (dir);
String file = "db-2x3.db";
File f = new File(dir + path + file);
Denis Spirin
Ranch Hand

Joined: Mar 22, 2004
Posts: 72
The problem is that you dont know from what directory your program will be run.
You may mention in userguide and documentation that dbfile must be in current working directory or something like that. Anyway if you want to hardcode dbpath, in this version your
program wont work on Windows (and this means authomatic failure).

Regards
[Andrew: edited code so that horizontal scrolling is not required to read the posts in this page]
[ April 29, 2004: Message edited by: Andrew Monkhouse ]
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
ok thanks u rule. it was working in windows though - are you thinking file separator causes probs? maybe wouldn't work in UNix?
// escapes the / in windows. but prob. this cause probs in unix.
anyway - so once i get all the data in i create record objects maybe using a column class, kind of make a schema? sorry might have bitten off more than i can chew here, never had major java project at work, only uni.
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
how do i read the numeric vals stored in the header.
come out as | (thick black downwards line) in textpad.
reading/writing with dataoutputstream/datainputstream does not let me read them.
Stephen Galbraith
Ranch Hand

Joined: Oct 27, 2003
Posts: 90
The values are bytes, not ascii so you'd expect them to appear in Notepad/TextPad as something strange.
Just read in the number of bytes you want and make the number up.
Steve


SCJP 1.4, SCJD, SCWCD 1.4
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
sorry? make the number up? can you explain. maybe i've bitten off more than i can chew with this assignment
Denis Spirin
Ranch Hand

Joined: Mar 22, 2004
Posts: 72
As far as I know file separator on Windows 2000 / XP is "\", on Linux and Mac "/"
(I use Mac).
You should not try to "read" header in notepad. File is binary, so you should use
something like readInt(), readShort(), readFully() etc.
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
using readByte.
Laura Williamson
Ranch Hand

Joined: Apr 28, 2004
Posts: 44
ok so
2 byte numeric, number of fields in each record
will require a readShort because short is two bytes, etc, etc.
ok i get it - i think.
i can see that this project is going to be quite a learning curve.
thanks for yr help.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11432
    
  85

Hi Laura,
Welcome to JavaRanch.
I have edited your post to put the code between [code] and [/code] UBB tags. Doing this ensures that indenting is preserved, which makes the code easier to read.
When you are writing your post, there are a number of buttons just below the edit pane, which will insert the tags for you, so you don't have to remember what they are.
If you would like to edit your original post so that you can see what I have done, you can click on the button that is just above your post.
This assignment may have a large learning curve, but I hope you stick with it. As you can see, there are lots of people willing to help.
Regards, Andrew
[ April 29, 2004: Message edited by: Andrew Monkhouse ]

The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Javini Javono
Ranch Hand

Joined: Dec 03, 2003
Posts: 286
Hi,
Concerning File Separators:
I've noticed that I can open a RandomAccessFile whose filename
is specified using the single forward slash like this on Windows
Professional 2000 with Sun JRE 1.5:
"C:/Project/MyDatabase001.db"
or
"C:\\Project\\MyDatabase001.db"
and in classes which extend JDialog, I'm able to enter the filename
into a textfield on Windows Professional 2000 with Sun JRE 1.5
in either of these two forms (both work, just as the two forms above
both work):
C:/Project/MyDatabase001.db
or
C:\Project\MyDatabase001.db
I have not tested this with Windows Professional 2000 with Sun JRE 1.4.
I'm not saying that one should not use the FileSeparator constant, I'm
only pointing out what I have noticed: that version 1.5 does seem to
not force the user to know the file separator for a given operating
system (which makes sense, since Java is not supposed to be bound
to a particular operating system's file system, under ideal circumstances).
Thanks,
Javini Javono
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11432
    
  85

Hi Javini,
The problem with that is that you are relying on an undocumented feature of the particular JVM you are using. While it may work with your particular release of the JVM on MS Windows, there is no knowing what will happen if you change to a different release of the JVM or a different JVM vendor, or a different operating system.
The platform abstraction fields were put there for a purpose. You really should use them.
Regards, Andrew
 
 
subject: file access within package