Java - 缓冲读取

时间:2013-11-28 17:32:01

标签: java

我正在为自己制作一个程序,它显示了在文本文件中阅读的所有不同方法。我使用过FileWriter和BufferedWriter。

我的问题是为什么FileWriter变量需要包装在BufferedWriter中。我在代码中看到了很多,在某些情况下,例如int包装在Integer中。这对我来说很有意义,但是将FileWriter包装在BufferedWriter中是什么收获。

    **FileWriter fWriter = new FileWriter(fileName);
    BufferedWriter bW = new BufferedWriter(fWriter);**

我在这个网站上找到了下面的代码,我通读了评论和答案,发现它有点帮助

我认为这部分令人困惑的是文件有很多不同的缓冲读取。这是读取文件的最佳方式,为什么必须将它们包装在其他对象中。它可以方法支持吗?它是否刷新缓冲区?它会提高速度吗? (可能不是)将文件包装在缓冲区中有一些很大的优势。

    FileWriter fWriter = new FileWriter(fileName);
    BufferedWriter bW  = new BufferedWriter(fWriter);

在上面的代码中,使用文件名创建了fWriter。然后将fWriter变量“包装”到BufferedWriter bW变量中。将FileWriter包装到BufferedWriter中的目的是什么。

  ---------------------------------------------------------
  -        FileWriter fw = new FileWriter (file);         -
  -        BufferedWriter bw = new BufferedWriter (fw);   - 
  -        PrintWriter outFile = new PrintWriter (bw);    - 
  ---------------------------------------------------------

以下是我的完整文件程序。我只是对缓冲的包装器有一个问题,但我认为如果有人想编译并运行那么我会发布它然后他们应该没有任何麻烦。

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Scanner;

public class DDHExampleTextFileReader {
    List<String> lines = new ArrayList<String>();
    final static String FILE_NAME = "G:testFile.txt";
    final static String OUTPUT_FILE_NAME = "G:output.txt";
    final static Charset ENCODING = StandardCharsets.UTF_8;
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    public DDHExampleTextFileReader() {
       //Zero argument constructor
    }

    private void fileReadOne() {
        String fileName = "G:testFile.txt"; // The name of the file to open.
        String line = null; // This will reference one line at a time

        try {
            // FileReader reads text files in the default encoding.
            // FileReader is meant for reading streams of characters. 
            // For reading streams of raw bytes, consider using a FileInputStream.
            FileReader fileReader = new FileReader(fileName);
            // Always wrap FileReader in BufferedReader.
            BufferedReader bufferedReader = new BufferedReader(fileReader);

                while((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }   

            bufferedReader.close();             // Always close files.
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println("Error reading file '" + fileName + "'");    
            ex.printStackTrace();
            // Or we could just do this: 
            // ex.printStackTrace();
        }
    }

    private void fileReadTwo() {
         // The name of the file to open.
        String fileName = "G:testFile.txt";

        try {
            // Use this for reading the data.
            byte[] buffer = new byte[1000];

            FileInputStream inputStream = 
                new FileInputStream(fileName);

            // read fills buffer with data and returns
            // the number of bytes read (which of course
            // may be less than the buffer size, but
            // it will never be more).
            int total = 0;
            int nRead = 0;
            while((nRead = inputStream.read(buffer)) != -1) {
                // Convert to String so we can display it.
                // Of course you wouldn't want to do this with
                // a 'real' binary file.
                System.out.println(new String(buffer));
                total += nRead;
            }   

            // Always close files.
            inputStream.close();        

            System.out.println("Read " + total + " bytes");
        }
        catch(FileNotFoundException ex) {
            System.out.println(
                "Unable to open file '" + 
                fileName + "'");                
        }
        catch(IOException ex) {
            System.out.println(
                "Error reading file '" 
                + fileName + "'");                  
            // Or we could just do this: 
            // ex.printStackTrace();
        }
    }

    private void fileReadThree() {
        String content = null;
           File file = new File("G:testFile.txt"); //for ex foo.txt
           FileReader reader = null;
           try {
               reader = new FileReader(file);
               char[] chars = new char[(int) file.length()];
               reader.read(chars);
               content = new String(chars);
               System.out.println(content);
           } catch (IOException e) {
               e.printStackTrace();
           } finally {
               try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
           }
    }

    private void fileReadFour() {
       File f = new File("G:testFile.txt");
            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();
            }
           System.out.println(text);
        }

