标准的输入输出流与转换流
标准的输入输出流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.io.*;
public class Test { public static void main(String[] args) { System.out.println(System.in.getClass());
System.out.println(System.out.getClass());
} }
|
转换流
可以将字节流转换为字符流,解决文件乱码的问题,默认情况下按照utf-8编码进行阅读的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import java.io.*;
public class Test { public static void main(String[] args) throws IOException { String filePath="D:\\文件下载\\hello.txt"; BufferedReader bufferedReader=new BufferedReader(new FileReader(filePath)); String line; while ((line=bufferedReader.readLine())!=null){ System.out.println(line); } bufferedReader.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import java.io.*;
public class Test { public static void main(String[] args) throws IOException { String filePath="D:\\文件下载\\hello.txt"; InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(filePath),"gbk"); BufferedReader bufferedReader=new BufferedReader(inputStreamReader); String line; while ((line=bufferedReader.readLine())!=null){ System.out.println(line); } bufferedReader.close(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| import java.io.*; import java.nio.charset.StandardCharsets;
public class Test { public static void main(String[] args) throws IOException { String filePath="D:\\文件下载\\hello.txt"; OutputStreamWriter outputStreamWriter=new OutputStreamWriter(new FileOutputStream(filePath), StandardCharsets.UTF_8); BufferedWriter bufferedWriter=new BufferedWriter(outputStreamWriter); bufferedWriter.write("你好啊"); bufferedWriter.close(); } }
|