跳至主要內容

代码应用实例集

TenSoFlow...大约 4 分钟代码应用代码应用

代码应用实例集

使用Java代码解决Markdown图片路径问题

问题描述

在Markdown上插入图片和Html一样都需要把图片放到一个文件夹中然后可以使用相对路径引用图片。你写的Markdown文档发给别人看时就需要把图片文件夹一起发给别人,不然图片不会正常显示。有时候我们也需要更改图片文件夹的路径,但是手动一张一张的更改太麻烦。既然是程序员就可以写代码来自动完成图片路径的更改。

代码

如想使用您只需改动下述 完整代码 中开头两个字符串的值

//替换成您Markdown文档的位置
final private static String filePath = "D:\\Desktop\\test.md";
//替换成您图片文件夹的位置
final private static String newFilePath = "D:\\Desktop";

完整代码

/*
   以下是一个Markdown中图片的路径
   ![img](D:\Desktop\Images\MySql\image.png)
   此功能会把Markdown文档中图片路径中的D:\Desktop\Images\MySql替换成你自定义的路径
*/
public class Main
{
    //Markdown文档的所在路径
    final private static String filePath = "D:\\Desktop\\test.md";
    /*\
        需要替换的路径
        如把 ![img](D:\Desktop\Images\MySql\image.png)
        替换成 ![img](D:\Desktop\image.png)
        则下方的 newFilePath 定义为 "D:\\Desktop"
     */
    final private static String newFilePath = "D:\\Desktop";

    public static String handle(String line)
    {
        /*
            以下是一个图片的路径
            ![img](D:\Desktop\Images\MySql\image.png)
         */

        //匹配是不是以![里面是任意字符]开头的 是则说明是图片
        Pattern pattern = Pattern.compile("^!\\[.*?\\]");
        Matcher matcher = pattern.matcher(line);
        //如果找到说明是图片
        if(matcher.find())
        {
            //得到 (D:\Desktop\Images\MySql\image.png)字符串
            pattern = Pattern.compile("\\(([^)]+)\\)");
            matcher = pattern.matcher(line);
            //如果找到则对D:\Desktop\Images\MySql\进行替换
            if(matcher.find())
            {
                String str = matcher.group(0);
                //去除( )
                str = str.substring(1, str.length() - 1);
                String regex = "\\\\";
                if(str.startsWith("."))
                {
                    regex = "/";
                }

                String Regex = "\\\\";
                if(newFilePath.contains("/")) Regex = "/";

                String[] strs = str.split(regex);

                //替换之后的字符串
                StringBuffer newStr = new StringBuffer();
                newStr.append("(")
                        .append(newFilePath)
                        .append(Regex)
                        .append(strs[strs.length-1])
                        .append(")");

                // 获取括号的结束位置
                int endIndex = matcher.end();
                // 获取括号后面的内容
                String contentAfter = line.substring(endIndex);
                // 返回新字符串
                return "![]" + newStr + contentAfter;
            }
        }
        return line;
    }
    public static String read(String filePath) throws IOException
    {
        BufferedReader reader = null;
        StringBuffer buf = new StringBuffer();
        try
        {
            reader = new BufferedReader(new FileReader(filePath));
            String line;
            while ((line = reader.readLine()) != null)
            {
                //按行处理
                line = handle(line);
                //处理之后进行拼接
                buf.append(line + "\n");
            }
        } catch (IOException e)
        {
            System.out.println("更换失败");
            e.printStackTrace();
        } finally
        {
            reader.close();
        }
        return buf.toString();
    }
    public static void delete(String filePath)
    {
        // 创建要删除的文件对象
        File fileToDelete = new File(filePath);
        // 检查文件是否存在并且是一个文件
        if (fileToDelete.exists() && fileToDelete.isFile())
        {
            // 尝试删除文件
            if (fileToDelete.delete())
            {
                System.out.println("文件删除成功: " + fileToDelete.getAbsolutePath());
            } else
            {
                System.err.println("无法删除文件: " + fileToDelete.getAbsolutePath());
            }
        } else
        {
            System.err.println("文件不存在或不是一个文件: " + fileToDelete.getAbsolutePath());
        }
    }
    public static void create(String filePath,String content) throws IOException
    {
        BufferedWriter writer = null;
        try
        {
            File file = new File(filePath);

            // 如果文件不存在,创建新文件
            if (!file.exists())
            {
                file.createNewFile();
            }

            // 使用BufferedWriter写入内容到文件
            writer = new BufferedWriter(new FileWriter(file));
            writer.write(content);

            System.out.println("内容已成功写入到文件: " + filePath);
        } catch (IOException e)
        {
            e.printStackTrace();
        } finally
        {
            writer.close();
        }
    }
    public static void main(String[] args) throws IOException
    {
        // 如果文件不存在,提示文件不存在
        File file = new File(filePath);
        if (!file.exists())
        {
            System.out.println("该路径下文件不存在");
            return ;
        }

        //进行读操作并返回对图片处理之后的字符串
        String str = read(filePath);

        //删除原文件
        delete(filePath);

        //创建新文件并写入新内容
        create(filePath,str);

        System.out.println("运行结束");
    }
}

使用Java代码获取文件夹下的所有文件路径

public static void main(String[] args)
{
    // 文件夹(不是文件)的路径
    String folderPath = "D:\\Desktop\\";
    // 用给定的文件夹路径创建File类
    File file = new File(folderPath);
    // 用数组把文件夹下的文件存起来
    File[] files = file.listFiles();
    // 如果文件数组为空则可能路径不对 给出提示
    if(files == null || files.length == 0)
    {
        System.out.println("您的文件夹下没有文件或者您的路径不对");
        return ;
    }
    //foreach遍历数组
    for (File file2 : files)
    {
        // 获取该文件夹下文件的全路径用getPath()方法
        String filePath = file2.getPath();
        System.out.println(filePath);
        // 获取该文件夹下文件的名称用getName()方法
        String fileName = file2.getName();
        System.out.println(fileName);
        System.out.println();
    }
}
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.8