在Java中读取纯文本文件

在Java中读取纯文本文件,java,file-io,ascii,Java,File Io,Ascii,在Java中,似乎有不同的方式来读取和写入文件数据 我想从文件中读取ASCII数据。可能的方法和它们的区别是什么?ASCII是一个文本文件,您可以使用它进行阅读。Java还支持使用从二进制文件读取。如果正在读取的文件很大,那么您可能希望使用a上的a来提高读取性能 详细介绍如何使用读卡器 我还建议你下载并阅读这本很棒(但免费)的书,书名是 在Java 7中: new String(Files.readAllBytes(...)) Files.lines(..).forEach(...) 或

在Java中,似乎有不同的方式来读取和写入文件数据


我想从文件中读取ASCII数据。可能的方法和它们的区别是什么?

ASCII是一个文本文件,您可以使用它进行阅读。Java还支持使用从二进制文件读取。如果正在读取的文件很大,那么您可能希望使用a上的a来提高读取性能

详细介绍如何使用
读卡器

我还建议你下载并阅读这本很棒(但免费)的书,书名是

在Java 7中

new String(Files.readAllBytes(...))
Files.lines(..).forEach(...)

在Java 8中

new String(Files.readAllBytes(...))
Files.lines(..).forEach(...)

最简单的方法是在Java中使用
Scanner
类和FileReader对象。简单的例子:

Scanner in = new Scanner(new FileReader("filename.txt"));
Scanner
有几种读取字符串、数字等的方法。。。您可以在Java文档页面上查找有关这方面的更多信息

例如,将整个内容读入
字符串

StringBuilder sb = new StringBuilder();
while(in.hasNext()) {
    sb.append(in.next());
}
in.close();
outString = sb.toString();
此外,如果您需要特定编码,您可以使用此编码而不是
文件读取器

new InputStreamReader(new FileInputStream(fileUtf8), StandardCharsets.UTF_8)

我最喜欢的读取小文件的方法是使用BufferedReader和StringBuilder。它非常简单,切中要害(虽然不是特别有效,但在大多数情况下都足够好):

有人指出,在Java 7之后,您应该使用(即自动关闭)功能:

try(BufferedReader br = new BufferedReader(new FileReader("file.txt"))) {
    StringBuilder sb = new StringBuilder();
    String line = br.readLine();

    while (line != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
        line = br.readLine();
    }
    String everything = sb.toString();
}
当我阅读这样的字符串时,我通常希望对每行进行一些字符串处理,所以我选择了这个实现

虽然如果我真的想把一个文件读入一个字符串,我总是使用Apache类IOUtils.toString()方法。您可以在此处查看源代码:

使用Java 7更简单:

try(FileInputStream inputStream = new FileInputStream("foo.txt")) {     
    String everything = IOUtils.toString(inputStream);
    // do something with everything string
}
其中的方法也可能非常方便,例如:

/**
 * Reads the contents of a file line by line to a List
 * of Strings using the default encoding for the VM.
 */
static List readLines(File file)

到目前为止,我还没有看到其他答案中提到这一点。但是,如果“最佳”意味着速度,那么新的JavaI/O(NIO)可能提供最快的性能,但对学习者来说并不总是最容易理解的


您想对文本做什么?文件是否足够小,可以放入内存?我会尽量找到最简单的方法来处理您的需要的文件。FileUtils库非常适合处理此问题

for(String line: FileUtils.readLines("my-text-file"))
    System.out.println(line);

我编写的这段代码对于非常大的文件要快得多:

public String readDoc(File f) {
    String text = "";
    int read, N = 1024 * 1024;
    char[] buffer = new char[N];

    try {
        FileReader fr = new FileReader(f);
        BufferedReader br = new BufferedReader(fr);

        while(true) {
            read = br.read(buffer, 0, N);
            text += new String(buffer, 0, read);

            if(read < N) {
                break;
            }
        }
    } catch(Exception ex) {
        ex.printStackTrace();
    }

    return text;
}
公共字符串readDoc(文件f){
字符串文本=”;
读取整数,N=1024*1024;
char[]buffer=新字符[N];
试一试{
FileReader fr=新的FileReader(f);
BufferedReader br=新的BufferedReader(fr);
while(true){
read=br.read(缓冲区,0,N);
text+=新字符串(缓冲区,0,读取);
如果(读取
以下是不使用外部库的另一种方法:

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public String readFile(String filename)
{
    String content = null;
    File file = new File(filename); // For example, foo.txt
    FileReader reader = null;
    try {
        reader = new FileReader(file);
        char[] chars = new char[(int) file.length()];
        reader.read(chars);
        content = new String(chars);
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if(reader != null){
            reader.close();
        }
    }
    return content;
}

可能没有缓冲I/O的速度快,但非常简洁:

    String content;
    try (Scanner scanner = new Scanner(textFile).useDelimiter("\\Z")) {
        content = scanner.next();
    }

\Z
模式告诉
扫描仪
分隔符是EOF。

以下是一个简单的解决方案:

String content;

content = new String(Files.readAllBytes(Paths.get("sample.txt")));

在Java中,从文件读取数据的最简单方法是使用file类读取文件,使用Scanner类读取文件内容

public static void main(String args[])throws Exception
{
   File f = new File("input.txt");
   takeInputIn2DArray(f);
}

public static void takeInputIn2DArray(File f) throws Exception
{
    Scanner s = new Scanner(f);
    int a[][] = new int[20][20];
    for(int i=0; i<20; i++)
    {
        for(int j=0; j<20; j++)
        {
            a[i][j] = s.nextInt();
        }
    }
}
Scanner in = new Scanner(new File("filename.txt"));

while (in.hasNext()) { // Iterates each line in the file
    String line = in.nextLine();
    // Do something with line
}

in.close(); // Don't forget to close resource leaks
publicstaticvoidmain(字符串args[])引发异常
{
文件f=新文件(“input.txt”);
takeInputIn2DArray(f);
}
公共静态void takeInputIn2DArray(文件f)引发异常
{
扫描器s=新扫描器(f);
int a[][]=新int[20][20];

对于(int i=0;i对于基于JSF的Maven web应用程序,只需使用ClassLoader和
Resources
文件夹即可读取所需的任何文件:

  • 将要读取的任何文件放入Resources文件夹中
  • 将Apache Commons IO依赖项放入POM中:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    
  • 您可以使用BufferedReader对文本文件、属性文件、架构等执行相同的操作。

    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    BufferedReader br;
    try {
        br = new BufferedReader(new FileReader("/fileToRead.txt"));
        try {
            String x;
            while ( (x = br.readLine()) != null ) {
                // Printing out each line in the file
                System.out.println(x);
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
    catch (FileNotFoundException e) {
        System.out.println(e);
        e.printStackTrace();
    }
    

    这基本上与Jesus Ramos的答案完全相同,只是使用了File而不是FileReader加上迭代来逐步浏览文件内容

    public static void main(String args[])throws Exception
    {
       File f = new File("input.txt");
       takeInputIn2DArray(f);
    }
    
    public static void takeInputIn2DArray(File f) throws Exception
    {
        Scanner s = new Scanner(f);
        int a[][] = new int[20][20];
        for(int i=0; i<20; i++)
        {
            for(int j=0; j<20; j++)
            {
                a[i][j] = s.nextInt();
            }
        }
    }
    
    Scanner in = new Scanner(new File("filename.txt"));
    
    while (in.hasNext()) { // Iterates each line in the file
        String line = in.nextLine();
        // Do something with line
    }
    
    in.close(); // Don't forget to close resource leaks
    
    …抛出
    FileNotFoundException

    如果这是为了简化结构,请使用:

    import static kiss.API.*;
    
    class App {
      void run() {
        String line;
        try (Close in = inOpen("file.dat")) {
          while ((line = readLine()) != null) {
            println(line);
          }
        }
      }
    }
    
    为此提供一个衬里:

    import com.google.common.base.Charsets;
    import com.google.common.io.Files;
    
    String contents = Files.toString(filePath, Charsets.UTF_8);
    

    我必须对不同的方法进行基准测试。我将对我的发现进行评论,但简言之,最快的方法是在FileInputStream上使用普通的旧BufferedInputStream。如果必须读取许多文件,那么三个线程将使总执行时间减少大约一半,但添加更多线程将逐渐降低性能,直到达到要求为止完成二十个线程所需的时间是完成一个线程所需时间的三倍

    假设您必须读取一个文件并对其内容执行一些有意义的操作。在这里的示例中,从日志中读取行并计算包含超过某个阈值的值的行数。因此,我假设一行Java 8
    Files.lines(path.get(“/path/to/file.txt”)).map(line->line.split(“;”)
    不是一个选项

    我在Java1.8、Windows7以及SSD和HDD驱动器上进行了测试

    我编写了六种不同的实现:

    rawParse:在FileInputStream上使用BufferedInputStream,然后逐字节剪切读取行。这比任何其他单线程方法都要好,但对于非ASCII文件来说可能非常不方便

    lineReaderParse:在文件阅读器上使用BufferedReader,逐行读取,通过调用String.split()拆分行。这大约比rawParse慢20%

    lineReaderParseParallel:这与lineReaderParse相同,但它使用多个线程。这是所有情况下最快的选项

    nioFilesParse:使用java.nio.files.files.lines()

    NIOSyncParse:使用带有完成处理程序和线程池的AsynchronousFileChannel

    nioMemoryMappedParse:使用
    rawParse                11.10 sec
    lineReaderParse         13.86 sec
    lineReaderParseParallel  6.00 sec
    nioFilesParse           13.52 sec
    nioAsyncParse           16.06 sec
    nioMemoryMappedParse    37.68 sec
    
    public void rawParse(final String targetDir, final int numberOfFiles) throws IOException, ParseException {
        overrunCount = 0;
        final int dl = (int) ';';
        StringBuffer lineBuffer = new StringBuffer(1024);
        for (int f=0; f<numberOfFiles; f++) {
            File fl = new File(targetDir+filenamePreffix+String.valueOf(f)+".txt");
            FileInputStream fin = new FileInputStream(fl);
            BufferedInputStream bin = new BufferedInputStream(fin);
            int character;
            while((character=bin.read())!=-1) {
                if (character==dl) {
    
                    // Here is where something is done with each line
                    doSomethingWithRawLine(lineBuffer.toString());
                    lineBuffer.setLength(0);
                }
                else {
                    lineBuffer.append((char) character);
                }
            }
            bin.close();
            fin.close();
        }
    }
    
    public final void doSomethingWithRawLine(String line) throws ParseException {
        // What to do for each line
        int fieldNumber = 0;
        final int len = line.length();
        StringBuffer fieldBuffer = new StringBuffer(256);
        for (int charPos=0; charPos<len; charPos++) {
            char c = line.charAt(charPos);
            if (c==DL0) {
                String fieldValue = fieldBuffer.toString();
                if (fieldValue.length()>0) {
                    switch (fieldNumber) {
                        case 0:
                            Date dt = fmt.parse(fieldValue);
                            fieldNumber++;
                            break;
                        case 1:
                            double d = Double.parseDouble(fieldValue);
                            fieldNumber++;
                            break;
                        case 2:
                            int t = Integer.parseInt(fieldValue);
                            fieldNumber++;
                            break;
                        case 3:
                            if (fieldValue.equals("overrun"))
                                overrunCount++;
                            break;
                    }
                }
                fieldBuffer.setLength(0);
            }
            else {
                fieldBuffer.append(c);
            }
        }
    }
    
    public void lineReaderParse(final String targetDir, final int numberOfFiles) throws IOException, ParseException {
        String line;
        for (int f=0; f<numberOfFiles; f++) {
            File fl = new File(targetDir+filenamePreffix+String.valueOf(f)+".txt");
            FileReader frd = new FileReader(fl);
            BufferedReader brd = new BufferedReader(frd);
    
            while ((line=brd.readLine())!=null)
                doSomethingWithLine(line);
            brd.close();
            frd.close();
        }
    }
    
    public final void doSomethingWithLine(String line) throws ParseException {
        // Example of what to do for each line
        String[] fields = line.split(";");
        Date dt = fmt.parse(fields[0]);
        double d = Double.parseDouble(fields[1]);
        int t = Integer.parseInt(fields[2]);
        if (fields[3].equals("overrun"))
            overrunCount++;
    }
    
    public void lineReaderParseParallel(final String targetDir, final int numberOfFiles, final int degreeOfParalelism) throws IOException, ParseException, InterruptedException {
        Thread[] pool = new Thread[degreeOfParalelism];
        int batchSize = numberOfFiles / degreeOfParalelism;
        for (int b=0; b<degreeOfParalelism; b++) {
            pool[b] = new LineReaderParseThread(targetDir, b*batchSize, b*batchSize+b*batchSize);
            pool[b].start();
        }
        for (int b=0; b<degreeOfParalelism; b++)
            pool[b].join();
    }
    
    class LineReaderParseThread extends Thread {
    
        private String targetDir;
        private int fileFrom;
        private int fileTo;
        private DateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        private int overrunCounter = 0;
    
        public LineReaderParseThread(String targetDir, int fileFrom, int fileTo) {
            this.targetDir = targetDir;
            this.fileFrom = fileFrom;
            this.fileTo = fileTo;
        }
    
        private void doSomethingWithTheLine(String line) throws ParseException {
            String[] fields = line.split(DL);
            Date dt = fmt.parse(fields[0]);
            double d = Double.parseDouble(fields[1]);
            int t = Integer.parseInt(fields[2]);
            if (fields[3].equals("overrun"))
                overrunCounter++;
        }
    
        @Override
        public void run() {
            String line;
            for (int f=fileFrom; f<fileTo; f++) {
                File fl = new File(targetDir+filenamePreffix+String.valueOf(f)+".txt");
                try {
                FileReader frd = new FileReader(fl);
                BufferedReader brd = new BufferedReader(frd);
                while ((line=brd.readLine())!=null) {
                    doSomethingWithTheLine(line);
                }
                brd.close();
                frd.close();
                } catch (IOException | ParseException ioe) { }
            }
        }
    }
    
    public void nioFilesParse(final String targetDir, final int numberOfFiles) throws IOException, ParseException {
        for (int f=0; f<numberOfFiles; f++) {
            Path ph = Paths.get(targetDir+filenamePreffix+String.valueOf(f)+".txt");
            Consumer<String> action = new LineConsumer();
            Stream<String> lines = Files.lines(ph);
            lines.forEach(action);
            lines.close();
        }
    }
    
    
    class LineConsumer implements Consumer<String> {
    
        @Override
        public void accept(String line) {
    
            // What to do for each line
            String[] fields = line.split(DL);
            if (fields.length>1) {
                try {
                    Date dt = fmt.parse(fields[0]);
                }
                catch (ParseException e) {
                }
                double d = Double.parseDouble(fields[1]);
                int t = Integer.parseInt(fields[2]);
                if (fields[3].equals("overrun"))
                    overrunCount++;
            }
        }
    }
    
    public void nioAsyncParse(final String targetDir, final int numberOfFiles, final int numberOfThreads, final int bufferSize) throws IOException, ParseException, InterruptedException {
        ScheduledThreadPoolExecutor pool = new ScheduledThreadPoolExecutor(numberOfThreads);
        ConcurrentLinkedQueue<ByteBuffer> byteBuffers = new ConcurrentLinkedQueue<ByteBuffer>();
    
        for (int b=0; b<numberOfThreads; b++)
            byteBuffers.add(ByteBuffer.allocate(bufferSize));
    
        for (int f=0; f<numberOfFiles; f++) {
            consumerThreads.acquire();
            String fileName = targetDir+filenamePreffix+String.valueOf(f)+".txt";
            AsynchronousFileChannel channel = AsynchronousFileChannel.open(Paths.get(fileName), EnumSet.of(StandardOpenOption.READ), pool);
            BufferConsumer consumer = new BufferConsumer(byteBuffers, fileName, bufferSize);
            channel.read(consumer.buffer(), 0l, channel, consumer);
        }
        consumerThreads.acquire(numberOfThreads);
    }
    
    
    class BufferConsumer implements CompletionHandler<Integer, AsynchronousFileChannel> {
    
            private ConcurrentLinkedQueue<ByteBuffer> buffers;
            private ByteBuffer bytes;
            private String file;
            private StringBuffer chars;
            private int limit;
            private long position;
            private DateFormat frmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    
            public BufferConsumer(ConcurrentLinkedQueue<ByteBuffer> byteBuffers, String fileName, int bufferSize) {
                buffers = byteBuffers;
                bytes = buffers.poll();
                if (bytes==null)
                    bytes = ByteBuffer.allocate(bufferSize);
    
                file = fileName;
                chars = new StringBuffer(bufferSize);
                frmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                limit = bufferSize;
                position = 0l;
            }
    
            public ByteBuffer buffer() {
                return bytes;
            }
    
            @Override
            public synchronized void completed(Integer result, AsynchronousFileChannel channel) {
    
                if (result!=-1) {
                    bytes.flip();
                    final int len = bytes.limit();
                    int i = 0;
                    try {
                        for (i = 0; i < len; i++) {
                            byte by = bytes.get();
                            if (by=='\n') {
                                // ***
                                // The code used to process the line goes here
                                chars.setLength(0);
                            }
                            else {
                                    chars.append((char) by);
                            }
                        }
                    }
                    catch (Exception x) {
                        System.out.println(
                            "Caught exception " + x.getClass().getName() + " " + x.getMessage() +
                            " i=" + String.valueOf(i) + ", limit=" + String.valueOf(len) +
                            ", position="+String.valueOf(position));
                    }
    
                    if (len==limit) {
                        bytes.clear();
                        position += len;
                        channel.read(bytes, position, channel, this);
                    }
                    else {
                        try {
                            channel.close();
                        }
                        catch (IOException e) {
                        }
                        consumerThreads.release();
                        bytes.clear();
                        buffers.add(bytes);
                    }
                }
                else {
                    try {
                        channel.close();
                    }
                    catch (IOException e) {
                    }
                    consumerThreads.release();
                    bytes.clear();
                    buffers.add(bytes);
                }
            }
    
            @Override
            public void failed(Throwable e, AsynchronousFileChannel channel) {
            }
    };
    
    Files.lines(Paths.get("text.txt")).collect(Collectors.toList());
    
    package io;
    import java.io.*;
    public class ReadFromFile2 {
        public static void main(String[] args)throws Exception {
            File file = new File("C:\\Users\\pankaj\\Desktop\\test.java");
            BufferedReader br = new BufferedReader(new FileReader(file));
            String st;
            while((st=br.readLine()) != null){
                System.out.println(st);
            }
        }
    }
    
    package io;
    
    import java.io.File;
    import java.util.Scanner;
    
    public class ReadFromFileUsingScanner {
        public static void main(String[] args) throws Exception {
            File file = new File("C:\\Users\\pankaj\\Desktop\\test.java");
            Scanner sc = new Scanner(file);
            while(sc.hasNextLine()){
                System.out.println(sc.nextLine());
            }
        }
    }
    
    package io;
    import java.io.*;
    public class ReadingFromFile {
    
        public static void main(String[] args) throws Exception {
            FileReader fr = new FileReader("C:\\Users\\pankaj\\Desktop\\test.java");
            int i;
            while ((i=fr.read()) != -1){
                System.out.print((char) i);
            }
        }
    }
    
    package io;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class ReadingEntireFileWithoutLoop {
    
        public static void main(String[] args) throws FileNotFoundException {
            File file = new File("C:\\Users\\pankaj\\Desktop\\test.java");
            Scanner sc = new Scanner(file);
            sc.useDelimiter("\\Z");
            System.out.println(sc.next());
        }
    }
    
    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.IOException;
    
    public class InputReader{
    
        public static void main(String[] args)throws IOException{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String s="";
            while((s=br.readLine())!=null){
                System.out.println(s);
            }
        }
    }
    
    java InputReader < input.txt
    
    java InputReader < input.txt > output.txt
    
    new TextOf(new File("a.txt")).asString();
    
    String str = String.join("\n",Files.readAllLines(Paths.get("e:\\text.txt")));
    
    String str = new String(Files.readAllBytes(Paths.get("e:\\text.txt")), StandardCharsets.UTF_8);
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    
    public class ReadFile_Files_ReadAllBytes {
      public static void main(String [] pArgs) throws IOException {
        String fileName = "c:\\temp\\sample-10KB.txt";
        File file = new File(fileName);
    
        byte [] fileBytes = Files.readAllBytes(file.toPath());
        char singleChar;
        for(byte b : fileBytes) {
          singleChar = (char) b;
          System.out.print(singleChar);
        }
      }
    }
    
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class ReadFile_BufferedReader_ReadLine {
      public static void main(String [] args) throws IOException {
        String fileName = "c:\\temp\\sample-10KB.txt";
        FileReader fileReader = new FileReader(fileName);
    
        try (BufferedReader bufferedReader = new BufferedReader(fileReader)) {
          String line;
          while((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
          }
        }
      }
    }
    
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.util.stream.Stream;
    
    public class ReadFile_Files_Lines {
      public static void main(String[] pArgs) throws IOException {
        String fileName = "c:\\temp\\sample-10KB.txt";
        File file = new File(fileName);
    
        try (Stream linesStream = Files.lines(file.toPath())) {
          linesStream.forEach(line -> {
            System.out.println(line);
          });
        }
      }
    }
    
    Path path = Paths.get("/myfolder/myfile.ext");
    try (BufferedReader reader = Files.newBufferedReader(path)) {
        // Read from the stream
        String currentLine = null;
        while ((currentLine = reader.readLine()) != null)
            //do your code here
    } catch (IOException e) {
        // Handle file I/O exception...
    }
    
    BufferedReader reader = Files.newBufferedReader(path);
    
    BufferedReader br = new BufferedReader(new FileReader("/myfolder/myfile.ext"));
    
    String fileName = 'yourFileFullNameWithPath';
    File file = new File(fileName); // Creates a new file object for your file
    FileReader fr = new FileReader(file);// Creates a Reader that you can use to read the contents of a file read your file
    BufferedReader br = new BufferedReader(fr); //Reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.
    
    BufferedReader br = new BufferedReader(new FileReader("file.txt")); // Optional
    
    try {
            StringBuilder sb = new StringBuilder();
            String line = br.readLine();
    
            while (line != null) {
            sb.append(line);
            sb.append(System.lineSeparator());
            line = br.readLine();
            }
            String everything = sb.toString();
            } finally {
            br.close();
        }
    
    import java.util.stream.Stream;
    import java.nio.file.*;
    import java.io.*;
    
    class ReadFile {
    
     public static void main(String[] args) {
    
        String filename = "Test.txt";
    
        try(Stream<String> stream = Files.lines(Paths.get(filename))) {
    
              stream.forEach(System.out:: println);
    
        } catch (IOException e) {
    
            e.printStackTrace();
        }
    
     }
    
     }
    
    try {
      File f = new File("filename.txt");
      Scanner r = new Scanner(f);  
      while (r.hasNextLine()) {
        String data = r.nextLine();
        JOptionPane.showMessageDialog(data);
      }
      r.close();
    } catch (FileNotFoundException ex) {
      JOptionPane.showMessageDialog("Error occurred");
      ex.printStackTrace();
    }
    
    import java.io.*;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    public class App {
        public static void main(String args[]) throws IOException {
            String content = Files.readString(Paths.get("D:\\sandbox\\mvn\\my-app\\my-app.iml"));
            System.out.print(content);
        }
    }