java读取文本文件最后一行 java 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 LineNumberInputStream LineNumberReader 额这两个类试试看 LineNumberReader lnr = new LineNumberReader(new InputStreamReader(formFile.getInputStream())); lnr.skip(formFile.getFileSize()); lnr.getLineNumber() 然后读取试试看 public static void main(String[] args) throws Exception { File file = new File("c:/data.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String buf = null; String str = null; if (file.length() > 1024 * 1024 * 1024) { br.skip(file.length() / 3 * 2);// 当文件超过1M大小的时候,跳过文本长度的2/3,具体跳过多少视实际情况而定,文件越大可以跳过的部分越多 } while ((buf = br.readLine()) != null) { str = buf; } br.close(); System.out.println(str); } 见3#,前面那2个类查了下api就是扯淡的 简单的定时任务可以用Timerpublic static void main(String[] args) throws Exception { Timer timer = new Timer(); timer.schedule(new TimerTask() { public void run() { try { File file = new File("c:/data.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String buf = null; String str = null; if (file.length() > 1024 * 1024 * 1024) { br.skip(file.length() / 3 * 2);// 当文件超过1M大小的时候,跳过文本长度的2/3,具体跳过多少视实际情况而定,文件越大可以跳过的部分越多 } while ((buf = br.readLine()) != null) { str = buf; } br.close(); System.out.println(str); } catch (Exception e) { e.printStackTrace(); } } }, 0, 5000); } public static String readLastLine(File file, String charset) throws IOException { if (!file.exists() || file.isDirectory() || !file.canRead()) { return null; } RandomAccessFile raf = null; try { raf = new RandomAccessFile(file, "r"); long len = raf.length(); if (len == 0L) { return ""; } else { long pos = len - 1; while (pos > 0) { pos--; raf.seek(pos); if (raf.readByte() == '\n') { break; } } if (pos == 0) { raf.seek(0); } byte[] bytes = new byte[(int) (len - pos)]; raf.read(bytes); if (charset == null) { return new String(bytes); } else { return new String(bytes, charset); } } } catch (FileNotFoundException e) { } finally { if (raf != null) { try { raf.close(); } catch (Exception e2) { } } } return null; } JAVA 我编的记事本中无法添加滚动条 python写入数据库的中文,用JAVA读就乱码 大家说说JAVA核心技术I、II怎么样 如何改变JFileChooser文件选择对话框左上角的图标和字体大小? 如何在JTabbedPane中植入JFrame的一些特性,比如可以关闭tab,其实就是使两者融合 请问这里论坛上的帖子能打包下载吗? 在JB6如何控制控件的大小 谁有用MD5签名的例子!!谢谢! 枚举 Calendar转Date出错 Map套Map的使用
LineNumberReader
额这两个类试试看
lnr.skip(formFile.getFileSize());
lnr.getLineNumber() 然后读取试试看
File file = new File("c:/data.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String buf = null;
String str = null; if (file.length() > 1024 * 1024 * 1024) {
br.skip(file.length() / 3 * 2);// 当文件超过1M大小的时候,跳过文本长度的2/3,具体跳过多少视实际情况而定,文件越大可以跳过的部分越多
} while ((buf = br.readLine()) != null) {
str = buf;
} br.close(); System.out.println(str);
}
public void run() {
try {
File file = new File("c:/data.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String buf = null;
String str = null; if (file.length() > 1024 * 1024 * 1024) {
br.skip(file.length() / 3 * 2);// 当文件超过1M大小的时候,跳过文本长度的2/3,具体跳过多少视实际情况而定,文件越大可以跳过的部分越多
} while ((buf = br.readLine()) != null) {
str = buf;
} br.close(); System.out.println(str);
} catch (Exception e) {
e.printStackTrace();
}
}
}, 0, 5000);
}
if (!file.exists() || file.isDirectory() || !file.canRead()) {
return null;
}
RandomAccessFile raf = null;
try {
raf = new RandomAccessFile(file, "r");
long len = raf.length();
if (len == 0L) {
return "";
} else {
long pos = len - 1;
while (pos > 0) {
pos--;
raf.seek(pos);
if (raf.readByte() == '\n') {
break;
}
}
if (pos == 0) {
raf.seek(0);
}
byte[] bytes = new byte[(int) (len - pos)];
raf.read(bytes);
if (charset == null) {
return new String(bytes);
} else {
return new String(bytes, charset);
}
}
} catch (FileNotFoundException e) {
} finally {
if (raf != null) {
try {
raf.close();
} catch (Exception e2) {
}
}
}
return null;
}