• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with writing to a file!  RSS feed

 
Bob Sanders
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For whatever reason my printwriter does not write to the file that is created, can someone help me?



I made a test program that could write to a file is already named, so I'm assuming there's some sort of problem with how this asks for a file name to create it to.
 
Bob Sanders
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need the answer to this by tomorrow can someone please help me!
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"testing", please check your private messages regarding an important administrative matter.

Thank you.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After you've dealt with Paul's issue, could also please read this and this.
 
Bob Sanders
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fixed, sorry I didn't expect my name to show up. Can someone look at my original post though?
 
Itzhok Alhasawi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on the same thing and was having the same problem. I couldn't see the output file after the program ran in the Eclipse browser. Mine was outputting to my Eclipse file folder where I have the program saved. I have my program files for Eclipse in MyDocs so it was saving the created txt or html file to ... C:\Documents and Settings\*******\My Documents\Downloads\eclipse\workspace\Lab8
My guess is that you can't see them in the Eclipse browser because the only files that will show up in there are the ones you have imported and the newly created file won't be imported. Check your program files folder where you run Eclipse from.
Hope that helps.
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Sanders wrote:Fixed, sorry I didn't expect my name to show up. Can someone look at my original post though?

Thanks Bob.

I can't tell from your code what file it is supposed to be writing to. And neither can you, because it depends on input from the user. So how about sticking in a line of debugging code which tells you what it is? You might be surprised.

And like Itzhok suggests, it helps if you know what your current working directory is when you run the code.
 
Bob Sanders
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you refresh the project, the file will appear, so that's not my problem. I can create it, just nothing writes to it.

To reiterate: It's not a problem with finding or creating the file, its with writing to it once its already created.
 
Itzhok Alhasawi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrintWriter output0 = new PrintWriter(new FileWriter(output2));

is that line the culprit? shouldn't it just be PrintWriter output0 = new PrintWriter(outputFileName);


the code for mine begins....


import java.util.Scanner;
import java.io.*;

public class Lab8
{
public static void main(String[] args) throws IOException
{
//gather input and output file names from user
//and prepare input file scanner and printwriter

Scanner kbd = new Scanner(System.in);
System.out.println("Name of input file: ");
String inFileName = kbd.nextLine();
File inputFile = new File(inFileName);
Scanner input = new Scanner(inputFile);
System.out.println("Name of output file: ");
String outFileName = kbd.nextLine();
PrintWriter output = new PrintWriter(outFileName);


and with that, I am able to go about the rest of the lab, writing to the output file with...

output.print("print whatever");
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Sanders wrote:To reiterate: It's not a problem with finding or creating the file, its with writing to it once its already created.

Suit yourself then. From my point of view I can tell that your code writes to the file, unless an exception is thrown. I just can't tell what file it writes to or where it is, so that's where I would start looking.
 
Itzhok Alhasawi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I put your code in Eclipse and see what you are saying. It's creating a file as it's supposed to, but none of the output material he has ....



...is being written to the created file.

I tried changing it to output without HTML, just straight text....



.....Still nothing.
Not sure if it has anything to do with it or not, but the code is also not terminating after it runs through?
 
Bob Sanders
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean as in terminating? As in its not being recognized?

And yeah thats as far as I can determine with whats happening, thanks for explaining it.
 
Itzhok Alhasawi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are using Eclipse, at the top of the console, while the app you've written is running, there will be a red square which you can hit to "terminate" or stop your app. Normally, you don't need to do this, as your app should terminate once it has run its course. Something in your code is messed up, though, and causing it to never terminate. I will keep messing with it and let you know if I can figure it out. Sorry I can't be more help. I'm a nub too lol
 
Bob Sanders
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay I've come to the conclusion that if I exclude the for loop it'll print out everything else, so for some reason the for loop is causing it not to work. Any ideas?
 
Itzhok Alhasawi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here's what I got so far. I seem to have solved your problem of it not writing anything to the created file, but I am having trouble with my fitness quotient and also, it's adding that last comma to the end of the heart rate results list...

 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Sanders wrote:Okay I've come to the conclusion that if I exclude the for loop it'll print out everything else, so for some reason the for loop is causing it not to work. Any ideas?

So: printing is working okay sometimes. No problem creating or finding the output file then. But if you broke it by inserting some more printing code, that suggests to me that the new code threw an exception. I would have thought you would notice that, though, you get the stack trace in red in your console windows and that's rather hard to miss. That goes against the exception-throwing theory, then. But since you're using Eclipse, why not just step through it in debug mode and find out what happens?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!