Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1434
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Newsgroups | comp.lang.java.help |
| Subject | Re: Easy way to read the contants of a folder/directory and output contents to file? |
| Date | 2011-12-12 09:15 -0600 |
| Organization | . |
| Message-ID | <5f6ce7l4gdb41amj4bhlppuftosk95vq0n@4ax.com> (permalink) |
| References | <e5rae71q7jppj0ms8jn0sc0qp636gn3csj@4ax.com> <nospam-4A7111.23085411122011@news.aioe.org> <lc4ce71958l9ddrl1ukvbi87e4j6l97ta4@4ax.com> <jc5549$sj3$1@dont-email.me> |
>On 12/12/2011 9:42 AM, Thee Chicago Wolf [MVP] wrote:
>>
>> Iterating through the array wasn't too hard with listFiles (thanks for
>> that BTW) but I wound up using the following to accomplish what I
>> needed to get output to the file to work. It was just a simple matter
>> of the for loop
>>
>> FileWriter folderOutput = new FileWriter("filelist.txt");
>> PrintWriter outFile = new PrintWriter(folderOutput);
>> for(int i = 0; i< list.length; i++)
>> {
>> outFile.write(String.valueOf(list[i]+"\n"));
>> }
>> folderOutput.close();
>
> This looks like it might work, but it also might not. I'd
>suggest using outFile.close() instead of folderOutput.close() to
>avoid the possibility that the PrintWriter might be holding on
>to a few characters that have not yet been flushed. It might be
>best to get rid of the `folderOutput' name altogether, just to
>make this particular mistake harder to make.
>
> There are also a few simplifications available. First, there's
>an easier way to iterate through an array (rather oddly named `list').
>Second, there's no need to use String.valueOf() on a String argument.
>Finally, you could use PrintWriter's own methods to deal with line
>termination instead of worrying about it yourself.
>
> Putting it all together yields
>
> PrintWriter outFile = new PrintWriter(
> new FileWriter("filelist.txt"));
> for (File f : list) {
> outFile.println(f);
> }
> outFile.close();
>
> "Production grade" code would probably use outFile.checkError()
>to detect any I/O errors that the PrintWriter may have encountered.
Thanks for tips. I'm still learning Java so super-tweaking the code as
you've shown above is not something I'm adept at doing just yet but in
looking at it, I see how it works and it does make sense. But as I've
asked other, using outFile.println yields me output in my file that
looks like so:
file1.txt
<space>
file2.txt
<space>
file3.txt
<space>
<space>
When I use outFile.write, I get:
file1.txt
file2.txt
file3.txt
<space>
I'm obviously going to stick with the cleaner looking output than the
one with extra spaces in it. But do you know why it's doing the extra
spacing with println? Realistically, it *should* just like the output
for what .write does. Any idea?
- Thee Chicago Wolf [MVP]
Back to comp.lang.java.help | Previous | Next — Previous in thread | Next in thread | Find similar
Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf (MVP)" <.@.> - 2011-12-11 21:06 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? "John B. Matthews" <nospam@nospam.invalid> - 2011-12-11 23:08 -0500
Re: Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf [MVP]" <.@.> - 2011-12-12 08:42 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-12-12 14:49 +0000
Re: Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf [MVP]" <.@.> - 2011-12-12 09:12 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? Nigel Wade <nmw-news@ion.le.ac.uk> - 2011-12-12 15:44 +0000
Re: Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf [MVP]" <.@.> - 2011-12-12 09:51 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-12 10:03 -0500
Re: Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf [MVP]" <.@.> - 2011-12-12 09:15 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-12 10:45 -0500
Re: Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf [MVP]" <.@.> - 2011-12-12 09:52 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? Roedy Green <see_website@mindprod.com.invalid> - 2011-12-12 01:02 -0800
Re: Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf [MVP]" <.@.> - 2011-12-12 09:19 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf [MVP]" <.@.> - 2011-12-12 09:08 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2011-12-12 18:24 +0200
Re: Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf [MVP]" <.@.> - 2011-12-12 10:54 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-12 12:40 -0500
Re: Easy way to read the contants of a folder/directory and output contents to file? "Thee Chicago Wolf [MVP]" <.@.> - 2011-12-12 12:03 -0600
Re: Easy way to read the contants of a folder/directory and output contents to file? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-12-12 13:04 -0500
csiph-web