JAVA和Nginx 教程大全

网站首页 > 精选教程 正文

JAVA学习:跨平台时文件路径处理,读写配置文件

wys521 2024-11-08 15:08:41 精选教程 22 ℃ 0 评论

上次咱们做好了最基本的功能,但是我偷懒,下载路径是个固定的,没有使用界面中TextField中的值。下面我把让下载路径使用界面中TextField中的值,并且使用时间和随机数来当做视频文件名。

要点:用文件夹选择对话框选出来的目录名,最后面是没有结束符的,比如"D:\老赵和他的",我们在将目录名和文件名拼音在一起时会变成“D:\老和他的20210706xxxx.mp4”,这显然是不对的,中间少个“\”符号,但是我们的软件是要在各个平台下运行的,这个符号在windows下是\,但是在linux和mac下是/号。

那么我们怎么样来根据不同系统去使用正确的路径分割符呢?

答案是:File.separator

在JAVA中,这个File.separator就代表了当前系统的路径分割符号。下面我贴出相应的代码:

public void btn_parseUrl(ActionEvent event) throws IOException {
    //解析URL,把复制的视频URL转换为无水印的下载URL
    taMemoAddLine("开始解析URL: " + txtVideoUrl.getText());
    //创建解析实例
    parseVideoUrl pv = new parseVideoUrl(txtVideoUrl.getText());
    try {
        pv.parseUrl(); //解析url,解析完无水印url存在里面呢
        taMemoAddLine("开始下载。 " + pv.getNoWaterUrl());

        Random r = new Random(1); //初始化一个随机数
        int randNum = r.nextInt(9999); //取一个四位随机数,用来避免文件名重复
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); //格式化日期时间
        String fileName = df.format(System.currentTimeMillis()) + randNum  + ".mp4"; //用日期时间加随机数来当文件名

        String filePath = txtSaveDir.getText(); //取出TextField中的路径
        if (!filePath.endsWith(File.separator)){
            //如果路径最后一位不是 \或/ 号,就加一个,要不然路径不对。
            //File.separator 在不同的系统上表示不同的路径符号,在win上是\在linux下是/
            filePath += File.separator;
        }

        pv.downLoadVideo(filePath + fileName, pb_ProBar);
        taMemoAddLine("下载完成,文件名:" + filePath + fileName);
    } catch (IOException e) {
        e.printStackTrace();
    }

}

至此,我们点下载后,下载的路径和文件名就都是对的了。

但有一个新的问题是:我们每次启动程序后,都要选择一次文件夹名,否则文件夹框里是空的。这就需要我们要在选择好文件夹后,将文件夹的名称写到一个配置文件里去,然后程序再次启动时再读出来放到TextField中去。

经过搜索,JAVA中有原生配置文件读写类。我们直接使用就行了。

少啰嗦,放代码:

package sample;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Properties;

public class myCfg {
    private String fileName = "config.properties";

    public String getConfig(String key){
        File fi = new File(fileName);
        Properties pro = new Properties();
        try {
            pro.load(new FileInputStream(fi));
            return pro.getProperty(key);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public boolean setConfig(String key, String value){
        File fi = new File(fileName);
        Properties pro = new Properties();
        try {
            //pro.load(new FileInputStream(fi));
            pro.setProperty(key, value);
            pro.store(new PrintStream(fi), "UTF-8");
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

}

为了方便使用,新建了一个类用来读写配置文件。

然后在选择好文件夹后,存入磁盘中:

public void selectDirectoryDialog(ActionEvent event) throws IOException {
    //选择保存文件夹的点击事件。
    DirectoryChooser directoryChooser = new DirectoryChooser();
    directoryChooser.setTitle("视频保存到...");
    File directory = directoryChooser.showDialog(new Stage());
    if (directory != null) {
        String dirPath = directory.getAbsolutePath();
        txtSaveDir.setText(dirPath);
        myCfg cfg = new myCfg();
        cfg.setConfig("outputDir",dirPath);
    }
}

在Main.java中绑定程序启动后窗体第一次显示并渲染完成的事件,在事件发生时,将文件夹路径再读取出来:

primaryStage.setOnShown(new EventHandler<WindowEvent>() {
    @Override
    public void handle(WindowEvent event) {
        //监听到窗口第一次显示完成,载入默认的保存路径
        myCfg cfg = new myCfg();
        String dirPath = cfg.getConfig("outputDir");
        controller.setDefaultOutputDir(dirPath);
    }
});

成功。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表