using files with java

N

Nuno Silva

Hi, if anyone can give me urls or tutorials on how to program in java with
files... I mean accessing to a file, saving some content in a file, reading
that content from a file I'd be greatfull.

I'm trying to make a program that will use files to store information
instead of store that stuff on data structures like hashtables, or tries, or
red black trees also...

So if anyone can help me with that I'd much appreciate it.

cheers

Nuno Silva
 
I

Inverno

Nuno Silva said:
Hi, if anyone can give me urls or tutorials on how to program in java with
files... I mean accessing to a file, saving some content in a file, reading
that content from a file I'd be greatfull.

Basically all you need is a File object and print and read Streams, check
the documentation on the various printStreams and readers and choose the one
that best fits you.
Then, to manipulate resulting strings you use StringTokenizer or
String.split(regex);
I'm trying to make a program that will use files to store information
instead of store that stuff on data structures like hashtables, or tries, or
red black trees also...

I really don't understand here.. what you mean? Once you read from a file
you have to put the data somewhere for you to use, i.e. a data structure of
some kind, shouldn't you?
And if you want to -store- data you certainly can't just leave it in a
hashtable or other data structures, as those will of course vanish once
thetanto program is terminated... do you mean something else when you
say -store-? As I really can't see what you mean.
 
N

nobody important

Inverno said:
Basically all you need is a File object and print and read Streams, check
the documentation on the various printStreams and readers and choose the one
that best fits you.
Then, to manipulate resulting strings you use StringTokenizer or
String.split(regex);
tries,

I really don't understand here.. what you mean? Once you read from a file
you have to put the data somewhere for you to use, i.e. a data structure of
some kind, shouldn't you?
And if you want to -store- data you certainly can't just leave it in a
hashtable or other data structures, as those will of course vanish once
thetanto program is terminated... do you mean something else when you
say -store-? As I really can't see what you mean.

Me neither - apart from having to store the data somewhere in RAM at some
point, you'll have to use a lot of files if you're trying to map 1 node in a
trie or one entry in a hashtable to a single file. A lot of files will pose
a problem like "Too many open files" (if you need them all to be accessed at
once). You may face a problem with the OS' max number of files.

Other than that - file-access is much slower than memory access.

Are you trying to build a database in Java?

If you just want object persistence, then I would suggest a Serializable
datastructure i RAM, which you'll store once in a while. Or perhaps use a
free database like mysql or postgresql.

Well the solution to your problem all depends on what you want to do. A few
properties to be stored/read - then use java.util.Properties (which is
actually a hashtable), a massive amount of data - as in a dictionary - then
I would suggest storing the words in a single file and combine this with a
trie. Use java.io.FileInputStream, InputStreamReader, and BufferedReader for
reading and PrintWriter, BufferedOutputStream, and FileOutputStream for
writing.

/nobody important
 
H

Hylander

nobody important said:
Me neither - apart from having to store the data somewhere in RAM at some
point, you'll have to use a lot of files if you're trying to map 1 node in a
trie or one entry in a hashtable to a single file. A lot of files will pose
a problem like "Too many open files" (if you need them all to be accessed at
once). You may face a problem with the OS' max number of files.

Other than that - file-access is much slower than memory access.

Are you trying to build a database in Java?

If you just want object persistence, then I would suggest a Serializable
datastructure i RAM, which you'll store once in a while. Or perhaps use a
free database like mysql or postgresql.

Well the solution to your problem all depends on what you want to do. A few
properties to be stored/read - then use java.util.Properties (which is
actually a hashtable), a massive amount of data - as in a dictionary - then
I would suggest storing the words in a single file and combine this with a
trie. Use java.io.FileInputStream, InputStreamReader, and BufferedReader for
reading and PrintWriter, BufferedOutputStream, and FileOutputStream for
writing.

File systems are slow compared to database FTMP since databases are
written to quickly access in a random/indexed manner. Esp in Windows
and there are a lot of limitations in directory size you don't get
with databases (also, performance goes out the door with directory
size). RandomAccess is best for a single large file (ie: delimited) so
when reading, you don't consume all the memory. If you use XML, there
are tools like castor and dom4j that make this easy. (might be old
now)

This is after a lot of experience with serialized structures.
Serialization is okay...commonly used in clustering,
replication/messaging etc. (but not good for large amounts of data as
it's very slow and often error prone(deserializing)...but once in
memory, it's fast of course since your data is then effectively
cached) (some frameworks built around this idea.ie: Prevayler at
codehaus.org).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top