tags:

views:

119

answers:

4

How can I modify this run method to not only print the srting in the output window, but also write to a file for example outfile.txt in project directory. Also each string should be on a separate line in the file.

So I have already created a file in the project directory called outfile.txt

A the moment the code is print fine on window but not printing in the text file

here is the code #

public void run() throws IOException
{
    Scanner sc = new Scanner(System.in);
    boolean cont = true;
    while (cont) {
       System.out.println("Enter text");
       String s = sc.nextLine();
       if ("*".equals(s)) {
           cont = false;
       } else {
           String result = shorthand(s);
           System.out.println(result);
           PrintWriter pw = new PrintWriter("outfile.txt");
           pw.println(result);
      }
   }
}
A: 

You should not use relative paths in java.io.File stuff. They will be relative to the current working directory which in turn depends on the way how you started the Java application. This may not be per se the project directory you expect it to be. You should always use absolute paths in java.io.File. E.g. c:/path/to/outfile.txt.

To learn where it is currently actually been written to, do:

 File file = new File("outfile.txt");
 System.out.println(file.getAbsolutePath());

Once again, never use relative paths. Always use absolute paths. Else just put it in the classpath and make use of ClassLoader#getResource() and then URL#toURI() and then new File(uri).

And indeed, close the streams. It will implicitly flush the data buffer to the resource.

BalusC
PrintWriter out = new PrintWriter(new FileWriter("outfilename")); out.write(result); out.close(); But still doesn't seem to be working, I have tried full path of where the file is aswell. Any ideas
luvthug
+4  A: 

Once you finish writing out, you need to close the open file:

pw.Close();
Oded
PrintWriter out = new PrintWriter(new FileWriter("outfilename")); out.write(result); out.close(); But still doesn't seem to be working, I have tried full path of where the file is aswell. Any ideas
luvthug
What _are_ you seeing? I would get you would only see the _last_ line, as you are overwriting the file each time you go through the loop.
Oded
No the text file is empty nothing is being written init
luvthug
Do you open the file for writing again? The old file would be erased and a new, empty one created.
Hardcoded
All the file should be able to do is when you type text in the input window is copy that text in the text file.so for example i type in input box.I hate java on screen it appears I hate java but the txt file remains competely empty
luvthug
Initialize your `PrintWriter` outside the `while` loop and do the `out.Close()` after the loop finishes. What is the result?
Oded
still nothing in text file
luvthug
Are you sure you are looking at the right file? Have you used the full path? Do you have the right permissions?
Oded
Mistake from it didn't have instance in the main class, now I have created that but it only seems to be coping the last input which meant to end the program and not print in the text file.for example:input: I hate java pressed return and it entered * to end program the txt file only shows * but inactual it should show I hate java
luvthug
@luvthug - it probably keeps _overwriting_ the file with whatever character you last entered. Think of changing the logic to either append a character at a time or write the whole string in one go.
Oded
I will keep trying in this case then , thank you for your help
luvthug
+1  A: 

Just take a look at exampledot.

khmarbaise
We like seeing _answers_ here, not links.
Oded
PrintWriter out = new PrintWriter(new FileWriter("outfilename")); out.write(result); out.close(); But still doesn't seem to be working, I have tried full path of where the file is aswell. Any ideas
luvthug
A: 

You're creating a new printwriter every time. Consider making it before the loop, as such

PrintWriter pw = new...
while(cond) {
    ...
    pw.println(...);
    pw.flush(); // do this too, it does the actual writing
}
pw.close();
glowcoder
no luck. basically the question is asking for the following:Modify the run method so that now the compressed string is not only printed in the output window, but also written to a file called outfile.txt in the project directory. Each compressed string should be on a separate line in the file. When * entered the code should terminate but * should not be written in the file
luvthug
Here's what I get when I copy your code and make my changes--> the code http://pastebin.com/LmP55aZUDoes this not work for you?
glowcoder