Results 1 to 11 of 11
  1. #1
    koreylegend is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default How to save executed program to a file?

    Hi I wanted to know how I would save the executed program (results) to a file, I can create a file but not save the content I want on there.
    The program reads a file and puts html tags on the file to be viewed in a browser, but I am having trouble saving the file once the tags have been implemented, here is my code to create a file, if my full code is needed just ask and i will post it:

    try
    {
    // Create file

    FileWriter fstream = new FileWriter("myresults.html");
    BufferedWriter out = new BufferedWriter(fstream);
    out.write("results.txt");
    System.out.println("You created a file");
    //Close the output stream
    out.close();
    }
    catch(Exception e)
    {
    //Catch exception if any
    System.err.println("Error: " + e.getMessage());
    }

    Please help any information would be useful, thank you.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to save executed program to a file?

    Change one line (see below) and see it a file is saved there:

    Java Code:
    System.out.println("You created a file here: "+System.getProperty("user.dir");
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    koreylegend is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to save executed program to a file?

    Hi Jos thank you for your quick reply,
    A file is created but it only stores what ever i put in the out.write("results.txt"); part so when I compile the code the words results.txt appear and I would like the edited file (executed file) to be automatically saved, if that makes sense

    once again thank you for your help

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to save executed program to a file?

    Quote Originally Posted by koreylegend View Post
    Hi Jos thank you for your quick reply,
    A file is created but it only stores what ever i put in the out.write("results.txt"); part so when I compile the code the words results.txt appear and I would like the edited file (executed file) to be automatically saved, if that makes sense

    once again thank you for your help
    I don't think I understand your remark: do you want the source text of your Java program to be written to your file as well?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    koreylegend is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to save executed program to a file?

    Once my program has read the file, it adds html tags onto the file to make it appropriate to view as an html file, however I am currently copying and pasting the results once the program has compiled into a file.html and then manually opening that file. Ideally it would be nice if once the program has finished adding the html tags on that the end results were automatically saved to a new file.

    Thank you
    Korey

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to save executed program to a file?

    I'm sorry, I still don't understand what you have in mind: you write some text to a file, including html tags (which is also just text) and then you close your file; what else do you want to be written to your file?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    koreylegend is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to save executed program to a file?

    Sorry for poor explanation, basically When a basic txt file gets imported/read into my program, I have a code that inserts html tags onto the origanal txt file, however I would like the new edited text to be automatically saved into a new file when comiled.

    thank you

    Korey

  8. #8
    Jaeela is offline Member
    Join Date
    Mar 2011
    Posts
    21
    Rep Power
    0

    Default Re: How to save executed program to a file?

    I think he wants to write text to a file, then make a new file that has html tags which displays the text file.
    This code will make a file on the user's desktop and write a string to it:
    Note: whenever you want to go to a new line using this, you will have to use System.getProperty("line.separator");
    Java Code:
    File f = new File("C:/Users/" + System.getProperty("user.name") + "/Desktop/results.txt");
    String s = "results";
    PrintWriter pout = new PrintWriter(new FileWriter(file));
    pout.print(results);	//or println, whatever floats your boat.
    pout.close();

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,773
    Blog Entries
    7
    Rep Power
    21

    Default Re: How to save executed program to a file?

    Quote Originally Posted by koreylegend View Post
    Sorry for poor explanation, basically When a basic txt file gets imported/read into my program, I have a code that inserts html tags onto the origanal txt file, however I would like the new edited text to be automatically saved into a new file when comiled.
    I assume you do the inserting of html tags in memory; why don't you open another (new) file afterwards and write the modified text to that new file?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    koreylegend is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to save executed program to a file?

    Thank you ive mannaged to sort it with your help :)

  11. #11
    koreylegend is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to save executed program to a file?

    Thank you ive mannaged to sort it with your help :)

Similar Threads

  1. Replies: 3
    Last Post: 06-21-2011, 05:51 PM
  2. My first java program could not be executed..help
    By miaaa00 in forum New To Java
    Replies: 7
    Last Post: 03-21-2011, 09:07 AM
  3. save file based on file extension
    By masa in forum AWT / Swing
    Replies: 4
    Last Post: 05-11-2010, 12:17 PM
  4. Replies: 0
    Last Post: 03-05-2010, 05:21 AM
  5. how to save file..
    By jont717 in forum New To Java
    Replies: 2
    Last Post: 02-13-2009, 12:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •