I have a Java program that I am using to scan a directory to look for certain files. It finds the files but now I am trying to get the code to open the files once it finds them, but I am not sure how to do that.
Here a part of my code
File file = new File("/Users/******/Desktop/******");
String[] A = file.list();
File[] C = file.listFiles();
for (String string : A) {
if (string.endsWith(".txt")) {
System.out.println(string);
}
if (string.contains("******")) {
System.out.println("It contains X file");
}
}
I am trying to get it so once it finds the files ending in .txt, it opens all of them
I have tried using Google on how to solve his, I came across .getRuntime() and so I tried
try{
Runtime.getRuntime().exec("******.txt");
} catch(IOException e){
}
But I am not fully understanding how how this works. I am trying to get to so that once it finds the files it opens them. I am not trying to have the IDE open the text on the screen. I want the actual Notepad/TextEdit program to open.
File[] files = new File("/Users/******/Desktop/******").listFiles();
for (File f : files) {
String fileName = f.getName();
if (fileName.endsWith(".txt")) {
System.out.println(fileName);
}
if (fileName.contains("******")) {
System.out.println("It contains X file");
}
}
Related
I have a windows directory tree with about 1,000,000 files inside.
I have a text file that i read in Java, contains some file names (about 100,000), and I want to check for every file name - if it exists in the directory (if yes - give me a full path of the file).
Already tried those options:
1.
File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()) {
System.out.println("Directory " + listOfFiles[i].getName());
}
}
2.
public void func(String path, String name)
{
Path folder = Paths.get(path);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder))
{
for (Path entry : stream)
{
if(Files.isDirectory(entry))
{
func(entry.toString(), name);
}
else
{
if(FilenameUtils.removeExtension(entry.getFileName().toString()).equals(name))
{
System.out.println(entry);
}
}
}
}
catch (IOException ex) {
// An I/O problem has occurred
}
}
So far all those options to do so are very slow.
As I guess, although all the files are in the same logic place, actually every file is saved in another place in the hard drive, so all those IO calls take too much time.
Another idea that i found here is ISearchFolderItemFactory interface, but I found documentation for it only in C++, not in Java.
Maybe I can implement a pre-sort or something, to put all the files really together in the hd, sorted by name, and then to use some hash method to find name by name?
Need some help...
I'm a newcomer to mobile programming and want to start off with a little Musicplayer-App. I need to search for all files which end with .mp3, .m4a, .wav, and so on. To do this, I want to first get a list of all files and then filter out the audio-files.
However, when I use getExternalStorageDirectory to get the path to the storage it returns "/storage/3139-6333", which is obviously not correct.
Basically I'm just curious why I get this result and how to fix it.
It's to be assumed that READ_EXTERNAL_STORAGE is permitted.
Thanks in advance.
String state = Environment.getExternalStorageState();
if ((Environment.MEDIA_MOUNTED.equals(state)||Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)))
{
File sd_card = Environment.getExternalStorageDirectory(); //"/storage/3139-6333"
File[] listFile = sd_card .listFiles(); //null
}
I guess you already have the path where you wish to do file search, if yes then you can use basic dfs algorithm to search your desired file.
below is example code for that, you can put any format in end to check all files for it.
public void search(String path){
File file = new File(path);
File[] files = file.listFiles();
if (files!=null) {
for (File f : files) {
if (f.isDirectory()) {
this.search(f.getAbsolutePath());
}
if (f.getAbsolutePath().endsWith(".txt")) {
System.out.println(f.getAbsolutePath());
}
}
}
}
hope this will help.
I have a file which is needed for running tests - this file needs to be personalized (name and password) by whomever is running the test. I do not want to store this file in Eclipse (since it would need to be changed by whomever runs the test; also it would be storing personal info in the repo), so I have it in my home folder (/home/conrad/ssl.properties). How can I point my program to this file?
I've tried:
InputStream sslConfigStream = MyClass.class
.getClassLoader()
.getResourceAsStream("/home/" + name + "/ssl.properties");
I've also tried:
MyClass.class.getClassLoader();
InputStream sslConfigStream = ClassLoader
.getSystemResourceAsStream("/home/" + name + "/ssl.properties");
Both of these give me a RuntimeException because the sslConfigStream is null. Any help is appreciated!
Use a FileInputStream to read data from a file. The constructor takes a string path (or a File object, which encapsulates string path).
Note 1: A "resource" is a file which is in the classpath (alongside your java/class files). Since you don't want to store your file as a resource because you don't want it in your repo, ClassLoader.getSystemResourceAsStream() is not what you want.
Note 2: You should use a cross-platform way of getting a file in a home directory, as follows:
File homeDir = new File(System.getProperty("user.home"));
File propertiesFile = new File(homeDir, "ssl.properties");
InputStream sslConfigStream = new FileInputStream("/home/" + name + "/ssl.properties")
You can simplify your work, using Java's 7 method:
public static void main(String[] args) {
String fileName = "/path/to/your/file/ssl.properties";
try {
List<String> lines = Files.readAllLines(Paths.get(fileName),
Charset.defaultCharset());
for (String line : lines) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
You can also improve your way of reading properties file, using Properties class and forget about reading and parsing your .properties file:
http://www.mkyong.com/java/java-properties-file-examples/
Is this a graphics program (ie. using the Swing library)? If so it is a pretty simple task of using a JFileChooser.
http://docs.oracle.com/javase/6/docs/api/javax/swing/JFileChooser.html
JFileChooser f = new JFileChooser();
int rval = f.showOpenDialog(this);
if (rval == JFileChooser.APPROVE_OPTION) {
// Do something with file called f
}
You can also use Scanner to read the file.
String fileContent = "";
try {
Scanner scan = new Scanner(
new File( System.getProperty("user.home")+"/ssl.properties" ));
while(scan.hasNextLine()) {
fileContent += scan.nextLine();
}
scan.close();
} catch(FileNotFoundException e) {
}
I am currently working on a project for school, it is Java based and I am using Eclipse on Linux Mint to write it. The assignment says use the statement String[] filenames = new java.io.File("icons).list(); to create an array of file names.
The problem is I am not sure what to do with this, I have spent the past few hours searching the Internet and my textbook, but to no avail. Does it need to be a separate method?
Below is my guess for the needed code in the model (the project is to make a matching game, with a GUI) the names will have to be converted later on into actual icons, but I am pretty sure I have that part figured out, I just can't seem to get the darn files into the array!!
Thanks in advance,
public String[] list() {
String[] fileNames = new java.io.File("icons").list();
return fileNames;
}
In Java, the File class does not necessary represent an "existing" file on the file system. For example:
File f = new File("some_unknown_unexisting_file.bob");
System.out.println(f.exists()); // most likely will print 'false'
Also, the class resolves the file from the current working directory. You may get this directory with
System.out.println(new File(".").getAbsolutePath());
In your case, if you can, I would suggest getting a File[] array with :
File[] files = new File("icons").listFiles(new FileFilter() {
#Override
public boolean accept(File f) {
return !f.isDirectory() && f.canRead();
}
});
for (File f : files) {
System.out.println(f.getAbsolutePath());
}
which will return an array of File objects which are not folders and that you can open for reading (note that this is not always true, but is just fine in your case).
But if you have to use list(), then this is equivalent :
File parent = new File("icons");
String[] fileStr = parent.list(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
File f = new File(dir, name);
return !f.isDirectory() && f.canRead();
}
});
for (String f : fileStr) {
System.out.println(new File(parent, f).getAbsolutePath());
}
Also, with your list of files (String[]), you can create an icon using :
String filename = fileStr[i]; // some file name within the array
ImageIcon icon = new ImageIcon("icons" + File.separator + filename);
or with your list of files (File[]), it is cleaner :
File file = files[i]; // some file within the File[] array
ImageIcon icon = new ImageIcon(file.getAbsolutePath());
Good luck.
The code you wrote looks okay. Are you sure the folder "icons" exists where Java is looking?
Try this:
File f = new File("icons");
System.out.println("Does icons exist?" + f.exists());
System.out.println("Is it a dir?" + f.isDirectory());
System.out.println("How many files does it contain?" + f.list().length);
Good luck!
I've had the same problem. When I tried moving the icons folder into the folder just before the src folder, it seems to work. Not sure what I will do when I submit the assignment, as for it to work in JCreator, I believe it has to be with the .java files.
I have a java program in a certain directory. I would like to make it find a list of all files that end with .dat or .DAT in the same directory as the program. Then I need to look at the first couple of lines of each file and parse a title string. The program should return an array of the file names, and an array of the first three lines of each file. I am pretty new to IO stuff, can anyone help?
Example: C:/my_directory/
Files: Program.class,
FOO.DAT (starts with "Lorem\nipsem\n$$0"),
bar.dat (starts with "ipsem\nLorem\n$$0")
Return: ["FOO.DAT","bar.dat"] and ["Lorem\nipsem\n","ipsem\nLorem\n"]
Thanks in advanced!
The following code snippet prints out all the files that have .dat or .DAT file extensions in a given directory. Try to figure out how you can read first three lines from a file. That must be fairly straightforward
File tmp = new File( "/tmp");
List<String> fileList = new ArrayList<String>();
if( tmp.isDirectory())
{
for( File f : tmp.listFiles())
{
if( f.isFile() )
{
if((f.getName().endsWith( ".dat") || f.getName().endsWith(".DAT")))
{
fileList.add( f.getName() );
}
}
}
for( String fileName : fileList)
{
System.out.println( fileName);
}
}