Echemos un vistazo a un ejemplo primero
import java.io.*; /*** Creado por Liguoqing el 2016/3/28.*/Public class ReadTxtfile {public static void readTxt (String filePath) {try {file file = new File (filePath); if (file.isfile () && file.exists ()) {inputStreamReader isr = new InputStreamReader (new FileInputStream (File), "UTF-8"); BufferedReader BR = New BufferedReader (ISR); Cadena linetxt = null; while ((linetxt = br.readline ())! = null) {system.out.println (linetxt); } br.close (); } else {System.out.println ("El archivo no existe!"); }} catch (Exception e) {System.out.println ("¡Error de lectura de archivo!"); }} public static void main (string [] args) {String filePath = "d: //test//i.txt"; readTxt (filepath); }}Después de leer el ejemplo anterior, estudiemos en detalle
Java lee el contenido del archivo TXT. Se puede entender de la siguiente manera:
Primero obtenga un mango de archivo. Archivo archivo = nuevo archivo (); El archivo es el mango del archivo. Los dos tienen una conexión de red telefónica. A continuación, puede comenzar a hacer llamadas.
Lea la información de la parte A a través de esta línea: nuevo FileInputStream (archivo) Actualmente, esta información se ha leído en la memoria. A continuación, debe ser interpretado como algo que el Partido B puede entender
Ya que ha usado fileInputStream (). Luego, el método correspondiente debe usarse para interpretar los datos cargados en la memoria en este momento.
Después de completar la interpretación, debe obtenerla. Por supuesto, debe convertirse en datos que IO puede reconocer. Luego debe llamar al método BufferedReader () que lee ByTecode. Al mismo tiempo, use el método Readline () de BufferedReader () para leer cada línea de datos en el archivo TXT.
paquete com.campu; import java.io.bufferedInputStream; import java.io.bufferedReader; import java.io.file; import java.io.fileInputStream; import java.io.inputstreamreader; import java.io.reader; /** * @author coder Xiaojiang* H20121012.java * 2012-10-12 11:40:21 pm */ public class H20121012 { /** * Function: Java reads the contents of txt files* Steps: 1: Get the file handle first* 2: Obtaining the file handle is used to input a byte code stream, and the input stream needs to be read * 3: After reading the input stream, the El flujo de byte generado debe leerse * 4: salida de una línea por una. Readline (). * Nota: Lo que debe considerarse son excepciones * @param filepath */ public static void readtxtfile (string filepath) {try {string coding = "gbk"; Archivo archivo = nuevo archivo (filepath); if (file.isfile () && file.exists ()) {// juzga si el archivo existe inputStreamReader read = new InputStreamReader (new FileInputStream (archivo), codificación); // Considere el formato de codificación BufferedReader Buffereder = new BufferedReader (Read); Cadena linetxt = null; while ((linetxt = bufferedReader.readline ())! = null) {system.out.println (linetxt); } read.close (); } else {System.out.println ("no se puede encontrar el archivo especificado"); }} catch (Exception e) {System.out.println ("Error de lectura de contenido de archivo"); E.PrintStackTrace (); }} public static void main (string argv []) {String filePath = "l: //apache//htdocs//res//20121012.txt"; // "res/"; readTxtFile (FilePath); }}