Question:
java question updated?
2007-07-29 04:08:18 UTC
I have to create a database but not by using a JDBC by using textPad. It is going to be command line.I would like to know if i can store data,read,delete,search by using RandomAccess onto dat or txt files. for example: i have to delete an item by using the seek() then deleting it.

This database is like an inventory database storing materials like life-jackets and wakeboards for example. So i created a main class, then sub-classes. I have created my classes which use inheritance.

Secondly, my program has a "address database" in it, which has the name, phone numbers and address of contacts.. Can i use a linked list to store data here?
Four answers:
oldguy
2007-07-29 04:31:27 UTC
For a quick and dirty solution look at standard classes:

java.io.ObjectOutputStream and java.io.ObjectInputStream



Store your database in a object of a class you create that defines/contains the entire dataset. Use the writeObject method in java.io.ObjectOutputStream to write the whole thing before shutting down and the method readObject in java.io.ObjectInputStream to read the whole thing back in when you next start up.



The encompasing class can include any number of objects of MOST types (they must "implements java.io.Serializable) including a LinkedList.



If this is for a class, the instructor will probably NOT accept it.
AnalProgrammer
2007-07-29 04:33:19 UTC
Can I start by trying to get things straight in my head. You want a database but you do not have a database. You have a textPad file. This to me is definately not a database but is a sequential file.



Java has a method for random access files, see link.

If you have a java program that takes your text input and writes it to a new random access file (This is not a database but a file) then you will now be able to perform all your random access, update and delete actions on the new file.



I hope this helps.
angel04
2007-07-29 05:40:42 UTC
//Creating txt file



import java.io.*;



public class CreateFile{



public static void main(String[] args) throws IOException{

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Please enter the file name to create : ");

String file_name = in.readLine();

File file = new File(file_name);

boolean exist = file.createNewFile();

if (!exist)

{

System.out.println("File already exists.");

System.exit(0);

}

else

{

FileWriter fstream = new FileWriter(file_name);

BufferedWriter out = new BufferedWriter(fstream);

out.write(in.readLine());

out.close();

System.out.println("File created successfully.");

}

}

}



// Writting to a file



import java.io.*;



class FileWrite

{

public static void main(String args[])

{

try{

// Create file

FileWriter fstream = new FileWriter("out.txt");

BufferedWriter out = new BufferedWriter(fstream);

out.write("Hello Java");

//Close the output stream

out.close();

}catch (Exception e){//Catch exception if any

System.err.println("Error: " + e.getMessage());

}

}

}





Random Access file



import java.io.*;



public class RandAccessFile{

public static void main(String[] args) throws IOException{

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

System.out.print("Enter File name : ");

String str = in.readLine();

File file = new File(str);

if(!file.exists())

{

System.out.println("File does not exist.");

System.exit(0);

}

try{

//Open the file for both reading and writing

RandomAccessFile rand = new RandomAccessFile(file,"rw");

char ch = rand.readChar(); //Read a character

rand.seek(file.length()); //Seek to end of file

rand.writeChars("Namaste India,"); //Write end of file

rand.close();

System.out.println("Successfully");

}

catch(IOException e)

{

System.out.println(e.getMessage());

}

}

}





This are few program mes which show creating writting and using random file.



For more detail refer java.io package
martinmm
2007-07-29 04:22:00 UTC
a star.not that it matters.or that i know any thing much of what you said. but i listen .i think you can add the link after its complete.

this is just discussion,sorry i have no facts .but i would like to know .

i have a weak data base my self.lol


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...