「java高效读写文件」java 文本文件读写

博主:adminadmin 2022-12-15 18:51:06 63

今天给各位分享java高效读写文件的知识,其中也会对java 文本文件读写进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

Java文件读写

实用的模糊(通配符)文件查找程序

1 import java.io.File;

2 import java.util.regex.Matcher;

3 import java.util.regex.Pattern;

4 import java.util.ArrayList;

5

6 /** *//**

7 * pTitle: FileService /p 8* pDescription: 获取文件 /p 9* pCopyright: Copyright (c) 2007/p

10* pCompany: /p

11* @author not attributable

12* @version 1.0

13*/

14public class FileService {

15 public FileService() {

16 }

17

18 /** *//**

19 * 在本文件夹下查找

20 * @param s String 文件名

21 * @return File[] 找到的文件

22 */

23 public static File[] getFiles(String s)

24 {

25 return getFiles("./",s);

26 }

27

28 /** *//**

29 * 获取文件

30 * 可以根据正则表达式查找

31 * @param dir String 文件夹名称

32 * @param s String 查找文件名,可带*.?进行模糊查询

33 * @return File[] 找到的文件

34 */

35 public static File[] getFiles(String dir,String s) {

36 //开始的文件夹

37 File file = new File(dir);

38

39 s = s.replace('.', '#');

40 s = s.replaceAll("#", "\\\\.");

java中如何提高读取文件内容的效率???

import java.util.*;

import java.io.*;

class Main{

static public void main(String[] str)throws Exception{

File f=new File("1.txt");

write(f,1024*1024);//写文件

read(f);

}//main

static public void write(File f,long length)throws Exception{

long begin=System.currentTimeMillis();

PrintWriter p=new PrintWriter(f);

for(long l=0;llength;l++){

char c=(char)(int)(Math.random()*70+40);

p.print(c);

}

long end=System.currentTimeMillis();

System.out.println("Write "+length+"char OK! Time:"+(end-begin));

}

static public void read(File f)throws Exception{

long begin=System.currentTimeMillis();

Scanner s=new Scanner(f);

while(s.hasNext()){

s.next();

}

long end=System.currentTimeMillis();

System.out.println("Read OK!Time:"+(end-begin));

}

}//class

Java操作高效并发操作读写文件,消息持久化到本地读取

楼主,如果写,先判断要写多大的文件、然后分段写,各线程写自己的段

如果读,也是先得到文件大小、再分段,然后各线程读自己的段

Java中如何高效的读取大文件

读取文件行的标准方式是在内存中读取,Guava 和Apache Commons IO都提供了如下所示快速读取文件行的方法:

Files.readLines(new File(path), Charsets.UTF_8); FileUtils.readLines(new File(path));

这种方法带来的问题是文件的所有行都被存放在内存中,当文件足够大时很快就会导致程序抛出OutOfMemoryError 异常。

java如何高效读取文本的第N行和最后N行?

一般的需求就用java的io 包里的 BufferedReader缓存一下,效率还算可以,

更高的性能要求,读N行或后N行,可以看看java.nio包里的东西。

下面是一个例子程序,给你参考一下(这里是读第N行的,读后N行的把第一个方法稍作修改即可,不举例了,呵呵):

public class ReadSelectedLine {

static void readLineVarFile(String fileName, int lineNumber) throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(

new FileInputStream(fileName)));

String line = reader.readLine();

if (lineNumber 0 || lineNumber getTotalLines(fileName)) {

System.out.println("不在文件的行数范围之内。");

}

int num = 0;

while (line != null) {

if (lineNumber == ++num) {

System.out.println("line " + lineNumber + ": " + line);

}

line = reader.readLine();

}

reader.close();

}

// 文件内容的总行数。

static int getTotalLines(String fileName) throws IOException {

BufferedReader in = new BufferedReader(new InputStreamReader(

new FileInputStream(fileName)));

LineNumberReader reader = new LineNumberReader(in);

String s = reader.readLine();

int lines = 0;

while (s != null) {

lines++;

s = reader.readLine();

}

reader.close();

in.close();

return lines;

}

public static void main(String[] args) throws IOException {

// 读取文件

String fileName = "d:/hst_23.txt";

// 获取文件的内容的总行数

int totalNo = getTotalLines(fileName);

System.out.println("There are "+totalNo+ " lines in the text!");

// 指定读取的行号

int lineNumber = 10;

//读取指定行的内容

readLineVarFile("d:/hst_23.txt", lineNumber);

}

}

关于java高效读写文件和java 文本文件读写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-12-15,除非注明,否则均为首码项目网原创文章,转载请注明出处。