    //Doesn't keep file formatting
    private void fileReadfive() {
          Scanner s = null;
            try {
                try {
                    s = new Scanner(new BufferedReader(new FileReader("G:testFile.txt")));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                    while (s.hasNext()) {
                        System.out.println(s.next());
                    }
            } finally {
                if (s != null) {
                    s.close();
                }
            }
    }

    private void fileReadSumsTheFileOfNumbersScanner() {
        Scanner s = null;
        double sum = 0;

        try {
            try {
                s = new Scanner(new BufferedReader(new FileReader("G:n.txt")));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            s.useLocale(Locale.US);

            while (s.hasNext()) {
                if (s.hasNextDouble()) {
                    sum += s.nextDouble();
                } else {
                    s.next();
                }   
            }
        } finally {
            s.close();
        }
        System.out.println(sum);
    }

    private void fileWriterOneTextFile() {
        String fileName = "G:testTemp.txt";
        try {
            // Assume default encoding.
            /*
             Whether or not a file is available or may be created depends upon the underlying platform. Some platforms, in particular, 
             allow a file to be opened for writing by only one FileWriter (or other file-writing object) at a time. In such 
             situations the constructors in this class will fail if the file involved is already open. 
             FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a 
             FileOutputStream.
             */
            FileWriter fWriter = new FileWriter(fileName);
            // Always wrap FileWriter in BufferedWriter.
            /*
             The buffer size may be specified, or the default size may be accepted. The default is large enough for most purposes.
             A newLine() method is provided, which uses the platform's own notion of line separator as defined by the system 
             property line.separator. Not all platforms use the newline character ('\n') to terminate lines. Calling this method 
             to terminate each output line is therefore preferred to writing a newline character directly. 
             In general, a Writer sends its output immediately to the underlying character or byte stream. Unless prompt output 
             is required, it is advisable to wrap a BufferedWriter around any Writer whose write() operations may be costly, such as 
             FileWriters and OutputStreamWriters. For example, 
                PrintWriter out
                    = new PrintWriter(new BufferedWriter(new FileWriter("foo.out")));
             will buffer the PrintWriter's output to the file. Without buffering, each invocation of a print() method would cause 
             characters to be converted into bytes that would then be written immediately to the file, which can be very inefficient.
             */
            BufferedWriter bW = new BufferedWriter(fWriter);
            // Note that write() does not automatically
            // append a newline character.
            bW.write("This is a test string that will be written to the file!!!!");
            bW.write("This more text that will be written to the file!!!!");
            bW.newLine();
            bW.write("After the newLine bufferedWriter method.");
            bW.write(" A   B   C D E         F     G");
            bW.newLine();
            bW.write("Hauf");
            bW.close();
        } catch(IOException ex) {
            System.out.println("Error writing to file '" + fileName + "'");
            ex.printStackTrace();
        }
    }

     List<String> readSmallTextFile(String aFileName) throws IOException {
            Path path = Paths.get(aFileName);
            return Files.readAllLines(path, ENCODING);
          }

          void writeSmallTextFile(List<String> aLines, String aFileName) throws IOException {
            Path path = Paths.get(aFileName);
            Files.write(path, aLines, ENCODING);
          }

          //For larger files
          void readLargerTextFile(String aFileName) throws IOException {
            Path path = Paths.get(aFileName);
            try (Scanner scanner =  new Scanner(path, ENCODING.name())){
              while (scanner.hasNextLine()){
                //process each line in some way
                log(scanner.nextLine());
              }      
            }
          }

          void readLargerTextFileAlternate(String aFileName) throws IOException {
            Path path = Paths.get(aFileName);
            try (BufferedReader reader = Files.newBufferedReader(path, ENCODING)){
              String line = null;
              while ((line = reader.readLine()) != null) {
                //process each line in some way
                log(line);
              }      
            }
          }

          void writeLargerTextFile(String aFileName, List<String> aLines) throws IOException {
            Path path = Paths.get(aFileName);
            try (BufferedWriter writer = Files.newBufferedWriter(path, ENCODING)){
              for(String line : aLines){
                writer.write(line);
                writer.newLine();
              }
            }
          }

          private static void log(Object aMsg){
            System.out.println(String.valueOf(aMsg));
          }

    public static void main(String[] args) throws IOException {
        DDHExampleTextFileReader doug = new DDHExampleTextFileReader();
        List<String> lines = doug.readSmallTextFile(FILE_NAME);
        //doug.fileReadOne();
        //doug.fileReadTwo();
        //doug.fileReadThree();
        //doug.fileReadFour();
        //doug.fileReadfive();
        //doug.fileReadSumsTheFileOfNumbersScanner();
        doug.fileWriterOneTextFile();
        log(lines);
            lines.add("This is a line added in code.");
            doug.writeSmallTextFile(lines, FILE_NAME);

            doug.readLargerTextFile(FILE_NAME);
            lines = Arrays.asList("Down to the Waterline", "Water of Love");
            doug.writeLargerTextFile(OUTPUT_FILE_NAME, lines);



        System.out.println(String.valueOf("\n\n\n-----End of Main Method!!!------"));
    }
}


/*
public static void copy(Reader input, OutputStream output, String encoding)
                   throws IOException {
               if (encoding == null) {
                    copy(input, output);
                } else {
                    OutputStreamWriter out = new OutputStreamWriter(output, encoding);
                    copy(input, out);
                    // XXX Unless anyone is planning on rewriting OutputStreamWriter,
                    // we have to flush here.
                    out.flush();
                }
     }

    public static void copy(Reader input, OutputStream output)
                    throws IOException {
                OutputStreamWriter out = new OutputStreamWriter(output);
                copy(input, out);
                // XXX Unless anyone is planning on rewriting OutputStreamWriter, we
                // have to flush here.
                out.flush();
     }

     public static int copy(Reader input, Writer output) throws IOException {
                  //  long count = copyLarge(input, output);
                    //copy large
                    byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
                    long count = 0;
                    int n = 0;
                    while (-1 != (n = input.read())) {
                        output.write(0);
                        count += n;
                    }

                    //end copy large
                    if (count > Integer.MAX_VALUE) {
                        return -1;
                    }
                    return (int) count;
    }

    public static long copyLarge(InputStream i, OutputStream o) throws IOException {
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
            long count = 0;
            int n = 0;
            while (-1 != (n = i.read(buffer))) {
              o.write(buffer, 0, n);
              count += n;
            }
        return count;
    }

    public static String toString(InputStream input) throws IOException {
                    StringWriter sw = new StringWriter();
                     copy(input, sw);
                     return sw.toString();
    }

    public static void copy(InputStream input, Writer output) throws IOException {
              InputStreamReader in = new InputStreamReader(input);
          copy(in, output);
    }


    public static void copy(InputStream input, Writer output, String encoding) throws IOException {
                if (encoding == null) {
                    copy(input, output);
                } else {
                    InputStreamReader in = new InputStreamReader(input, encoding);
                    copy(in, output);
                }
     }

2 个答案:

答案 0 :(得分:4)

  

我的问题是为什么FileWriter变量需要包装在BufferedWriter中。

,但如果你这样做,你可能会获得更好的性能,这样你就可以避免经常进入磁盘。

此外,您使用的是BufferedWriter.newLine,而FileWriter未定义 - 虽然手动编写新行很容易。

我建议您使用FileOutputStream包裹的OutputStreamWriter,而不是直接使用FileWriter - 这样您可以指定应使用的编码,而不是只使用平台默认编码。

我还建议避免 PrintWriter因为它吞下了异常 - 这不是一个好主意,IMO。

答案 1 :(得分:3)

如您所知,缓冲区是一个临时内存位置,用于存储您正在处理的数据。在BufferedWriter和BufferedReader的情况下,您正在使用它来最小化对硬盘驱动器的写入或读取次数。

想想超市里发生的事情。你拿着一辆手推车走在货架上,用货物装满你的手推车。这个想法是到结账柜台一趟。假设您不使用手推车而是一次购买一件物品?你需要花很长时间才能买到杂货。

当你要求BufferedWriter将一些字节写入文件时,它实际上只是将它存储在缓冲区中。当缓冲区已满或刷新时,缓冲区中的所有内容都会写入文件系统。

Java IO框架使用Decorator design pattern。这意味着您可以在运行时构建适合您需要的编写器或阅读器。因此,如果您需要缓冲读卡器,请将任何读卡器包装在缓冲读卡器中。如果您需要其他功能,请使用适当的包装器来简单地添加您需要的功能。

输入或输出的核心发生在一个名为InputStream或OutputStream的类中。让我们以InputStream为例。 InputStream只读取字节。方法read()例如读取单个字节或read(byte [] b)可以读取字节数组。对于非常低级别的输入,这是一个有用的类。但是,每次调用read()时,都必须从硬盘驱动器中获取单个字节,这可能非常慢。因此,在需要从文件中读取多个字节的情况下,可以将InputStream包装在BufferedStream中,并获得缓冲区的好处。

InputStreamReader类是InputStream的包装器,它从字节到字符进行桥接。 InputStreamReader的read()方法读取单个字符。 FileReader类是InputStreamReader的包装器,它添加了从文件读取的功能。

因此,当您浏览java.io中的一组类时,您会看到每个类都添加了一些功能,您可以将各种对象包装在您想要的任何功能层中。