读取指定目录下的txt文件内容


读取指定txt文件内容

package file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

public class DearTextFile {

	public static void main(String[] args) {
		try {
			dearTxt("E:\\-----------------------------学习资料\\ddd.txt", "gbk");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void dearTxt(String filePath, String encoding) throws Exception {
		InputStream is = new FileInputStream(new File(filePath));
		BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding));
		String line = "";
		//读一行,输出一行
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
	}

}

你可能感兴趣的:(txt)