繁体   English   中英

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

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

我是 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);


}


}

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

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


}

我还在 class 秒中创建了一个变量名文件名,您将从 class 一个中传递它,同时创建一个 object 的 ZA2F2ED4F8EBC2ABC1DCB4ZC2 之类的 object

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

第二个 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");
    }
    
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM