I'm trying to find all the wavs in every folder that I have in a directory, for example, "Music". Right now, my code shows the wavs found in whatever folder I select, but I want it to list all the wavs found in the whole directory at once, without the user going from folder to folder. How can I do that? Here's my code:
FileChooser chooseFile = new FileChooser();
FileChooser.ExtensionFilter filter = new FileChooser.ExtensionFilter("Choose a file (*.wav)", "*.wav");
chooseFile.getExtensionFilters().add(filter);
File file = chooseFile.showOpenDialog(null);
directory = file.toURI().toString();
First you should use a DirectoryChooser to select a directory. You can use the NIO package to find the files:
DirectoryChooser chooser = new DirectoryChooser();
File directory = chooser.showDialog(null);
Path[] wavs = findWavs(directory);
private static Path[] findWavs(File directory) throws IOException {
Path dir = directory.toPath();
try (Stream<Path> stream = Files.find(dir, Integer.MAX_VALUE, (path, attributes) -> path.getFileName().toString().endsWith(".wav"))) {
return stream.toArray(Path[]::new);
}
}
If you need Files instead you could simply map the paths to files:
return stream.map(Path::toFile).toArray(File[]::new);
Related
I've added my file into resources with right click -> existing item.
Now I want to copy the added file into another directory like this:
File.Copy(#"I don't know", #"C:\Users\user-1\Desktop\", true);
I don't know what I have to write in #"I don't know" part to addressing the added resource file.
If myDir is the directory of your project, the resources' file has path myDir\Properties\Resources.resx.
Now, when you execute a program from Visual Studio in Debug mode, the folder is myDir\Bin\Debug.
You have to go up 2 folders and enter the Properties folder:
var resourcesFileName = "Resources.resx";
var currentDirectory = Directory.GetCurrentDirectory();
var actualResourceFilePath = Path.Combine(
currentDirectory, "..", "..", "Properties", resourcesFileName);
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
var newFilePath = Path.Combine(desktop, resourcesFileName);
File.Copy(actualResourceFilePath, newFilePath, true);
I suggest you to not use the path separator in the hard-coded way (#"dir1\dir2...").
Use Path.Combine instead.
I have simple wpf application where I am trying to move a file from one folder to other folder. My file is RTC.hex is on desktop. I am trying to move it to a folder in D drive. Code:
private void Move_ButtonClick(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
string filename = dlg.SafeFileName;
System.IO.File.Move(filename, #"D:\New Folder\" + filename);
}
}
but it shows following error:
What wrong I am doing here.?
Use the FileDialog's FileName property instead of SafeFileName, because it contains a full path:
string filename = dlg.FileName;
From the SafeFileName page on MSDN:
This value is the FileName with all path information removed.
You also have to create the target folder, before you can write to it.
var targetDir = #"D:\New Folder";
System.IO.Directory.CreateDirectory(targetDir);
System.IO.File.Move(filename,
System.IO.Path.Combine(targetDir, System.IO.Path.GetFileName(filename)));
Which value is on running time in your filename variable?
Their should be the path to your desktop folder (C:/Users//Desktop) + 'RTC.hex'.
(For Windows 7 and 8)
The desktop path could be different for older Windows OS.
I have a spring boot project that is deployed on Tomcat 6. I have a list of files under project's resource directory and I am trying to list all files under that directory. I am able to get specific file if I specify classpath:abc.txt in the getResource() method. However I am wondering if there is a way to just list all files with a wild card or something.
You cannot do like this MyClass.class.getResource('*.txt'). The alternative to this is :
ServletContext sc = request.getSession().getServletContext();
String strPath = sc.getRealPath("/");
File[] allFiles = new File(strpath).listFiles(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".txt");
}
});
Refer this :Using File.listFiles with FileNameExtensionFilter
I am working on an application that stores files under /WEB-INF/someFolder/. But I dont find the right way to create files under this folder. I did this, but it is not working:
File newFile = new File("/WEB-INF/fileName.xml");
When I try to check the creation:
boolean isCreated = newFile.createNewFile();
I get :
java.io.IOException: No such file or directory
Please help me doing it in the right way.
Update:
I did this workaround, it is working but I dont see that it is performant solution.
ServletContext servletContext = getServletContext();
String path = servletContext.getRealPath("/WEB-INF/");
File newFile2 = new File(path+"/fileName.xml");
Any ideas?
You shall use ServletContext.getRealPath(String) and build the entire classpath manually
String webInfPath = getServletConfig().getServletContext().getRealPath("WEB-INF");
OR go step by step:
ServletConfig scfg= getServletConfig();
ServletContext scxt = scfg.getServletContext();
String webInfPath = sxct.getRealPath("WEB-INF");
And than use the webInfPath to create a File object inside WEB-INF
File newFile = new File(webInfPath + "/fileName.xml");
make sure you applaction have the permissions to write.
you can get the path ues like this:
String path=Thread.currentThread().getContextClassLoader().getResource("com/youpackage/");
Now you get the path which is your class folder path,so you can get the WEB-INF path.
ps: i remember when create file you must writer some content,otherwies it may not create.
Hi I have a wpf application that plays sounds on events such as button click. And the current code I have now plays the sound file, but Ive realized that it has to be in another folder which for example if the user deletes, makes the application pretty much not useable.
I was wondering, How could I get a .wav file without creating a whole new folder in the application Release directory.
Any ideas?
Thanks.
If the .wav files is added to your project at the root then marked as content (Right Click on the .wav file and click properties. Then change the build action to content) then it will be copied directly to the root output folder
You can the reference the wav file in code or XAML as a relative path (simple "Myfile.wav")
I believe this is what you are asking for?
How about putting the .wav-file as en embedded resource in the dll?
Just add the file to the project, right-click on the file and set Build Action to Embedded Resource.
Then you can load the .wav file as a stream and save it to disc like this:
private void WriteEmbeddedResourceToFile(string writePath, string assemblyName)
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream s = asm.GetManifestResourceStream(assemblyName);
if (s != null)
{
var file = new byte[s.Length];
s.Read(file, 0, (int)s.Length);
File.WriteAllBytes(writePath, file);
s.Close();
}
}
The string assemblyName must be set like this: namespace.(if you put the file in a folder, insert folder subpath here).filename.wav
Alternatively, you can drop saving it to disc, and just use the stream directly:
private byte[] GetEmbeddedResource(string assemblyName)
{
Assembly asm = Assembly.GetExecutingAssembly();
Stream s = asm.GetManifestResourceStream(assemblyName);
if (s != null)
{
var file = new byte[s.Length];
s.Read(file, 0, (int)s.Length);
s.Close();
return file;
}
}
Then, when you want to load the file, GetEmbeddedResource will return the byte array.