提问者:小点点

Java-将一个变量从用户输入传递到另一个Java文件


我是新到Java和我有一个项目要做,所以我有一个Java文件和用户必须选择从一个目录的文件清单。 用户的输入保存在变量(文件名)中。 我想在另一个Java文件中使用这个变量来做一些其他的工作。 我在网上搜索,但没有找到任何适合我的解决方案。 可能是我做错了什么。

第一个文件的代码:

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);
    source.delete();

}


}

我要使用输入的第二个文件的代码:

 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\\available_videos\\??") //this is where i want the same variable
            .addOutput("C:\\Users\\miltos\\Desktop\\polimesa\\videos\\??") //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");
}


}

共1个答案

匿名用户

我还在类second中创建了一个变量名filename,您将从类1传递它,同时创建类second的一个对象,如

TestFFMpeg obj = new TestFFMpeg(filename);

二等:

 public class TestFFMpeg {

static Logger log = LogManager.getLogger(TestFFMpeg.class);
string filename ;
TestFFMpeg(string filename){
this.filename = filename;
}

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\\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");
}


}

相关问题