java 文件生成

记录一下

本篇文章主要是写了如何把一些信息写入到一个文件中。在这里呢,我是把地址前缀放到一个配置文件中,方便后期修改。

业务需求:通过后端处理过后返回的错误信息写入到一个txt文件里面

简单直接,上代码

	    Properties properties = new Properties();
        try {
			properties.load(this.getClass().getClassLoader().getResourceAsStream("fileConfig.properties"));
		} catch (IOException e) {
			e.printStackTrace();
		}
        String path = System.getProperty("catalina.home"); // 获取tomcat路径s
        String fileUpload = properties.getProperty("vaildErrPath"); // 读取配置文件配置的路径
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMMdd");
        String dateStr = sdf.format(date) + ".txt";  // 存入对应任务表中的url列
        String dStr = sdf2.format(new Date());
        String tPath = path + fileUpload + "/" + dStr + "/" + dateStr;
        File file = new File(tPath);
        if(!file.exists()) {
            try {
				file.createNewFile(); // 创建新文件,有同名的文件的话直接覆盖
				FileOutputStream fos = new FileOutputStream(file,true);
		        OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");
		        BufferedWriter bw = new BufferedWriter(osw);
		        bw.write("共【"+errCount+"】行校验不通过!分别是第【"+errStr.toString()+"】行。");
		        bw.newLine();
		        bw.flush();
		        bw.close();
		        osw.close();
		        fos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
        }

就是这么简单粗暴

欢迎评论探讨!

代码跑不通可留言

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java文件生成二维码代码的示例: ```java import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) { String data = "https://www.example.com"; String filePath = "qrcode.png"; int size = 200; String fileType = "png"; File qrFile = new File(filePath); createQRCode(data, size, fileType, qrFile); System.out.println("QR Code created successfully!"); } private static void createQRCode(String data, int size, String fileType, File qrFile) { try { // Set QR code parameters Map<EncodeHintType, Object> hintMap = new HashMap<EncodeHintType, Object>(); hintMap.put(EncodeHintType.CHARACTER_SET, "UTF-8"); // Create QR code QRCodeWriter qrCodeWriter = new QRCodeWriter(); BitMatrix byteMatrix = qrCodeWriter.encode(data, BarcodeFormat.QR_CODE, size, size, hintMap); int width = byteMatrix.getWidth(); BufferedImage image = new BufferedImage(width, width, BufferedImage.TYPE_INT_RGB); image.createGraphics(); // Set QR code colors int white = 255 << 16 | 255 << 8 | 255; int black = 0; for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) { image.setRGB(i, j, byteMatrix.get(i, j) ? black : white); } } // Write QR code to file ImageIO.write(image, fileType, qrFile); } catch (WriterException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } ``` 这段代码使用了Google的ZXing库来生成二维码。它创建了一个包含指定数据的二维码,并将其写入指定的文件中。您可以通过更改`data`,`size`,`fileType`和`qrFile`变量来生成不同的二维码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值