读取文件,生成随机文件名

// 读取数据
	public String ReaderjsonAll(String url) {
		StringBuffer text = new StringBuffer();
		BufferedReader br = null;
		try {
			br = new BufferedReader(new InputStreamReader(new FileInputStream(url), "gbk"));
			String line = br.readLine();
			while (null != line) {
				text.append(line);
				line = br.readLine();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		LOG.info("测试读取数据"+text.toString());
		return text.toString();
	}



//生成指定文件名的文件
 public static String filePath(String path)throws Exception{

        String savePath = path;
        String uuid = UUID.randomUUID().toString().replace("-", "");//随机字串
        String suffix = ".html";
        String randomName = uuid+suffix;
        String fullName = savePath + File.separator + randomName;

        File file2=new File(fullName);
        file2.createNewFile();



        return file2.toString();

    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Snowflake算法生成分布式随机文件名,示例代码如下: ```java import java.io.File; import java.io.IOException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.node.ObjectNode; public class DistributedRandomFileNameGenerator { private static final long EPOCH = 1420041600000L; // 2015-01-01 private static final int NODE_ID_BITS = 10; private static final int SEQUENCE_BITS = 12; private static final long MAX_NODE_ID = (1L << NODE_ID_BITS) - 1; private static final long MAX_SEQUENCE = (1L << SEQUENCE_BITS) - 1; private final long nodeId; private long lastTimestamp = -1L; private long sequence = 0L; public DistributedRandomFileNameGenerator(long nodeId) { if (nodeId < 0 || nodeId > MAX_NODE_ID) { throw new IllegalArgumentException(String.format("NodeId must be between 0 and %d", MAX_NODE_ID)); } this.nodeId = nodeId; } public synchronized String generate() { long timestamp = System.currentTimeMillis() - EPOCH; if (timestamp < lastTimestamp) { throw new RuntimeException(String.format("Clock moved backwards. Refusing to generate id for %d milliseconds", lastTimestamp - timestamp)); } if (timestamp == lastTimestamp) { sequence = (sequence + 1) & MAX_SEQUENCE; if (sequence == 0) { timestamp = tilNextMillis(lastTimestamp); } } else { sequence = 0L; } lastTimestamp = timestamp; long id = ((timestamp << (NODE_ID_BITS + SEQUENCE_BITS)) | (nodeId << SEQUENCE_BITS) | sequence) & Long.MAX_VALUE; return Long.toString(id, 36); } private long tilNextMillis(long lastTimestamp) { long timestamp = System.currentTimeMillis() - EPOCH; while (timestamp <= lastTimestamp) { timestamp = System.currentTimeMillis() - EPOCH; } return timestamp; } public static void main(String[] args) throws IOException { DistributedRandomFileNameGenerator generator = new DistributedRandomFileNameGenerator(1); String fileName = generator.generate(); System.out.println(fileName); // Save nodeId and lastTimestamp to file for recovery ObjectMapper mapper = new ObjectMapper(); mapper.enable(SerializationFeature.INDENT_OUTPUT); ObjectNode node = mapper.createObjectNode(); node.put("nodeId", generator.nodeId); node.put("lastTimestamp", generator.lastTimestamp); mapper.writeValue(new File("generator.json"), node); } } ``` 上述代码中,`DistributedRandomFileNameGenerator`类使用Snowflake算法生成分布式随机文件名。在构造函数中指定节点ID,然后调用`generate()`方法即可生成随机文件名。为了防止时钟回拨等问题,需要在生成文件名时保证时间戳单调递增。如果需要在重启程序后恢复生成器状态,可以将节点ID和上一次生成文件名的时间戳保存到文件中,然后在程序启动时读取文件,恢复生成器状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值