Java 如何在android中动态设置FileOutputStream的文件名?

Java 如何在android中动态设置FileOutputStream的文件名?,java,android,file,file-io,android-sdcard,Java,Android,File,File Io,Android Sdcard,我的要求是文件的格式为filename.extension(.png、.mp4.mp3、txt等) 我想知道我该怎么做 示例代码行: File extStore = Environment.getExternalStorageDirectory(); FileOutputStream fos = new FileOutputStream(extStore + "/Get_file/filename.jpg"); 根据我的上述方法?环境。getExternalStorageDirector

我的要求是文件的格式为filename.extension(.png、.mp4.mp3、txt等)
我想知道我该怎么做

示例代码行:

 File extStore = Environment.getExternalStorageDirectory();

 FileOutputStream fos = new FileOutputStream(extStore + "/Get_file/filename.jpg");

根据我的上述方法?

环境。getExternalStorageDirectory()
返回实际的外部存储目录,作为
java
文件
对象调用
getAbsolutePath()
on返回其绝对路径,然后简单地连接所需的文件名或路径并创建一个
新文件(Environment.getExternalStorageDirectory().getAbsolutePath()+“/Get_file/filename.jpg”)
和ceck(如果文件存在)。如果不存在,则必须创建一个

用这样的东西

String filepath = "/Get_file/filename.jpg";
File yourFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + filepath);

if(!yourFile.exists()) {
    yourFile.createNewFile();
} 
FileOutputStream oFile = new FileOutputStream(yourFile, false); 

在文件名后添加时间戳,以便每次文件名都不同。

回答问题:

File namefile= new File(newFile);
en_Name=namefile.getName();

*** //pass the file name to fileoutputstream//***

FileOutputStream fos = new FileOutputStream(extStore + "/folder/"+en_name);    

最后我终于明白了…

我不确定我是否理解你的问题,但这里的这一位
“/Get_file/filename.jpg”
是一个字符串,因此你可以根据用户的一些输入或你用来确定正确文件名的任何东西动态构造该字符串。希望这对你有所帮助