如何在FileReader周围包装BufferedReader?

时间:2013-01-23 20:46:59

标签: java bufferedreader filereader

如何在readFileFixed方法中围绕FileReader实现/包装BufferedReader?我还需要研究一下:

  1. 变量初始化。
  2. 异常处理
  3. 关闭阅读器
  4. 我如何阅读文件。
  5. 任何提示都会非常感激。

    package codeTest;
    
    
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    import java.util.Scanner;
    
    public class CodeTestExerciseBadCode {
    
        private static final String testFilename = "CodeTestExerciseBadCode.java";
    
        public CodeTestExerciseBadCode() {}
    
    
        public static void main(String[] args) throws IOException {
            CodeTestExerciseBadCode part2 = new CodeTestExerciseBadCode();
    
            System.out.println(part2.readFileFixed());
        }
    
        public String readFile() {
            File f = null;
            FileReader fr = null;
            StringBuffer content = null;
    
            try {
            f = new File(testFilename);
            fr = new FileReader(f);
    
            int c;
    
                while ((c = fr.read()) != -1) {
                    if (content == null) {
                    content = new StringBuffer();
                    }
    
                content.append((char) c);
                }
    
            fr.close();
            } catch (Exception e) {
                // Error message along with the cause of the Error
                throw new RuntimeException("An error occured reading your file", e);
            }
    
        return content.toString();
        }
    
        public String readFileFixed() throws IOException {
            StringBuffer content = null;
            File file = new File(testFilename);
    
            // I added this as a test. If directory name is provided then no issues
            if (file.isFile() && (file.length() > 0)) {
    
                // Placed inside the if() block, to avoid creating it unnecessarily if not needed
                content = new StringBuffer();
    
                try {
                    Scanner scanner = new Scanner(file);
    
                    while (scanner.hasNextLine()) {
                        content.append(scanner.nextLine());
                        content.append(System.getProperty("line.separator"));
                    }
    
                } catch (Exception e) {
                    // Throw an exception instead of printing the stack trace
                    throw new IOException("An error occured reading your file", e);
                }
            }
    
            return((content == null)
            ? ""
            : content.toString());
        }
    }
    

1 个答案:

答案 0 :(得分:2)

的内容
String s;
File file = new File("file.txt");
StringBuilder content = new StringBuilder( (int) file.length());
BufferedReader r = new BufferedReader(new FileReader(file));
try {
  while ( (s = r.readLine()) != null ) {
    content.append(s);
    content.append('\n');
} finally {
    r.close();
}

此外,在此网页的右侧,您会看到许多可能有用的链接。