Jump to content

Java help writing a directory to a file

shea99
1 minute ago, shea99 said:

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

Oh. You did not mention it though. I just read that you do not wish to print to console part.

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, wasab said:

Oh. You did not mention it though. I just read that you do not wish to print to console part.

Yea i didnt state that was my bad sorry mate 

Link to comment
Share on other sites

Link to post
Share on other sites

Your args are the texts to write to the files correct?

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, wasab said:

Your args are the texts to write to the files correct?

yea 

Link to comment
Share on other sites

Link to post
Share on other sites

Must be one of the system.out.println statement. One of your function call was returning a string value containing the directory path and being fed to the console.

 

i can probably examine more closely if I have my ide with me.

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, wasab said:

Must be one of the system.out.println statement. One of your function call was returning a string value containing the directory path and being fed to the console.

 

i can probably examine more closely if I have my ide with me.

My only problem atm is getting it to write the whole directory to the file now ive successfully removed it from console and got it to write to the file instead however it only does it once and then doesnt write another file in the directory

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, shea99 said:

My only problem atm is getting it to write the whole directory to the file now ive successfully removed it from console and got it to write to the file instead however it only does it once and then doesnt write another file in the directory

I am not sure what you mean by writing whole directory to a file. :/ 

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, wasab said:

I am not sure what you mean by writing whole directory to a file. :/ 

So say if i have a memory stick using my walkin method it outputs every files path to the console. I want to be able to do the same thing but instead of to the console i want it to write to a file. 

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, shea99 said:

So say if i have a memory stick using my walkin method it outputs every files path to the console. I want to be able to do the same thing but instead of to the console i want it to write to a file. 

Well, in that case, you have to use recursion. Enhance for loop won’t help here.

 

oh, never mind. Didn’t see your walk-in method. I must’ve  missed it

Sudo make me a sandwich 

Link to comment
Share on other sites

Link to post
Share on other sites

If you put your code in a code tag, people can try it themselves which makes it much easier to analyze.

 

Have you tried adding a string variable to the main method (say the line under "File[] drives = File.listRoots();")

File[] drives = File.listRoots();
String filesInDirectory = ""; // hold the results of the search

and then every time you output the file to the console (line 88), also append it to that variable:

{
	if (listFile[i].getName().endsWith(pattern){
		System.out.println(listFile[i].getPath());
		filesInDirectory += listFile[i].getPath(); // store the result
	}
}

Then after the recursive "walkin" method completes, you could save the results to your file.

Web Developer and Java contractor

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

×