Jump to content

Java help writing a directory to a file

shea99

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
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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, shea99 said:

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

Do you mind showing your code?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Ryujin2003 said:

Do you mind showing your code?

yea sure ill just add a few comments to help understand

Link to comment
Share on other sites

Link to post
Share on other sites

9 minutes ago, Ryujin2003 said:

Do you mind showing your code?

It will be changed in the end product for Windows use but im using mac ATM

Screen Shot 2018-03-31 at 15.51.19.png

Screen Shot 2018-03-31 at 15.54.38.png

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, shea99 said:

It will be changed in the end product for Windows use but im using mac ATM

Screen Shot 2018-03-31 at 15.51.19.png

Screen Shot 2018-03-31 at 15.54.38.png

Showing when ran how it ouputs to the console

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, Ryujin2003 said:

where do you instantiate your filewalker?

its the walkin on 27

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, shea99 said:

its the walkin on 27

Yes, but I want to see where you declare the walkin object

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, Ryujin2003 said:

Yes, but I want to see where you declare the walkin object

Ah i think i may have found the issue but ill show it anyway its here 

Screen Shot 2018-03-31 at 16.14.17.png

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, shea99 said:

Ah i think i may have found the issue but ill show it anyway its here 

Screen Shot 2018-03-31 at 16.14.17.png

Yeah, your System.out.println on line 88 could definitely be the culprit.

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Ryujin2003 said:

Yeah, your System.out.println on line 88 could definitely be the culprit.

Yea id completley ignored the fact that i even had that still i was so in the zone on the other class

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, shea99 said:

Yea id completley ignored the fact that i even had that still i was so in the zone on the other class

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

Link to comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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 comment
Share on other sites

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

×