Java倒序读取文件夹最新更改的文件

借鉴其他博客上的方法,自己整理了一个倒序读取最新文件的方法,如有侵权则删除。

public Map<String,String> read(String fileName ) {
		// 获取最新改动的文件名
		File path = new File(fileName);
		if (path.exists()) {
			// 列出该目录下所有文件和文件夹
			File[] files = path.listFiles();
			// 按照文件最后修改日期倒序排序
			Arrays.sort(files, new Comparator<File>() {
				@Override
				public int compare(File file1, File file2) {
					return (int) (file2.lastModified() - file1.lastModified());
				}
			});
			fileName += files[0].getName();
		}
		Map<String, String> map = new HashMap<String, String>();
		File file = new File(fileName);
		if (file.exists()) {
			RandomAccessFile rf = null;
			try {
				rf = new RandomAccessFile(fileName, "r");
				long len = rf.length();
				long start = rf.getFilePointer();//返回此文件当前偏移量
				long nextend = len + start - 1;
				rf.seek(nextend); //设置偏移量为文件末尾行
				String line;
				int c = -1;
				while (nextend > start) {
					c = rf.read();
					if (c == '\n' || c == '\r') {		//只有行与行之间才有\r\n,这表示读到每一行上一行的			      		    末尾的\n,而执行完read后,
														//指针指到了这一行的开头字符
						line = rf.readLine();
						if (line != null) {
							if (line.contains("string")) {   //判断获取的line
								line = new String(line.getBytes("ISO-8859-1"), "UTF-8");						
							}
							nextend--;
						}
					}
					nextend--;
					rf.seek(nextend);
					if (nextend == 0) {  //读取到第一行时
						rf.seek(0);   //不需要为下次做准备了,但是因为read()方法指针从0到了1,需重置为0
						line = rf.readLine();
						if (line != null) {
							if (line.contains("Heart") || line.contains("Carcheck")) {
								line = new String(line.getBytes("ISO-8859-1"), "UTF-8");
							}
						}
					}
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					if (rf != null) {
						rf.close();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return map;
		}
		return map;
	}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中可以使用RandomAccessFile类的seek()和read()方法来读取文件字节。 1. 首先,需要创建一个RandomAccessFile对象,指定文件路径和读取模式,如下所示: ```java RandomAccessFile raf = new RandomAccessFile("example.txt", "r"); ``` 2. 然后,使用seek()方法将文件指针移动到文件末尾,如下所示: ```java raf.seek(raf.length()); ``` 3. 接下来,使用read()方法读取文件字节,每次读取一个字节,如下所示: ```java byte[] buffer = new byte[1]; while (raf.getFilePointer() > 0) { raf.seek(raf.getFilePointer() - 1); raf.read(buffer); System.out.print(new String(buffer)); } ``` 在上面的代码中,先创建一个长度为1的byte数组作为缓冲区,然后使用while循环,将文件指针向前移动一个字节,读取一个字节到缓冲区,并将缓冲区中的字节转换成字符串输出。 4. 最后,记得关闭RandomAccessFile对象,如下所示: ```java raf.close(); ``` 完整代码示例: ```java import java.io.IOException; import java.io.RandomAccessFile; public class ReverseReadFile { public static void main(String[] args) throws IOException { RandomAccessFile raf = new RandomAccessFile("example.txt", "r"); raf.seek(raf.length()); byte[] buffer = new byte[1]; while (raf.getFilePointer() > 0) { raf.seek(raf.getFilePointer() - 1); raf.read(buffer); System.out.print(new String(buffer)); } raf.close(); } } ``` 注意:读取文件字节时,要确保文件编码和缓冲区大小与实际情况相符。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值