-2

I need to make a database for book storing. So what I want to do Is basically make a file for each book which I have done.

Now I need to search books when I need them. In 2 functions I'll need to find the book and edit its information.

I have no idea how to get the name of a file and assign it to a scanner\bufferwriter\ect.

Can anyone help me?

David Yee
  • 3,285
  • 20
  • 41
  • Are you using a RDBMS or flat files? – David Yee Jun 03 '14 at 19:01
  • Just .txt files, I put them in C://"program_name" (don't have a name yet) – MySecAccCodeMast Jun 03 '14 at 19:01
  • Your question is too broad. However, you have clearly identified where the gap is in your knowledge, where you said *I have no idea how to get the name of a file and assign it to a scanner\bufferwriter\ect*, which is a great starting point. Split that up in to two concerns: getting the name of a file(s), and writing to / reading from text files. That should be where your research begins. Write some prototype programs to text what you find, and then put it all together and write the main program. Post back when you've encountered a question or problem that hasn't already been answered here. – Paul Richter Jun 03 '14 at 19:05
  • I know how to write to a file with a constand name. but what if I were to change thet name. I'd need to put it in a variable. the only problem so far is how to find the file with the name I'm looking for. Is there a statement that returns the filename. – MySecAccCodeMast Jun 03 '14 at 19:10

1 Answers1

0

You have two options:

  1. Put the names of the files into another, known filename (ie: BookList.txt) and then when your program opens you can parse through this file and get the other file names in the directory.

  2. You can get the file directory listing and store it into a list. For example:

    File f = new File("C:\\");
    ArrayList<String> names = new ArrayList<String>(Arrays.asList(f.list()));
    

Once you know the file names, you can open these files and make any necessary edits. Don't forget to also close your file readers and writers once you're done with them!

You can iterate through the array using a for loop. For example:

for(String i : names){ // iterate through each file name in the array
    Scanner fileScanner = new Scanner(i);

    ... // do some parsing

    fileScanner.close();
}
David Yee
  • 3,285
  • 20
  • 41