-2

I have a .java classfile with a main method which produces a file "output.txt".

I want to write a program in Java which compiles and runs it, so that the file "output.txt", produced by given Java class, is outputted to a specified folder. How can this be done?

I can compile and run it, but I couldn't make something like a cd command in Java. Showing absolute class in Java is not allowed.

UmNyobe
  • 22,539
  • 9
  • 61
  • 90
Roman Lebedev
  • 903
  • 1
  • 6
  • 16
  • Can you edit the code that creates `output.txt`? – Garrett Hall May 08 '12 at 20:31
  • 1
    I believe you are creating `output.txt` programmatically. So, just give the complete path of the file like `C:\myfolder\output.txt` instead of simply `output.txt` in your java code. If you can edit the code. – RanRag May 08 '12 at 20:33
  • 1
    "Showing absolute class in java is not allowable" Why not? What do you mean by that statement? – Gray May 08 '12 at 20:44
  • C:\myfolder\output.txt is not allowable due to file specifications, only same folder output, cant be discussed. Also I cant modify Java class, it's like class which is been tested by it's output. – Roman Lebedev May 08 '12 at 20:51
  • 1
    Your above comment is pretty unclear to me but anyways take a look at [FileUtils](http://commons.apache.org/io/apidocs/org/apache/commons/io/FileUtils.html). – RanRag May 08 '12 at 20:57
  • possible duplicate of [Changing the current working directory in Java?](http://stackoverflow.com/questions/840190/changing-the-current-working-directory-in-java) – trutheality May 08 '12 at 20:59
  • I cant modify the java class, that'it. It is like input. – Roman Lebedev May 08 '12 at 21:13
  • I don't understand "Showing absolute class" either. Can't you produce the file, and move it to the target dir later? Or call the class from the destination dir, giving the class's path in the - surprise - CLASSPATH? – user unknown May 08 '12 at 21:14
  • I'll describe the application, it checks if submitted(uploaded) .java files after compiling and running with some "input.txt" produce a right "output.txt" and then show a result. It's a servlet, that means it's multithreaded app, so i cant store file in folder and then move, because if 2 users simultaneously upload, it can cause data unconsistency or loss. – Roman Lebedev Jun 21 '12 at 09:39

2 Answers2

0

Create a File object pointing to the specified folder.

Then use new File(file, "output.text") to create a file object referencing "output.txt" in the folder pointed to above. See http://docs.oracle.com/javase/1.4.2/docs/api/java/io/File.html#File(java.io.File, java.lang.String)

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
0

I want to write a program in Java which compiles and runs it, so that the file "output.txt", produced by given Java class, is outputted to a specified folder. How can this be done?

Let's get this straight:

  • The existing Java application A outputs a file output.txt ... somewhere. You can't change that application.
  • You want to write a application B that runs A in such a way that you can control the directory to which output.txt is written.

Q: Can it be done?

A: It depends on how A opens the output file.

  • If A uses a relative pathname for the file; e.g. new FileWriter(new File("output.txt")) then it can be done; see below.

  • If A uses an absolute pathname for the file; e.g. new FileWriter(new File("/output.txt")) then it cannot be done. There's no practical way to cause A to put the file somewhere else. (I wouldn't go so far as to say that it is theoretically impossible, but you'd need to resort to things that no sane developer would seriously contemplate doing.)

How you deal with the relative case is to have B use the ProcessBuilder to specify a different current directory for A when launching it. The javadoc has an example that covers this; look for the use of setDirectory in the example ... and its specification.

(Note: you can also do this by running an external script that changes directory and runs the command. But that approach involves platform specific things, and should be avoided if you want your code to be portable.)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216