aspose file tools*
The moose likes Java in General and the fly likes File Access object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "File Access object" Watch "File Access object" New topic
Author

File Access object

Kevin Adams
Greenhorn

Joined: Nov 12, 2000
Posts: 21
I have a class which contains a method that performs file access. In this method I call the DriverManager.getConnection() method.
My problem is, I won't always be able to provide the username and password that getConnection() requires.
Is there a way to perform file access without having to do this?
Thanks in advance.
Kevin

Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Kevin,
I'm not sure I follow. DriverManager.getConnection() is use for JDBC. How is that connected with your file access?
Can you give me a small psuedo-code of what you need? I may be able to help you after I see your psuedo-code or if you can clarify your requirements.
Thanks,
-Peter
Kevin Adams
Greenhorn

Joined: Nov 12, 2000
Posts: 21
Originally posted by Peter Tran:
Kevin,
I'm not sure I follow. DriverManager.getConnection() is use for JDBC. How is that connected with your file access?
Can you give me a small psuedo-code of what you need? I may be able to help you after I see your psuedo-code or if you can clarify your requirements.
Thanks,
-Peter

Sure, here ya go...
Let's say I have a file that contains codes and their descriptions, like so:
AP ACCOUNTS PAYABLE
IC INVENTORY CONTROL
I want to connect to a database and retrieve 1 row from the file (by doing this: SELECT DESC FROM FILE WHERE CODE = "IC")
Usually what I would've done would be to do the following:

Since this piece of code will always be instantiated by other objects, which have already been authenticated, I don't want to have to give a username and password to DriverManager.getConnection().
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Hey Kevin,
Sorry it took me so long to get back to this post. I got pulled into some other discussions, and I also have to do real work. Unfortunately, my employer doesn't pay me to stay on JavaRanch all day. I wish I was independently wealthy so I can hang out here all day.
Back to your problem.
I would suggest trying one of following techniques.
1. Use connection pooling.
2. Use a singleton pattarn to wrap your Connection object.
-Peter
[This message has been edited by Peter Tran (edited January 05, 2001).]
Kevin Adams
Greenhorn

Joined: Nov 12, 2000
Posts: 21
Cool.
I'm not sure what all that means just yet, but I'll certainly take a look into it. Thanks, Peter, for all your help.
Kevin
Yoo-Jin Lee
Ranch Hand

Joined: Nov 01, 2000
Posts: 119
Hi,
A singleton pattern makes sure that there's only one instance of an object. There's a couple of ways to create one and a great book on design patterns can be found at:
James W. Cooper's: Java Design Patterns, A Tutorial at http://www.patterndepot.com/put/8/JavaPatterns.htm
Also, being able to read values from a file gives you several advantages, the most obvious is not having to recompile your code if you want to change any of the values.
Here's some code I made for a project:
<code>
import java.util.*;
import java.io.*;
public class STANDARDS {
static String path = "";
private static final String [] propKeys = {"SERVLET",
"DNS",
"FROM_NAME",
"FROM_EMAIL",
"SMTP_HOST",
"JDBC_DRIVER",
"JDBC_URL"};
private static String filepath = "";
private static HashMap ht = new HashMap(propKeys.length);
private STANDARDS () {
File f = null;
Properties options = new Properties();
File currentDir = new File(System.getProperty("user.dir"));
f = new File(currentDir,"prop.ini");
path = f.getAbsolutePath();
try {
options.load(new FileInputStream(f));
} catch (IOException e) {
System.out.println(e.toString());
}
for(int i = 0; i < propKeys.length; i++){
ht.put(propKeys[i],options.getProperty(propKeys[i]));
}
}
private static STANDARDS instance = new STANDARDS();
static public STANDARDS getInstance(){
if (instance == null) {
instance = new STANDARDS();
}
return instance;
}
static public String getSERVLET(){
return (String) ht.get("SERVLET");
}
static public String getDNS(){
return (String) ht.get("DNS");
}
static public String getHOME(){
return ((String) ht.get("DNS")) + ((String) ht.get("SERVLET"));
}
static public String getFROM_NAME(){
return (String) ht.get("FROM_NAME");
}
static public String getFROM_EMAIL(){
return (String) ht.get("FROM_EMAIL");
}
static public String getSMTP_HOST(){
return (String) ht.get("SMTP_HOST");
}
static public String getJDBC_DRIVER(){
return (String) ht.get("JDBC_DRIVER");
}
static public String getJDBC_URL(){
return (String) ht.get("JDBC_URL");
}
static public String getPath(){
return path;
}
static public void setFilePath(String path) {
filepath = path;
}
}
</code>
After this you would create a poolmanager, again using a singleton pattern (in the same way as STANDARDS) that would call this class (STANDARDS) with the appropriate method call. Now, everytime you want a connection then would get an instance of your poolmanager and call a getConnection method.
Hope this helps.
cheers,
Yoo-Jin.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Access object
 
Similar Threads
file accessed by two threads and two objects
How can I write a new record at the end of random access file of database?
accessing method in another file in another class
ImageIO and .exists can't find file.
Passing a value from a JSP page to an Action to get some data after processing