文件读取时是乱码 基本都是问号 为什么

qq_az1x9bfu Java本地文件操作 最后由 极客学院_吕布 于2017年08月28日回复

  • 1 回答
  • 920 浏览
package com.jikexueyuan.hell.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;



public class FileDemo06 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		File file = new File("输出流.txt");
        if(file.exists()) {
        	System.out.println("此文件存在");
			try {
				FileInputStream fis = new FileInputStream(file);
				InputStreamReader isr = new InputStreamReader(fis,"UTF-8");
				BufferedReader br = new BufferedReader(isr);
				String line;
				
			while((line = br.readLine()) != null) {
				System.out.println(line);
			}
			br.close();
			isr.close();
			fis.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

	}

}