按行读取文件(不乱码)
1.第一种方式
public class writetest {
public static void main(String[] args) {
List<String> list=new ArrayList<>();
File filepath = new File("f://write/test.txt");
if(filepath.isFile()&&filepath.exists())
{
InputStreamReader read = null;
try {
read = new InputStreamReader(new FileInputStream(filepath),"gbk");
BufferedReader reader=new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null)
{
list.add(line);
}
read.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2.第二种方式
String con="";
try {
BufferedReader in=new BufferedReader(new InputStreamReader
(new FileInputStream("F://3d50fbd8-2374-4cd7-a021-b29c32e4117f.txt"),"utf-8"));
String str=null;
while ((str=in.readLine())!=null){
con=con+str;
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}