Java filename searching - java

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?

You have two options:
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.
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();
}

Related

In Java, how do I find the proper path to open a txt file that's name is provided as a command line arguement?

I'm working on a program that is supposed to take two files as command line arguments, open the files, and read data from the files to make a data structure.
So far, I have been able to make the structure using File() to open the files and Scanner to read the data. The problem is that I have been providing a specific path to the call for File like this
File f1 = new File("F:/MinSpan/resources/cities.txt");
Scanner sc1 = new Scanner(f1);
I don't think this is going to work for the person who tries to run this program, because I have provided the path for where my specific txt files are located - they're on my flash drive (F) and in some folders. Is there a way I can program this to pass some kind of args[] value in for File() based on the cmd arguement the user has provided?
I have already tried just doing new File(args[2]) , and it can't find the file because there is no path.
The reason for that is because, if you are passing in only two paths, args[2] wont return anything, because args[] starts at 0. So you'd want to use:
new File(args[0]);
new File(args[1]);
Does that make sense?
If you're going for something like java -jar program.jar FILE, then have the program check for the String in args[] at index 0.
Then, construct your file. Check if the file exists (in java.io, it's File.exists()) and return an error message to the user if it's wrong.

How to start reading lines from text from beginning again?

So i have a scanner that reads through a text file of many lines using while(file.hasNext()), however after it reaches the end of the text file how do I make it so that I can start reading lines from the beginning again for a separate while loop?
If you want to read multiple files (example, "text1.txt, text2.txt, text3.txt..etc"), this is what you can do:
Implement your file reading within a method such as:
public void readFile(String filename){
//all the code for file reading goes here
}
String[] filesToRead = new String[]{"text1.txt", "text2.txt", "text3.txt"};
for(int x=0; x<filesToRead.length; x++) //iterate through the file names
readFile(filesToRead[x]); //repeatedly invoked to read various files
Certianly, you may also save the file names within another text file, and just read from there. This way you don't even have to recompile your program when you want to change the list of files to be read.
Example:
FilesToRead.txt
text1.txt
text2.txt
text3.txt

read files names from directory without System files(.DS_Store)

Im trying to read all filenames from a folder and save them in an array but
im still reading all files (including the system folder .DS_Storefrom)so that my code doesn´t work for what i want to do. anyone knows how to read only non Systems files/folder?
Thanks for the help!
Here´s what i wrote,
String pathLevel= "/Users/lan/Desktop/top/";
File file = new File(pathLevel);
String [] levelNames = file.list();
String [] matrix= new String[levelNames.length];

list of file names sort to a specific order

My java code lists all code files under a directory of file system, and load each file one by one:
File[] files = mDir.listFiles();
for(File f: files) {
System.out.println(f.getPath());
//load code file
System.load(f);
}
The above code logically looks good, but is not suitable for my case.
My case is that I can NOT load them in a loop one by one, because there are dependencies among those code files. I need to load the files in a specific order according to dependencies.
Say, I already know there are following files under the directory mDir which should be load in the following order:
["dFile", "xFile", "aFile", "hFile"]
and I already got the directory instance mDir .
How can I load files with above order efficiently in java?
If you already know which files you are interested in then just load them in the proper order.
If you have to see which files are available first and then load them in the specific order, then use one loop to get the names of the existing files, then process the list by picking the correct files in the correct order.
I'd suggest just setting the working directory correctly (see Changing the current working directory in Java?) and then doing
for(String fname : fileArray) {
System.load(new File(fname));
}
(where fileArray is the list of file names) or
for(String fname : fileArray) {
System.load(new File(mDir.getPath() + fname));
}
if you're intent on loading from a specific directory.
Other than that, you'd need to divine the dependencies from each file in order, or read the list of files to load from some other source (an array, another file, whatever).

How to read from a file that has no extension in Java?

So basically say i have a file that is simply called settings, however it has no extension, but contains the data of a text file renamed.
How can i load this into the file() method in java?
simply using the directory and file seems to make java think its just a directory and not a file.
Thanks
In Java, and on unix, and even on the filesystem level on windows, there is no difference in if a file has an extension or not.
Just the Windows Explorer, and maybe its pendants on Linux, use the extension to show an appropriate icon for the file, and to choose the application to start the file with, if it is selected with a double click or in similar ways.
In the filesystem there are only typed nodes, and there can be file nodes like "peter" and "peter.txt", and there can be folder nodes named "peter" and "peter.txt".
So, to conclude, in Java there is really no difference in file handling regarding the extension.
new File("settings") should work fine. Java does not treat files with or without extension differently.
Java doesn't understand file extensions and doesn't treat a file any differently based on its extension, or lack of extension. If Java thinks a File is a directory, then it is a directory. I suspect this is not what is happening. Can you try?
File file = new File(filename);
System.out.println('\'' + filename + "'.isDirectory() is "+file.isDirectory());
System.out.println('\'' +filename + "'.isFile() is "+file.isFile());
BTW: On Unix, a file file. is different to file which is different to FILE. AFAIK on Windows/MS-DOS they are treated as the same.
The extension should not make a difference. Can you post us the code you are using? And the error message please (stack trace).
Something along these lines should do the trick (taken from http://www.kodejava.org/examples/241.html)
//
// Create an instance of File for data file.
//
File file = new File("data");
try {
//
// Create a new Scanner object which will read the data
// from the file passed in. To check if there are more
// line to read from it we check by calling the
// scanner.hasNextLine() method. We then read line one
// by one till all line is read.
//
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}

Categories