Jump to content

Java help writing a directory to a file

So im trying to write an entire directory into a file to enable me to implement a search criteria technique. However im struggling to figure out how to implement this. My code atm lists the available drives and the asks for UI to select a drive its at this point i then want to take the UI and run that drive to a file. I can run through a directory but not without printing to console and i do not want to print the directory to the console 

Link to comment
https://linustechtips.com/topic/911409-java-help-writing-a-directory-to-a-file/
Share on other sites

Link to post
Share on other sites

You can make a list of type file, and you can use file walk  (or might be folder walk) to go from the selected folder and walk every single sub folder and sub file. Then you can differientiate between any by  *.isDirectory() boolean to discriminate if need by.

 

Edit: https://docs.oracle.com/javase/tutorial/essential/io/walk.html

 

Within that tutorial, just eliminate the print to console commands, and set a new file to the associated file location, and add to a list.

Link to post
Share on other sites

1 hour ago, Ryujin2003 said:

You can make a list of type file, and you can use file walk  (or might be folder walk) to go from the selected folder and walk every single sub folder and sub file. Then you can differientiate between any by  *.isDirectory() boolean to discriminate if need by.

 

Edit: https://docs.oracle.com/javase/tutorial/essential/io/walk.html

I can walk the file tree fine its printing it to a new file that i have the right code to create without it writing to the console

Link to post
Share on other sites

9 minutes ago, shea99 said:

I can walk the file tree fine its printing it to a new file that i have the right code to create without it writing to the console

So get rid of the System.out lines. That will remove the printing to the console. Then take the values, however you're doing it (String or File), and return that to a list to store all files and folders under the user selected object. System.out.println(*) is what is outputting to the console.

Link to post
Share on other sites

28 minutes ago, Ryujin2003 said:

So get rid of the System.out lines. That will remove the printing to the console. Then take the values, however you're doing it (String or File), and return that to a list to store all files and folders under the user selected object. System.out.println(*) is what is outputting to the console.

i dont operate any system.out lines in my code the walkin operator outputs the filetree anyway 

Link to post
Share on other sites

2 minutes ago, Ryujin2003 said:

Haha, it happens. Did that get rid of it for you?

Unfortunately not i had copied and pasted the declaration and changed the name as i need walkin later and removed the system.out completely yet it still prints and doesnt print to the file either 

Link to post
Share on other sites

Just now, shea99 said:

Unfortunately not i had copied and pasted the declaration and changed the name as i need walkin later and removed the system.out completely yet it still prints and doesnt print to the file either 

Fixed the print issue just need it to write to the document now which quite frankly im struggling with and dont have a clue how to 

 

Link to post
Share on other sites

55 minutes ago, shea99 said:

Fixed the print issue just need it to write to the document now which quite frankly im struggling with and dont have a clue how to 

 

So, where you were printing, take that value as a new file File file = new File(whatEverStringLocationYouHaveHere), then add that to an ArrayList or some other type of list object. That way you have a list of all files.

Link to post
Share on other sites

Also, not sure if your current loop will cause issues, but you have an If/else, with a void return statement. I'm not sure if java will take that recursive call as the void return, so you may need to use a Lambda function in that case. Something to double check on if you think you aren't getting everything back yet.

Link to post
Share on other sites

2 hours ago, Ryujin2003 said:

Also, not sure if your current loop will cause issues, but you have an If/else, with a void return statement. I'm not sure if java will take that recursive call as the void return, so you may need to use a Lambda function in that case. Something to double check on if you think you aren't getting everything back yet.

Yea i think your right because it now writes to the file but only prints the directory root and then stops 

Link to post
Share on other sites

4 minutes ago, shea99 said:

Yea i think your right because it now writes to the file but only prints the directory root and then stops 

I think that's a default action of PrintWriter. You may want to look into a different approach if this isn't going to work for you. I'm assuming you're doing CMD interface instead of GUI?

Link to post
Share on other sites

1 hour ago, Ryujin2003 said:

I think that's a default action of PrintWriter. You may want to look into a different approach if this isn't going to work for you. I'm assuming you're doing CMD interface instead of GUI?

its only extra implementation anyway so can be removed if necessary as its not required but would prefer it to work and yea cmd interface 

Link to post
Share on other sites

9 hours ago, shea99 said:

So im trying to write an entire directory into a file to enable me to implement a search criteria technique. However im struggling to figure out how to implement this. My code atm lists the available drives and the asks for UI to select a drive its at this point i then want to take the UI and run that drive to a file. I can run through a directory but not without printing to console and i do not want to print the directory to the console 

Use a GUI like javaFX if you don’t want terminal. Then use something like a directory chooser.

https://docs.oracle.com/javase/8/javafx/api/javafx/stage/DirectoryChooser.html

 This is so much more simpler in my opinion. 

Sudo make me a sandwich 

Link to post
Share on other sites

5 minutes ago, wasab said:

Use a GUI like javaFX if you don’t want terminal. Then use something like a directory chooser.

https://docs.oracle.com/javase/8/javafx/api/javafx/stage/DirectoryChooser.html

 This is so much more simpler in my opinion. 

it has to be command line as my spec says so so unfortunatley not an option

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×