FuManSchu FuManSchu - 3 months ago 18
Java Question

Printing a java method to a text file

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("");


}
}
}
}

Answer

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;
}
}
Comments