java连续写一个文件

xiaoxiao2026-03-19  7

try { SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd"); System.err.println(sdf2.format(new Date())); // File f = new File("C:\\errorJmsMessage"+sdf2.format(new // Date())+".txt"); // 创建文件夹 File tf = new File("C:\\临时文件"); if (!tf.exists()) { tf.mkdir(); } File f = new File("C:\\临时文件\\errorJmsMessage" + sdf2.format(new Date()) + ".txt"); if (!f.exists()) { f.createNewFile(); System.err.println(f + "已创建!"); } String str; byte[] s; RandomAccessFile rf = new RandomAccessFile(f, "rw"); long count = rf.length(); rf.seek(count); // 如要换行,用 \r\n 次序不要乱 for (int i = 0; i < 4; i++) { str = "这是第 " + i + "行文本示例\r\n"; s = str.getBytes(); rf.write(s); } rf.close(); } catch (Exception e) { e.printStackTrace(); System.err.println("读写出错"); }

 

转载请注明原文地址: https://www.6miu.com/read-5046169.html

最新回复(0)