Java-将变量从用户输入传递到另一个Java文件
我是Java新手,我有一个项目要做,所以我有一个Java文件,用户必须从目录中的文件列表中进行选择。来自用户的输入保存在变量(文件名)中。我想在另一个java文件中使用该变量来执行其他工作。我在网上搜索,但没有找到适合我的解决方案。也许我做错了什么 第一个文件的代码:Java-将变量从用户输入传递到另一个Java文件,java,file,variables,Java,File,Variables,我是Java新手,我有一个项目要做,所以我有一个Java文件,用户必须从目录中的文件列表中进行选择。来自用户的输入保存在变量(文件名)中。我想在另一个java文件中使用该变量来执行其他工作。我在网上搜索,但没有找到适合我的解决方案。也许我做错了什么 第一个文件的代码: public class Director { private static void copyFileUsingStream(File source, File dest) throws IOException { Input
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");
}
}
我还在第二类中创建了一个变量名filename,您将从第一类传递它,同时创建第二类的对象,如
TestFFMpeg obj = new TestFFMpeg();
obj.methodInSecondClass(filename);
二等舱:
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");
}
}
你可以在第二类中创建一个构造函数,在第二类中,你可以将文件名作为参数传递。我有个问题要问,你什么时候必须在第一类中传递文件名参数?请指定位置好吗?@harmandepsingkalsi我想在ffmpeg生成器的输入(和输出)中传递filename变量。我看到你的密码了。我把“TestFFMpeg obj=newtestffmpeg(filename);”放在哪里?在班级的声明之上?当我将您编写的其他代码放在类的声明下时,我得到了关于变量filenameNo的错误,您应该在类1中编写它:TestFFMpeg obj=newtestffmpeg(filename);因为你有第一类的文件名,对吗?我认为应该在复制文件之前写。在此之前:copyFileUsingStream(源、目标);我更新了错误代码。谢谢!现在我在第二个类中遇到了一个错误,我将变量filename作为输入和输出(我更新了这些部分的代码)。错误是:无法对非静态方法进行静态引用。代码中有一个小的更新。setInput(“C:\\Users\\miltos\\Desktop\\polimesa\\available\U videos\\”+文件名)。addOutput(“C:\\Users\\miltos\\Desktop\\polimesa\\videos\\”+文件名)。请更新