Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.help > #1425 > unrolled thread
| Started by | "Thee Chicago Wolf (MVP)" <.@.> |
|---|---|
| First post | 2011-12-11 21:06 -0600 |
| Last post | 2011-12-12 13:04 -0500 |
| Articles | 19 — 7 participants |
Back to article view | Back to comp.lang.java.help
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
| From | "Thee Chicago Wolf (MVP)" <.@.> |
|---|---|
| Date | 2011-12-11 21:06 -0600 |
| Subject | Easy way to read the contants of a folder/directory and output contents to file? |
| Message-ID | <e5rae71q7jppj0ms8jn0sc0qp636gn3csj@4ax.com> |
HI all, Having a bit of trouble with the how-to of this. Say I want to read the contents of a folder and then output the contents of the folder to a file with each file name occupying its own line in the output file as is shown below: file2.txt doc1.doc hello.dat goobye.inf I'm guessing the gist is to store each filename as a String in an array and then to read each element and send it to the output file with an "\n" so it goes down a line every time it writes a filename to the output file, yes?
[toc] | [next] | [standalone]
| From | "John B. Matthews" <nospam@nospam.invalid> |
|---|---|
| Date | 2011-12-11 23:08 -0500 |
| Message-ID | <nospam-4A7111.23085411122011@news.aioe.org> |
| In reply to | #1425 |
In article <e5rae71q7jppj0ms8jn0sc0qp636gn3csj@4ax.com>, "Thee Chicago Wolf (MVP)" <.@.> wrote: > Having a bit of trouble with the how-to of this. Say I want to read > the contents of a folder and then output the contents of the folder > to a file with each file name occupying its own line in the output > file as is shown below: > > file2.txt > doc1.doc > hello.dat > goobye.inf > > I'm guessing the gist is to store each filename as a String in an > array and then to read each element and send it to the output file > with an "\n" so it goes down a line every time it writes a filename > to the output file, yes? Conveniently, the method listFiles() returns just such an array: <http://docs.oracle.com/javase/7/docs/api/java/io/File.html> You'll have to decide how you want to iterate through the array: <http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html> -- John B. Matthews trashgod at gmail dot com <http://sites.google.com/site/drjohnbmatthews>
[toc] | [prev] | [next] | [standalone]
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Date | 2011-12-12 08:42 -0600 |
| Message-ID | <lc4ce71958l9ddrl1ukvbi87e4j6l97ta4@4ax.com> |
| In reply to | #1426 |
>In article <e5rae71q7jppj0ms8jn0sc0qp636gn3csj@4ax.com>,
> "Thee Chicago Wolf (MVP)" <.@.> wrote:
>
>> Having a bit of trouble with the how-to of this. Say I want to read
>> the contents of a folder and then output the contents of the folder
>> to a file with each file name occupying its own line in the output
>> file as is shown below:
>>
>> file2.txt
>> doc1.doc
>> hello.dat
>> goobye.inf
>>
>> I'm guessing the gist is to store each filename as a String in an
>> array and then to read each element and send it to the output file
>> with an "\n" so it goes down a line every time it writes a filename
>> to the output file, yes?
>
>Conveniently, the method listFiles() returns just such an array:
>
><http://docs.oracle.com/javase/7/docs/api/java/io/File.html>
>
>You'll have to decide how you want to iterate through the array:
>
><http://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html>
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();
cheers.
- Thee Chicago Wolf [MVP]
[toc] | [prev] | [next] | [standalone]
| From | Nigel Wade <nmw-news@ion.le.ac.uk> |
|---|---|
| Date | 2011-12-12 14:49 +0000 |
| Message-ID | <9kmij4FsduU1@mid.individual.net> |
| In reply to | #1429 |
On 12/12/11 14:42, Thee Chicago Wolf [MVP] wrote: > outFile.write(String.valueOf(list[i]+"\n")); Do you something against the PrintWriter.println(String) method? -- Nigel Wade
[toc] | [prev] | [next] | [standalone]
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Date | 2011-12-12 09:12 -0600 |
| Message-ID | <s96ce79sq980me16uu6s37f89bt0jb602f@4ax.com> |
| In reply to | #1430 |
>On 12/12/11 14:42, Thee Chicago Wolf [MVP] wrote: > >> outFile.write(String.valueOf(list[i]+"\n")); > >Do you something against the PrintWriter.println(String) method? Well, in some code I'd written previous, I used .write instead of .println. When I actually tried .println, the output I got was as such: file1.txt <space> file2.txt <space> file3.txt <space> <space> Not so pretty right? when using .write, I get: file1.txt file2.txt file3.txt <space> Which is exactly what I want. I asked this of another person as well. I naturally though println would give me nice clean output as in my second example. But it didn't. When I used .write, it did. So, I'm sticking with .write. Do you have any idea why println would give me such spaced output? - Thee Chicago Wolf [MVP]
[toc] | [prev] | [next] | [standalone]
| From | Nigel Wade <nmw-news@ion.le.ac.uk> |
|---|---|
| Date | 2011-12-12 15:44 +0000 |
| Message-ID | <9kmlr3Fmm1U1@mid.individual.net> |
| In reply to | #1433 |
On 12/12/11 15:12, Thee Chicago Wolf [MVP] wrote: >> On 12/12/11 14:42, Thee Chicago Wolf [MVP] wrote: >> >>> outFile.write(String.valueOf(list[i]+"\n")); >> >> Do you something against the PrintWriter.println(String) method? > > Well, in some code I'd written previous, I used .write instead of > .println. When I actually tried .println, the output I got was as > such: > > file1.txt > <space> > file2.txt > <space> > file3.txt > <space> > <space> > > Not so pretty right? > when using .write, I get: > > file1.txt > file2.txt > file3.txt > <space> > > Which is exactly what I want. I asked this of another person as well. > I naturally though println would give me nice clean output as in my > second example. But it didn't. When I used .write, it did. So, I'm > sticking with .write. Do you have any idea why println would give me > such spaced output? If you append a "\n" to the filename that is what you'll get. println() prints a line of output with a line terminator. Hence there is no need to append a "\n". That saves both processing time (concatenating Strings is relatively slow) and unnecessary coding. -- Nigel Wade
[toc] | [prev] | [next] | [standalone]
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Date | 2011-12-12 09:51 -0600 |
| Message-ID | <5n8ce7pgpk3ebv71sgs10i8pie2vtlhdpa@4ax.com> |
| In reply to | #1436 |
>On 12/12/11 15:12, Thee Chicago Wolf [MVP] wrote: >>> On 12/12/11 14:42, Thee Chicago Wolf [MVP] wrote: >>> >>>> outFile.write(String.valueOf(list[i]+"\n")); >>> >>> Do you something against the PrintWriter.println(String) method? >> >> Well, in some code I'd written previous, I used .write instead of >> .println. When I actually tried .println, the output I got was as >> such: >> >> file1.txt >> <space> >> file2.txt >> <space> >> file3.txt >> <space> >> <space> >> >> Not so pretty right? > >> when using .write, I get: >> >> file1.txt >> file2.txt >> file3.txt >> <space> >> >> Which is exactly what I want. I asked this of another person as well. >> I naturally though println would give me nice clean output as in my >> second example. But it didn't. When I used .write, it did. So, I'm >> sticking with .write. Do you have any idea why println would give me >> such spaced output? > >If you append a "\n" to the filename that is what you'll get. println() >prints a line of output with a line terminator. Hence there is no need >to append a "\n". That saves both processing time (concatenating Strings >is relatively slow) and unnecessary coding. Got it and yes, it is now obvious to me. I'll fix it up. Thanks for that. - Thee Chicago Wolf [MVP]
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-12-12 10:03 -0500 |
| Message-ID | <jc5549$sj3$1@dont-email.me> |
| In reply to | #1429 |
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.
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Date | 2011-12-12 09:15 -0600 |
| Message-ID | <5f6ce7l4gdb41amj4bhlppuftosk95vq0n@4ax.com> |
| In reply to | #1431 |
>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]
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-12-12 10:45 -0500 |
| Message-ID | <jc57jk$bhe$1@dont-email.me> |
| In reply to | #1434 |
On 12/12/2011 10:15 AM, Thee Chicago Wolf [MVP] wrote:
> [...] 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?
Debugging code that I can't see is an uncertain business, but
I'll bet you did something like
outFile.println(filename + "\n");
instead of just
outFile.println(filename);
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Date | 2011-12-12 09:52 -0600 |
| Message-ID | <qo8ce7t7u30st1giu7dvos28l7i7mpipn3@4ax.com> |
| In reply to | #1437 |
On Mon, 12 Dec 2011 10:45:56 -0500, Eric Sosman <esosman@ieee-dot-org.invalid> wrote: >On 12/12/2011 10:15 AM, Thee Chicago Wolf [MVP] wrote: >> [...] 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? > > Debugging code that I can't see is an uncertain business, but >I'll bet you did something like > > outFile.println(filename + "\n"); > >instead of just > > outFile.println(filename); Yes, Nigel pointed it out to me. .println with adding the \n will make it work correctly. I'll fix it up. Thanks. - Thee Chicago Wolf [MVP]
[toc] | [prev] | [next] | [standalone]
| From | Roedy Green <see_website@mindprod.com.invalid> |
|---|---|
| Date | 2011-12-12 01:02 -0800 |
| Message-ID | <8jgbe7hqkm4jg2mc7ns2j1jgvnmj82sfie@4ax.com> |
| In reply to | #1425 |
On Sun, 11 Dec 2011 21:06:38 -0600, "Thee Chicago Wolf (MVP)" <.@.> wrote, quoted or indirectly quoted someone who said : >Having a bit of trouble with the how-to of this. Say I want to read >the contents of a folder and then output the contents of the folder to >a file with each file name occupying its own line in the output file >as is shown below: File.list gets you an array of Strings with unqualified filenames. see http://mindprod.com/jgloss/filesanddirectories.html then a for:each loop through the array lets you get at each String. see http://mindprod.com/jgloss/jcheat.html#LOOPS Then just output that String to a PrintWriter. See http://mindprod.com/applet/fileio.html for sample code. -- Roedy Green Canadian Mind Products http://mindprod.com For me, the appeal of computer programming is that even though I am quite a klutz, I can still produce something, in a sense perfect, because the computer gives me as many chances as I please to get it right.
[toc] | [prev] | [next] | [standalone]
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Date | 2011-12-12 09:19 -0600 |
| Message-ID | <pn6ce7tlo3qnhse1lnhs2k5mu4ae4dgjo3@4ax.com> |
| In reply to | #1428 |
>On Sun, 11 Dec 2011 21:06:38 -0600, "Thee Chicago Wolf (MVP)" <.@.>
>wrote, quoted or indirectly quoted someone who said :
>
>>Having a bit of trouble with the how-to of this. Say I want to read
>>the contents of a folder and then output the contents of the folder to
>>a file with each file name occupying its own line in the output file
>>as is shown below:
>
>File.list gets you an array of Strings with unqualified filenames.
>see http://mindprod.com/jgloss/filesanddirectories.html
>
>then a for:each loop through the array lets you get at each String.
>see http://mindprod.com/jgloss/jcheat.html#LOOPS
>
>Then just output that String to a PrintWriter.
>See http://mindprod.com/applet/fileio.html
>for sample code.
Thanks much for the links! I wound up doing what I needed with the
below code:
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();
Now before you ask why I used .write instead of .println, it's because
when I use .println I get:
file1.txt
<space>
file2.txt
<space>
file3.txt
<space>
<space>
When I use .write, I get:
file1.txt
file2.txt
file3.txt
<space>
I've asked a few others as to why this would be. Do you have any idea?
cheers.
- Thee Chicago Wolf [MVP]
[toc] | [prev] | [next] | [standalone]
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Date | 2011-12-12 09:08 -0600 |
| Message-ID | <hn4ce7t0gdu40rgrfaj9546mepb3kcnhm7@4ax.com> |
| In reply to | #1425 |
>"Thee Chicago Wolf (MVP)" <.@.> writes:
>>output the contents of the folder to a file
>
>public class Main
>{ public static void main( final java.lang.String[] args )
> throws java.lang.Throwable
> { final java.io.PrintWriter out = new java.io.PrintWriter( "out.txt" );
> for( final java.io.File file : new java.io.File( "." ).listFiles() )
> out.println( file.getName() ); out.close(); }}
I re-used some code I wrote a few months ago for reading from and
writing to a file and I wound up doing it this way.
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();
It was the for loop that I needed to add so it would add the file
names line by line and the "\n" to put each filename on a new line.
So now that I have a filelist.txt with each file listed on it's own
line, is it realistically somewhat the reverse of the output process
to read in each filename line by line? As I mentioned above, I did
write some code to read from a .txt file that only contained *one*
line and it was an integer. That wasn't too big a deal to work out.
But if I want to read each filename in the .txt file line by line like
if it were an MP3 playlist, is the gist basically to have some kind of
for loop or while loop to iterate through the file, read the filename
into some String variable, and have the condition in the loop stating
something like below?
Scanner in = new Scanner(new File("filelist.txt"));
String temp = in.nextLine();
while (in.nextLine(temp != null))
{
//some code and filereading stuff goes here
}
Thanks much for showing me an example above. What I actually
discovered when using outFile.println is that the file output would
look like:
file1.txt
<space>
file2.txt
<space>
file3.txt
<space>
<space>
but when using outFile.write, I got:
file1.txt
file2.txt
file3.txt
<space>
A lot cleaner. In my original code, I used outFile.write. Just for my
own knowledge, any idea why outFile.println versus outFIle.write would
make the output look different?
cheers!
- Thee Chicago Wolf [MVP]
[toc] | [prev] | [next] | [standalone]
| From | Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> |
|---|---|
| Date | 2011-12-12 18:24 +0200 |
| Message-ID | <m3obvdpxkz.fsf@despammed.com> |
| In reply to | #1432 |
"Thee Chicago Wolf [MVP]" <.@.> writes: > A lot cleaner. In my original code, I used outFile.write. Just for my > own knowledge, any idea why outFile.println versus outFIle.write would > make the output look different? Not before you actually show us your code with println calls. -- Jukka Lahtinen
[toc] | [prev] | [next] | [standalone]
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Date | 2011-12-12 10:54 -0600 |
| Message-ID | <k5cce75e2aqlfk5vmqvsll2jopa48hj3bc@4ax.com> |
| In reply to | #1440 |
On Mon, 12 Dec 2011 18:24:28 +0200, Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> wrote: >"Thee Chicago Wolf [MVP]" <.@.> writes: > >> A lot cleaner. In my original code, I used outFile.write. Just for my >> own knowledge, any idea why outFile.println versus outFIle.write would >> make the output look different? > >Not before you actually show us your code with println calls. A couple others did figure it out. When using .write, it's not adding the extra "\n" whereas with .println, the "\n" was adding an extra line so that was the issue. Realistically, these two statements work exactly the same: outFile.print(String.valueOf(list[i]+"\n")); outFile.write(String.valueOf(list[i]+"\n")); but this one gives me and extra line: outFile.println(String.valueOf(list[i]+"\n")); That was the issue, the "\n". It wasn't needed when used with .println. - Thee Chicago Wolf [MVP]
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-12-12 12:40 -0500 |
| Message-ID | <jc5ea2$pqr$1@dont-email.me> |
| In reply to | #1441 |
On 12/12/2011 11:54 AM, Thee Chicago Wolf [MVP] wrote:
>[...]
> Realistically, these two statements work exactly the same:
>
> outFile.print(String.valueOf(list[i]+"\n"));
> outFile.write(String.valueOf(list[i]+"\n"));
If you want to become a programmer, you'll need to develop
an eye for detail ...
Also, it's *still* silly to take String.valueOf(a_String).
Why not String.valueOf(String.valueOf(String.valueOf("Hello!")))?
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [next] | [standalone]
| From | "Thee Chicago Wolf [MVP]" <.@.> |
|---|---|
| Date | 2011-12-12 12:03 -0600 |
| Message-ID | <9jfce7h85it7g77fe56jhihjla9p9mdrf0@4ax.com> |
| In reply to | #1442 |
>On 12/12/2011 11:54 AM, Thee Chicago Wolf [MVP] wrote:
>>[...]
>> Realistically, these two statements work exactly the same:
>>
>> outFile.print(String.valueOf(list[i]+"\n"));
>> outFile.write(String.valueOf(list[i]+"\n"));
>
> If you want to become a programmer, you'll need to develop
>an eye for detail ...
>
> Also, it's *still* silly to take String.valueOf(a_String).
>Why not String.valueOf(String.valueOf(String.valueOf("Hello!")))?
Well, I have to crawl before I can walk, and walk before I can run. I
can see now that's not necessary to use the String.valueOf because I'm
writing a String so it's just redundant to add the method. But from my
original post, please recall that I stated I was re-using my old code.
What I did not mention was that in my original code I was writing an
integer--not a String as is in this case--to a file. So in that case I
needed String.valueOf to grab the integer and convert it to a String.
So on the surface, yes, it looked clumsy but only because it was
re-used code and I wasn't paying such close attention to that part. In
any case, thanks for pointing it out to me. It's the little stuff I
miss that will help me write better code as I learn (and am taught).
Thanks!
- Thee Chicago Wolf [MVP]
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@ieee-dot-org.invalid> |
|---|---|
| Date | 2011-12-12 13:04 -0500 |
| Message-ID | <jc5fmv$2ts$1@dont-email.me> |
| In reply to | #1442 |
On 12/12/2011 12:40 PM, Eric Sosman wrote:
> On 12/12/2011 11:54 AM, Thee Chicago Wolf [MVP] wrote:
>> [...]
>> Realistically, these two statements work exactly the same:
>>
>> outFile.print(String.valueOf(list[i]+"\n"));
>> outFile.write(String.valueOf(list[i]+"\n"));
>
> If you want to become a programmer, you'll need to develop
> an eye for detail ...
... sez me, whose eye for detail seems to have developed
cataracts. (Sigh.)
> Also, it's *still* silly to take String.valueOf(a_String).
> Why not String.valueOf(String.valueOf(String.valueOf("Hello!")))?
--
Eric Sosman
esosman@ieee-dot-org.invalid
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.help
csiph-web