aspose file tools*
The moose likes Beginning Java and the fly likes Writing Output To A File Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Writing Output To A File" Watch "Writing Output To A File" New topic
Author

Writing Output To A File

Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Hi Guys, Im new to Java and am looking for a solution to a printWriter issue im having.

So far my program works fine up to the point where I want to write its final output to a new file.
The program itself compiles with no errors, however, even when given a direct route to write the file to it still does not produce anything. Please can anyone help.

I've tried some debugging using system.out.println()'s but am not really having any success.
I'll attach the code below. thanks so much for your time.

regards S

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

So, what *does* happen? Without any input file information, it's impossible for us to know what might be going wrong.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

(I'm not sure I like that you do all the work in the Find constructor. I guess there's no technical reason not to, though.)
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Hi David,

basically the program successfully reads the file and stores all the instances of the string into an array list. The list is then successfully sorted and outputted.

The issue is that although I have given a direct path and filename for the program to write to, nothing is created. Originally i left it so that the program would create in the relative directory, , however when it did not appear I tried a direct path link to the relative directory. This however was also unsuccessfull.

I've also tried some debugging with print lines but am not having any success.
regards Nick
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Dunno--if there's no exception, I find it difficult to believe the file isn't being created.
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Thats my problem lol, your welcome to try my code and see for yourself.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

As I mentioned before, I have no input file. If I use your code for writing and a dummy List<String> (and a filename that actually exists on my system) it works fine.
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
maybe im a little confused, so do i need to create the txt file manually via notepad etc?
I thought that if given the write command my program would create and write to that file. or am i just over thinking things?
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
in my program the writer method code isn't being executed. although there are no exceptions I added a line of code to display a message to say whether the file has or has not been written. In my program no such message is displayed.

is there anything that could be preventing the code from executing? regards Nick
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
I've just looked at an example of writing to a file and have noticed that in my code
i dont have a file writer, surely i need this to create a file, i seem to have the appropriate library for it but do not have one in my writeResults method???
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Nick Rowe wrote:I've just looked at an example of writing to a file and have noticed that in my code
i dont have a file writer, surely i need this to create a file, i seem to have the appropriate library for it but do not have one in my writeResults method???


Actually the PrintWriter constructor that accepts a filename argument, which you've used:

PrintWriter writer = new PrintWriter( filename );

creates a FileWriter internally.


[Jess in Action][AskingGoodQuestions]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

PrintWriter accepts a filename; the Javadocs explain what happens.

If you believe the method isn't being executed, you'd have to explain why you think that.
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
I believe my printWriter method is not being executed for simply two reasons.
I have since added a line of code to print out a message informing the user that the data has been correctly written to the file, in conjunction with that I have a line of code that is outputted if an exception is thrown preventing the data from being written.

The program compiles and executes without any errors but does not write to the file.
either way some kind of message should be output from the method when running the program.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Without seeing the actual current code it's difficult to know if you are handling exceptions correctly, or what would happen under various circumstances. Have you tried simply creating a PrintWriter and trying to write to that exact location in a test program as a sanity check?
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
The only additional content i've added is before i close the writer in my try/catch scenario

System.err.println( "Your Data Has Been Written To The File Successfully" );
so i have one line that prints if it works and another if an eception is thrown.

I've tried creating a seperate print writer previously and it works, its really frustrating.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

It works for me. Even if it doesn't find any matches it still creates an empty file. Are you sure you're looking for the file in the right directory?

One general comment though: You're using javadoc to document your code which is great . But there's a lot more you can still do. For example, when documenting a method, you should also describe each method parameter and its return value.


Check out this page for more info on writing javadoc comments.


SCJP 6 || SCWCD 5
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Michael Angstadt wrote:when documenting a method, you should also describe each method parameter and its return value.

Only if doing so adds value: if these parameters were named better no comment would be necessary.
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
im simply drilling down the the relative directory i.e.

C:Documents and Settings\Kieren Mcdonald>
cd Desktop
cd Nick
cd Java
cd Test

then javac Find.java
java Find

