根据小写26个字母,随机生成单词,乱文

根据小写26个字母,随机生成单词,乱文


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

/**
 * @author Mxhlin
 * @Email fuhua277@163.com
 * @Date 2022/09/22/19:27
 * @Version
 * @Description 根据小写26个字母,随机生成单词,乱文
 */
public class OutputStreamDemo {
    public static void main(String[] args) {
        Random rand = new Random();
        try (FileOutputStream fos = new FileOutputStream("木.txt")){
            for (int i = 0; i < 100; i++) {
                fos.write(randStr(rand.nextInt(2,20)).concat("\t\n").getBytes());
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static String randStr(int n) {
        Random rand = new Random();
        String letter = "abcdefghijklmnopqrstuvwsyz";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            StringBuilder sb1 = new StringBuilder();
            int len = rand.nextInt(2,10);// 随机单词的长度
            for (int j = 0; j < len; j++) {// 给单词赋值
                sb1.append(letter.charAt(rand.nextInt(letter.length())));
            }
            sb1.append(" ");// 后面添加空格

            String ts = new String(sb1);
            if (i ==0 ){
                // 首字母大写
                ts = ts.substring(0,1).toUpperCase().concat(ts.substring(1));
            }else if (i == n-1){
                // 结尾符号
                ts = ts.substring(0,ts.length()-1)+"!.?".charAt(rand.nextInt(3));
            }
            sb.append(ts);
        }
        return sb.toString()    ;
    }
}

识别一个文件的真实格式

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

/**
 * @author Mxhlin
 * @Email fuhua277@163.com
 * @Date 2022/09/22/19:52
 * @Version
 * @Description  识别一个文件的真实格式
 */
public class Ex3 {


    public static void main(String[] args) {
        HashMap<String, String> map = new HashMap<>();
        map.put(".jpg","ffd8ffe0");
        map.put(".7z","377abcaf");
        map.put(".zip","504b34");
        map.put(".txt","aced05");
        System.out.println(map.containsKey(".7z"));

        System.out.println(getHeader(new File("D:\\IOTest\\112\\112.7z"), 4));

        String type = getType(new File("D:\\IOTest\\112\\112.7z"));
        System.out.println(type);
        // File file = new File("D:\\IOTest\\112");
        // File[] files = file.listFiles();
        // for (File file1 : files){
        //     String name = file1.getName();
        //     String hou = name.substring(name.lastIndexOf(".")).toLowerCase();
        //     System.out.printf("%s : %s : %s %n",name,hou,getHeader(file1,4));
        // }
    }

    public static String getType(File file){
        HashMap<String, String> map = new HashMap<>();
        map.put("ffd8ffe0",".jpg");
        map.put("377abcaf",".7z");
        map.put("504b34",".zip");
        map.put("aced05",".txt");
        String header = getHeader(file, 4);
        if (map.containsKey(header)){
            return map.get(header);
        }else {
            return "未知文件";
        }
    }


    public static String getHeader(File file,int n){
        String header = "";
        if (file.isFile()){
            try (FileInputStream fis = new FileInputStream(file)){
                byte[] bytes = new byte[n];
                fis.read(bytes);
                StringBuilder sb = new StringBuilder();
                for (byte byt : bytes){
                    sb.append(String.format("%x",byt));
                }
                header = sb.toString();
            } catch (FileNotFoundException e) {
                throw new RuntimeException(e);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return header;
    }
}

指定一个目录下所有的java文件,把里面的内容格式化输出在md文件


 


import java.io.*;

/**
 * @author Mxhlin
 * @Email fuhua277@163.com
 * @Date 2022/09/22/17:08
 * @Version
 * @Description 指定一个目录下所有的java文件,把里面的内容格式化输出在md文件
 */
public class AllCodeDemo {
    static int sum= 0 ;
    public static void main(String[] args) {
        all(new File("D:\\peixun\\java\\Lx"));
    }

    public static void all(File file){
        if (file.isDirectory()){
            File[] files = file.listFiles();
            for (File file1 : files) {
                if (file1.isDirectory()) all(file1);
                if (file1.isFile()&&file1.getName().endsWith(".java")){
                    all(++sum,file1,new File("木.md"));
                }
            }
        }
        if (file.isFile()&&file.getName().endsWith(".java")){
            all(++sum,file,new File("木.md"));
        }
    }

    public static void all(int n, File src,File dst){
        String template = """
                %d %s (%d行 %tF %<tT)
                ```java
                %s
                ```
                
                """;
        try (FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(dst,true)){
            // 根据上面的参数 格式化
            sum = n;
            String path = src.getAbsolutePath();
            String s = new String(fis.readAllBytes());
            long count = s.lines().count();
            long l = src.lastModified();
            String format = String.format(template, sum, path, count, l, s);
            fos.write(format.getBytes());
            fos.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值