Java Tutorial/File/FileReader

Материал из Java эксперт
Перейти к: навигация, поиск

FileReader

  1. The FileReader class is a subclass of InputStreamReader
  2. The FileReader class is a convenient class to read characters from a file.
  3. The FileReader class uses an encoding other than the default one.

To construct a FileReader object:



public FileReader (File file)
public FileReader (FileDescriptor fileDescriptor)
public FileReader (String filePath)





Loading text from a file

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
public class Main {
  public static void main(String[] argv) throws Exception {
    File f = new File("C:\\test.txt");
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
    StringBuffer sb = new StringBuffer();
    String eachLine = br.readLine();
    while (eachLine != null) {
      sb.append(eachLine);
      sb.append("\n");
      eachLine = br.readLine();
    }
    System.out.println(sb.toString());
  }
}





Read file upside down

import java.io.File;
import java.io.FileReader;
public class Main {
  public static void main(String[] args) throws Exception {
    File f = new File("hello.txt");
    FileReader fr = new FileReader(f);
    char[] c = new char[(int) f.length()];
    char[] cnew = new char[(int) f.length()];
    StringBuffer sbuf = new StringBuffer();
    fr.read(c, 0, (int) f.length());
    int len = (int) f.length();
    for (int i = 0, j = len - 1; i < len; i++, j--) {
      cnew[i] = c[j];
      sbuf.append(cnew[i]);
    }
    System.out.println(sbuf.toString());
    fr.close();
  }
}





Read the file one buffer at a time

import java.io.FileReader;
public class Main {
  public static void main(String[] argv) throws Exception {
    FileReader fr = new FileReader("text.txt");
    int count;
    char chrs[] = new char[80];
    do {
      count = fr.read(chrs);
      for (int i = 0; i < count; i++)
        System.out.print(chrs[i]);
    } while (count != -1);
  }
}





Use a FileReader to display a text file.

import java.io.FileReader;
public class Main {
  public static void main(String[] argv) throws Exception {
    FileReader fr = new FileReader("text.txt");
    int ch;
    do {
      ch = fr.read();
      if (ch != -1)
        System.out.println((char) ch);
    } while (ch != -1);
    fr.close();
  }
}





Wrap FileReader with BufferedReader

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class SourceReader {
  public static void main(String[] arguments) {
    try {
      FileReader file = new FileReader("SourceReader.java");
      BufferedReader buff = new BufferedReader(file);
      boolean eof = false;
      while (!eof) {
        String line = buff.readLine();
        if (line == null)
          eof = true;
        else
          System.out.println(line);
      }
      buff.close();
    } catch (IOException e) {
      System.out.println("Error - " + e.toString());
    }
  }
}