-3

Hey I'm looking to run my java class and each time it is run it would print the console output to a new text file on the desktop. I know my syntax is a little weird with the methods but that's just how my mind works. I tried changing the output to a String variable so I could try to directly print it to the new text file however that didn't work. I removed all of my console to text code just for y'alls convenience. So basically my question is, when I run this, how would I print it to a new text file? (I apologize for any posting errors on my part as this is my first post on this site and I don't know the typical format) So I am editing this because I got downvoted because someone thought it was a replica question... I didn't state this but I know how to make a file through java but I want to make the output of a method be printed to a new text file, not just hard code it in. Sorry for the inconvenience.


public class Main {
 public static void main(String[] args){

     addHash();
 }

 public static void addHash() {
     String outString = "#";
     for (int maxRun = 0; maxRun <= 1000; maxRun++) {
         for (int y = 0; y <= 10; y++) {
             for (int x = -1; x <= y; x++) {

                 System.out.print(outString);
             }
             outString += "#";
             System.out.println("");


         }

         for (int y = 10; y >= 0; y--) {
             for (int x = 0; x <= y; x++) {
                 System.out.print(outString);
             }
             outString = outString.substring(0, outString.length() - 1);
             System.out.println("");


         }
     }
 }
}
FuManSchu
  • 1
  • 1
  • 3
  • Why can't you just use Log4j file appender concept http://www.java4s.com/log4j-tutorials/example-on-log4j-properties-file-with-fileappender/ – Aditya Aug 10 '16 at 17:13

4 Answers4

1

For posting always in a new file, you can use java.util.UUID. Complete flow will be like:

  1. Generate a UUID using UUID.randomUUID() and consider this UUID as your file name
  2. Check if the file name already exists at particular location (say desktop in your case)
  3. If it exists, go to step 1. If it doesn't exists, go to step 4
  4. You have your unique file name
Amber Beriwal
  • 1,568
  • 16
  • 30
0

Whatever you want to display and write to the file, let's say content is the String variable that holds it. You can now follow this method to just write the stuff to the file.

public static void main(String[] args) {
        try {
            String content = "This is the content to write into file";

            File file = new File("/users/mkyong/filename.txt");

            // if file doesnt exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(content);
            bw.close();

            System.out.println("Done");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
sekhar
  • 710
  • 1
  • 7
  • 13
0

Place your sysouts as it is run your program from cmd prompt

compile: javac Main.java

run: java Main > stacktrace.txt

This will take care of writing your console output to file.

Aditya
  • 2,385
  • 18
  • 25
-1

This code will give you your required output.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

class WriteToFile {
public static void main(String[] args) throws FileNotFoundException {
    String fileName = generateFileName();
    addHash(fileName);
}

public static void addHash(String file) throws FileNotFoundException {
    PrintStream out = new PrintStream(new FileOutputStream(file));

    String outString = "#";
    for (int maxRun = 0; maxRun <= 1000; maxRun++) {
        for (int y = 0; y <= 10; y++) {
            for (int x = -1; x <= y; x++) {

                System.setOut(out);
                System.out.print(outString);
            }
            outString += "#";
            System.setOut(out);
            System.out.println("");

        }

        for (int y = 10; y >= 0; y--) {
            for (int x = 0; x <= y; x++) {
                System.out.print(outString);
            }
            outString = outString.substring(0, outString.length() - 1);
            System.out.println("");

        }
    }
}
//creating filename
public static String generateFileName() {
    int i = 0;
    String filename = "D:\\TempFile"; //it will be stored inside D drive,you can change drive value according to your need.
    File f = new File(filename);
    while (f.exists()) {
        i++;
        filename = "D:\\TempFile"; 
        filename += Integer.toString(i);
        f = new File(filename);
    }
    return filename;
}
}
Rishal
  • 1,480
  • 1
  • 11
  • 19
  • This seems to be working flawlessly however I can't seem to locate the designated file creation location... – FuManSchu Aug 10 '16 at 18:07
  • you are using eclipse or running it through console? – Rishal Aug 10 '16 at 18:09
  • IntelliJ. It runs, however, I have no idea where the file it creates is located. – FuManSchu Aug 10 '16 at 18:40
  • it will be in your classpath, else try the edited code it will create the file in D drive. – Rishal Aug 10 '16 at 18:48
  • I found it! Thanks so much! – FuManSchu Aug 10 '16 at 18:52
  • Although, we are creating the file names by ourselves, this code is not thread-safe. UUID provides you thread-safety and guarantees unique identifier for one particular runtime. Also, if you create large bundle of files (say 100000), checking file name will happen 100000 times whereas in case UUID, it will happen only once. – Amber Beriwal Aug 20 '16 at 15:57
  • This programm is not at all written keeping thread safety in context, It was just a demonstration that you can do it in this way, Anyways thanks for downvoting and keep up the spirit :) – Rishal Aug 20 '16 at 16:21