Miltos Taramanidis
Miltos Taramanidis

Reputation: 23

Java - Pass a variable from user input to another java file

I am new to Java and I have a project to do, so I have a java file and the user have to choose from a listing of files in a directory. The input from user is saved in a variable (fileName). I want to use that variable in another java file for doing some other work. I searched online but didn't find any solution that works for me. Probably I've done something wrong.

code of the first file:

public class Director {

private static void copyFileUsingStream(File source, File dest) throws IOException {
InputStream is = null;
OutputStream os = null;
try {
    is = new FileInputStream(source);
    os = new FileOutputStream(dest);
    byte[] buffer = new byte[1024];
    int length;
    while ((length = is.read(buffer)) > 0) {
        os.write(buffer, 0, length);
    }
} finally {
    is.close();
    os.close();
}
}

public static void main(String[] args) throws IOException {
    
    

    // Creates an array in which we will store the names of files and directories
    String[] pathnames;

    // Creates a new File instance by converting the given pathname string
    // into an abstract pathname
    File f = new File("C:\\Users\\miltos\\Desktop\\polimesa\\available_videos");

    // Populates the array with names of files and directories
    pathnames = f.list();
    System.out.println("Files in the directory:");
    // For each pathname in the pathnames array
    for (String pathname : pathnames) {
        // Print the names of files and directories
        System.out.println(pathname);
    }
    
    Scanner myObj = new Scanner(System.in);  // Create a Scanner object
    System.out.println("Enter file name");
    String fileName = myObj.nextLine();
    File source = new File("C:\\Users\\miltos\\Desktop\\polimesa\\available_videos\\" + fileName);
    File dest = new File("C:\\Users\\miltos\\Desktop\\polimesa\\raw_videos\\" + fileName);
    copyFileUsingStream(source, dest);


}


}

code of the second file that i want to use the input:

 public class TestFFMpeg {

static Logger log = LogManager.getLogger(TestFFMpeg.class);



public static void main(String[] args) {
    
    
    FFmpeg ffmpeg = null;
    FFprobe ffprobe = null;
    

    
    try {
        log.debug("Initialising FFMpegClient");
        ffmpeg = new FFmpeg("C:\\Users\\miltos\\ffmpeg\\bin\\ffmpeg.exe");
        ffprobe = new FFprobe("C:\\Users\\miltos\\ffmpeg\\bin\\ffprobe.exe");
    } catch (IOException e) {
        e.printStackTrace();
    }

    log.debug("Creating the transcoding");
    FFmpegBuilder builder = new FFmpegBuilder()
            .setInput("C:\\Users\\miltos\\Desktop\\polimesa\\raw_videos\\" + filename) //updated
            .addOutput("C:\\Users\\miltos\\Desktop\\polimesa\\videos\\" + filename) //updated
            .setVideoBitRate(200000) 
            .done();
      log.debug("Creating the executor");
    FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);

    log.debug("Starting the transcoding");
    // Run a one-pass encode
    executor.createJob(builder).run();
    log.debug("Transcoding finished");
}


}

Upvotes: 1

Views: 803

Answers (1)

Harmandeep Singh Kalsi
Harmandeep Singh Kalsi

Reputation: 3345

I created a variable names filename in class second also, which you will pass from the class one , while creating an object of class second like

TestFFMpeg obj = new TestFFMpeg();
obj.methodInSecondClass(filename);

Second Class :

    public class TestFFMpeg {
    
    static Logger log = LogManager.getLogger(TestFFMpeg.class);
    
    public void methodInSecondClass(String filename){

    FFmpeg ffmpeg = null;
        FFprobe ffprobe = null;
        
    
        
        try {
            log.debug("Initialising FFMpegClient");
            ffmpeg = new FFmpeg("C:\\Users\\miltos\\ffmpeg\\bin\\ffmpeg.exe");
            ffprobe = new FFprobe("C:\\Users\\miltos\\ffmpeg\\bin\\ffprobe.exe");
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        log.debug("Creating the transcoding");
        FFmpegBuilder builder = new FFmpegBuilder()
                .setInput("C:\\Users\\miltos\\Desktop\\polimesa\\available_videos\\"+filename) //this is where i want the same variable
                .addOutput("C:\\Users\\miltos\\Desktop\\polimesa\\videos\\"+filename) //this is where i want the same variable
                .setVideoBitRate(200000) 
                .done();
          log.debug("Creating the executor");
        FFmpegExecutor executor = new FFmpegExecutor(ffmpeg, ffprobe);
    
        log.debug("Starting the transcoding");
        // Run a one-pass encode
        executor.createJob(builder).run();
        log.debug("Transcoding finished");
    }
    
}

Upvotes: 1

Related Questions