基本 IO

操作流程

JavaIO操作也是有相应步骤的,以文件操作为例,主要的操作流程如下:

使用File类打开一个文件

通过字节流或字符流的子类,指定输出的位置

进行读/写操作

关闭输入/输出

 

InputStream 和OutputStream,两个是为字节流设计的,主要用来处理字节或二进制对象,
Reader和 Writer.两个是为字符流(一个字符占两个字节)设计的,主要用来处理字符或字符串.

 

将byte[]类型转化成String类型不可以用 b.toString(),应该使用String s=new String(b,offset,len,"charsetName")。

 

使用InputStream读文件:

 1 void inputStreamTest() {
 2         File file = new File("e:\abcdata.txt");
 3         // InputStream 读取的是字节流,读取中文会乱码。
 4         // 中文utf-8占用3个字节,gbk占用2个字节,InputStream读取中文时可能会隔断中文编码
 5         InputStream inputStream = null;
 6         byte[] b = new byte[1024];
 7         int len;
 8         StringBuilder sb = new StringBuilder();
 9         try {
10             inputStream = new FileInputStream(file);
11             while ((len = inputStream.read(b, 0, b.length)) != -1) {
12                 String s = new String(b, 0, len, "UTF-8"); 
13                 sb.append(s);
14             }
15             System.out.println(sb);
16         } catch (FileNotFoundException e) {
17             // TODO Auto-generated catch block
18             e.printStackTrace();
19         } catch (IOException e) {
20             // TODO Auto-generated catch block
21             e.printStackTrace();
22         } finally {
23             if (inputStream != null) {
24                 try {
25                     inputStream.close();
26                 } catch (IOException e) {
27                     // TODO Auto-generated catch block
28                     e.printStackTrace();
29                 }
30             }
31         }
32     }

使用OutputStream写文件:

 1 void outputStreamTest() {
 2         // 写入中文不会乱码,因为一次性写入时不会像InputStream那样把中文编码隔断。
 3         File file = new File("e:\abcdata.txt");
 4         OutputStream os = null;
 5         String string = "测试中文";
 6         try {
 7             //写入新内容时覆盖原文件内容
 8             // os = new FileOutputStream(file);
 9             // 写入新内容时自动追加到原内容末尾
10             os = new FileOutputStream(file, true);
11             os.write(string.getBytes());
12         } catch (FileNotFoundException e) {
13             // TODO Auto-generated catch block
14             e.printStackTrace();
15         } catch (IOException e) {
16             // TODO Auto-generated catch block
17             e.printStackTrace();
18         } finally {
19             if (os != null) {
20                 try {
21                     os.close();
22                 } catch (IOException e) {
23                     // TODO Auto-generated catch block
24                     e.printStackTrace();
25                 }
26             }
27         }
28 
29     }

Reader 读取文件:

 1 void readerTest() {
 2         // 读取中文不会乱码,因为读取的char字符格式
 3         File file = new File("e:\test.txt");
 4         Reader reader = null;
 5         char[] ch = new char[24];
 6         StringBuilder sb = new StringBuilder();
 7         try {
 8             reader = new FileReader(file);
 9             while (reader.read(ch) != -1) {
10                 // System.out.println(ch);
11                 sb.append(ch);
12             }
13             System.out.println(sb);
14         } catch (FileNotFoundException e) {
15             // TODO Auto-generated catch block
16             e.printStackTrace();
17         } catch (IOException e) {
18             // TODO Auto-generated catch block
19             e.printStackTrace();
20         } finally {
21             if (reader != null) {
22                 try {
23                     reader.close();
24                 } catch (IOException e) {
25                     // TODO Auto-generated catch block
26                     e.printStackTrace();
27                 }
28             }
29         }
30     }

Writer写入文件:

 1 void writerTest() {
 2         // 如果文件不存在会自动创建,但不能是多级目录,因此十分有必要先检测文件目录是否存在
 3         File file = new File("e:\test.txt");
 4         // if (!file.exists()) {
 5         // file.mkdirs();
 6         // }
 7         Writer writer = null;
 8         try {
 9             // writer = new FileWriter(file);
10             // 写入时是否在文件末尾追加
11             writer = new FileWriter(file, true);
12             writer.write("dfl");
13         } catch (IOException e) {
14             // TODO Auto-generated catch block
15             e.printStackTrace();
16         } finally {
17             if (writer != null) {
18                 try {
19                     writer.close();
20                 } catch (IOException e) {
21                     // TODO Auto-generated catch block
22                     e.printStackTrace();
23                 }
24             }
25         }
26     }

 

原文地址:https://www.cnblogs.com/mada0/p/4705011.html