Then in explorer im looking in C:\Documents and Settings\Kieren McDonald\Desktop\Nick\Java\Test
and there is no file there. I have also attempted the program leaving the route empty so that the file is created in the location (the same place) relative to the program. And nothing has happened. If the write was successful the output message would have appeared in the command window also informing me that the task had been completed.


I have no compilation errors and no file is being written, blank or otherwise.
If you run my program you'll see what I mean, im getting so frustrated with it :( lol


p.s. thanks for the tips on the documentation.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Nick Rowe wrote:If you run my program you'll see what I mean, im getting so frustrated with it

He did, and says it created an empty file (which is what I'd expect).

Have you tried writing somewhere else?
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
oh, apologies i thought that he simply took my write method and ran that somewhere and not my entire program.
Apologies il try it now. Am i right in referencing the location with double backslashes?

i.e C:\\Documents and Settings\\Kieren McDonald\\Desktop\\Nick\\Java\\Test\\filetest.txt
nope i've tried another location and it does not work.

Would deleting the class file and re-compiling the program do anything to assist me?
regards Nick
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Nick Rowe wrote:The only additional content i've added is before i close the writer in my try/catch scenario
System.err.println( "Your Data Has Been Written To The File Successfully" );
Nick Rowe wrote:If the write was successful the output message would have appeared in the command window also informing me that the task had been completed.


So, to clarify, your writeResults() method looks like:

And when you run your program, the line "Your Data Has Been Written To The File Successfully" does NOT get printed? Have you recompiled your class? What is the exact console output you are getting?
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Nick Rowe wrote:Am i right in referencing the location with double backslashes?

Either forward or backslashes works in Windows, but really, you should use File.separator so that your program is more cross-platform.
Nick Rowe wrote:Would deleting the class file and re-compiling the program do anything to assist me?

It wouldn't hurt. You may have forgotten to recompile after making a change to the source code.
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
Yep thats correct, thats what the method looks like.
Also I compile the program each time i make a change, I never forget.
The output gets as far as sorting and printing the array but does NOT display any kind of message.

Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 274

Nick Rowe wrote:I never forget.

Yeah, until you do.

It runs the writeResults() method no matter what (even if the input file is empty), so it has to print something.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Do me a favor: run your program using

java -cp . Find

(That's java space dash cp space dot space Find). The only possible explanation for what you're seeing is that you're not running the code you think you are, and this may help get to the bottom of things.
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
I've recompiled the program but when i try to run it using java -cp.Find all i get is Could not create the java virtual machine. I have two copies of the file one for testing and one as a back up what if i just deleted the class files and re-compile the test program?

would that do anything?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Nick Rowe wrote:I've recompiled the program but when i try to run it using java -cp.Find ...

Ernest Friedman-Hill wrote:... (That's java space dash cp space dot space Find)

What command exactly did you type in? The spaces between -cp, the dot and Find are important.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Nick Rowe
Ranch Hand

Joined: May 26, 2010
Posts: 88
ahhh ok, I now get the three occurances for the resources printed out and a message informing me the the data has been successfully written.

So pardon my stupidity but what the hell did i just do and why doesn't it work with the standard java Find command after compiling with javac??

This is a little weird in my eyes

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Nick Rowe wrote:So pardon my stupidity but what did i just do

We don't actually know.
and why doesn't it work with the standard java Find command after compiling with javac??

What's the "standard java Find" command? Does the "standard java Find" command include the correct classpath?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Hi Nick,

OK, cool! The "-cp" (classpath) flag tells Java where to look for class files; the "." means "the current directory." What this all means is that you have the CLASSPATH environment variable set, and it doesn't include "." to represent the current directory, so Java was not finding "Find.class" right there where you can see it. It must be finding a different Find.class somewhere else -- someplace listed on your CLASSPATH. Maybe an earlier version of this same program, or maybe some other experiment you or someone else did?

Anyway, read this page to learn more about CLASSPATH and also how to fix this situation: HowToSetTheClasspath . Good luck!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing Output To A File