• Post Reply Bookmark Topic Watch Topic
  • New Topic

create a file in memory ?

 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi :

I need to create a file, and store its contents in memory.
Storing it as a byte[] in the jvm, however, is not an option because
I need other programs to be able to access it.

Is there a cross platform way of doing this ? If not I guess I will have
to use unix sockets (are there any other ways of accomplishing this without sockets) ?

Finally, if this is the case (I have to use a socket in UNIX), does anybody know how to create a unix socket from java ---- Im assuming Ill have to use runtime.exec() but Id rather do it using a more robust API if anything exists in the standard sun libraries....
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sockets are pretty simple and cross-platform in Java. See the Sun Socket Tutorial.

I'm interested in your requirements. Why does it have to be in memory? Why not a database? Think how much time database vendors have put into concurrent data access and imagine how much better you or I might do it.

If it really has to be in memory, have you thought about any other protocols? RMI if you're all Java on both ends?
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.

Im a researcher at the Uconn health center...
Im actually storing the files in a database, but I want to allow the user to be able to access the files easily and quickly (for view only) while the app is running, right after they are generated.
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Again : Okay, so i read the tutorial, but I have another qustion... once you create a socket, what is the (non java) "path" to it ? -j
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's no path. You're thinking of UNIX domain sockets, I see. Others are talking about platform-independent TCP/IP sockets; there's no path, only a host name and port number. Java has no API for UNIX domain sockets.
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does anyone know how to write a simple file to a socket using unix?
Every example online seems to be about unix network sockets...
Apologies that this is not a pure java question but its related to the topic, in the sense that i cant find a java way to write a file to memory and still access it in the unix environment, so Ill need the next best thing...
 
Martin Simons
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java, as far as I know, does not support unix domain sockets. The
following is a truely ugly, but workable hack/work-around as long as you
only wish to write and not read from the socket. It may be able to be
expnaded if you also need to read however.

Write a small program (in C, perl, or whatever) that opens a localhost
network socket and takes info from that and writes it to the domain
socket.

As I said, a truely ugly hack, but it would work. You would have to
figure out your own security for it though (authentication for connection
to the localhost network socket maybe).
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow !?

There must be some way to create an in memory file though right !? One that is basically the same as a disk file but that is managed in memory rather than disk ?

Is there a unix way to create a temporary file that I could access using runtime.exec() ?

Thanks so much ! jay
 
Michael Powe
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by JAY asasd:

There must be some way to create an in memory file though right !? One that is basically the same as a disk file but that is managed in memory rather than disk ?


you can use java.nio.*, the "new" i/o classes, to memory-map files.




Is there a unix way to create a temporary file that I could access using runtime.exec() ?


there are all kinds of ways to make "temporary" files. getting rid of them when you're done with them is usually more of an issue, judging by the way my /tmp directories fill up. ;-)

see



thanks.

mp
 
jay vas
Ranch Hand
Posts: 407
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The createTempFile method seems a little bit quirky - I wrote a program that uses it and it crashes on one machine, but works fine on another.
 
aslam parveez
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

From your requirement what i can understand is u need to host a shared memory kinda thing in which u can store the file contents and various clients can access it thereby allowing no disk (i/o ) involvment. Sockets is defnitely an option but apart Java offeres RMI which helps u in easily implementing Shared memory for JVMs.

Hope this helps.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!