“文本IO”的版本间的差异
(→使用try-with-resource自动关闭资源) |
(→用法二:添加异常处理语句) |
||
(未显示同一用户的1个中间版本) | |||
第48行: | 第48行: | ||
FileWriter(String name, boolean append) 直接使用文件名或路径创建文件输出流对象,并由参数append指定是否追加,异常情况同上</nowiki> | FileWriter(String name, boolean append) 直接使用文件名或路径创建文件输出流对象,并由参数append指定是否追加,异常情况同上</nowiki> | ||
− | === | + | ==文本读写示例== |
+ | ===用法一:直接使用 === | ||
+ | <nowiki>package main; | ||
+ | import java.io.*; | ||
+ | import java.util.*; | ||
+ | /*本类对应实训: */ | ||
+ | public class MainClass { | ||
+ | public static void main(String[] args) throws IOException { | ||
+ | |||
+ | FileWriter fw = new FileWriter("reader.txt"); | ||
+ | String str = "欢迎来到山东商职"; | ||
+ | fw.write(str.toCharArray()); | ||
+ | fw.close(); | ||
+ | |||
+ | FileReader fr= new FileReader("reader.txt"); | ||
+ | char[] c=new char[20]; | ||
+ | int hasRead=0; | ||
+ | while((hasRead=fr.read(c))!=-1) { | ||
+ | System.out.print(new String(c,0,hasRead)); | ||
+ | } | ||
+ | fr.close(); | ||
+ | } | ||
+ | }</nowiki> | ||
+ | ===用法二:添加异常处理语句 === | ||
+ | <nowiki>try{ | ||
+ | FileWriter fw = new FileWriter("reader.txt"); | ||
+ | String str = "欢迎来到山东商职"; | ||
+ | fw.write(str.toCharArray()); | ||
+ | fw.close(); | ||
+ | }catch (Exception e) { | ||
+ | System.out.println("出现异常:"+e); | ||
+ | //e.printStackTrace(); | ||
+ | } | ||
+ | |||
+ | try{ | ||
+ | FileReader fr= new FileReader("reader.txt"); | ||
+ | char[] c=new char[20]; | ||
+ | int hasRead=0; | ||
+ | while((hasRead=fr.read(c))!=-1) { | ||
+ | System.out.print(new String(c,0,hasRead)); | ||
+ | } | ||
+ | fr.close(); | ||
+ | }catch(Exception e){ | ||
+ | System.out.println("出现异常:"+e); | ||
+ | } | ||
+ | </nowiki> | ||
+ | |||
+ | ===用法三:使用try-with-resource自动关闭资源=== | ||
在编写IO操作时经常会忘记关闭IO流,JDK7提供了新的try-with-resource语法来自动关闭IO流,语法格式如下: | 在编写IO操作时经常会忘记关闭IO流,JDK7提供了新的try-with-resource语法来自动关闭IO流,语法格式如下: | ||
第63行: | 第110行: | ||
String str = "欢迎来到山东商职"; | String str = "欢迎来到山东商职"; | ||
fw.write(str.toCharArray()); | fw.write(str.toCharArray()); | ||
− | }catch ( | + | }catch(Exception e){ |
− | + | System.out.println("出现异常:"+e); | |
} | } | ||
//把内容显示在屏幕上 | //把内容显示在屏幕上 | ||
第73行: | 第120行: | ||
System.out.print(new String(c,0,hasRead)); | System.out.print(new String(c,0,hasRead)); | ||
} | } | ||
− | }catch( | + | }catch(Exception e){ |
− | + | System.out.println("出现异常:"+e); | |
− | + | } | |
}</nowiki> | }</nowiki> | ||
2018年4月26日 (四) 07:58的最新版本
目录
Reader和Writer
Java IO中的java.io.Reader和java.io.Writer的工作原理很像InputStream和OutputStream,但不同的是reader和writer是基于字符的,他们是用来用些文本数据的。其类层级结构如图6-13所示。
图6-13 字符流结构
Reader用于读取字符流的抽象类。Reader类的常用方法如表6-7所示。
表6-7 Reader类常用方法 方法名 说明 boolean ready() 输入字符流是否可读 int read() 读取一个字符 int read(char[] cbuf) 读取一串字符(到字符数组cbuf) long skip(long n) 跳过n个字符 mark(int readAheadLimit) 在当前位置做一标记 reset() 将读取位置恢复到标记处 close() 关闭字符流
Writer是写入字符流的抽象类。Writer类的常用方法如表6-8所示。
表6-8 Writer类常用方法
方法名 说明 void close() 关闭流 void flush() 强行写入 void write(int c) 写入c void write(char[] cbuf) 写入字符数组cbuf void write(char[] cbuf, int off, int len) 写入字符数组cbuf中自位置off开始的len个字符 void write(String str) 写入字符串str void write(String str, int off, int len) 写入字符串str中自位置off开始的len个字符
FileReader和FileWriter
FileReader:与FileInputStream对应,主要用来读取字符文件,使用缺省的字符编码,常用的构造函数如表6-9所示。
表6-9 FileReader类构造方法 构 造 方 法 说 明
FileReader(File file) 使用File对象创建文件输入流对象,如果文件打开失败,将抛出异常 FileReader(String name) 使用文件名或路径创建文件输入流对象,如果文件打开失败,将抛出异常 FileWrite:与FileOutputStream对应 ,将字符类型数据写入文件,使用缺省字符编码和缓冲器大小,常用的构造函数如表6-10所示。
表6-10 FileWriter类构造方法
构 造 方 法 说 明 FileWriter(File file) 使用File对象创建文件输出流对象,如果文件打开失败,将抛出异常,必须捕捉 FileWriter(File file, boolean append) 使用File对象创建文件输出流对象,并由参数append指定是否追加,异常情况同上 FileWriter(String name) 直接使用文件名或路径创建文件输出流对象,异常情况同上 FileWriter(String name, boolean append) 直接使用文件名或路径创建文件输出流对象,并由参数append指定是否追加,异常情况同上
文本读写示例
用法一:直接使用
package main; import java.io.*; import java.util.*; /*本类对应实训: */ public class MainClass { public static void main(String[] args) throws IOException { FileWriter fw = new FileWriter("reader.txt"); String str = "欢迎来到山东商职"; fw.write(str.toCharArray()); fw.close(); FileReader fr= new FileReader("reader.txt"); char[] c=new char[20]; int hasRead=0; while((hasRead=fr.read(c))!=-1) { System.out.print(new String(c,0,hasRead)); } fr.close(); } }
用法二:添加异常处理语句
try{ FileWriter fw = new FileWriter("reader.txt"); String str = "欢迎来到山东商职"; fw.write(str.toCharArray()); fw.close(); }catch (Exception e) { System.out.println("出现异常:"+e); //e.printStackTrace(); } try{ FileReader fr= new FileReader("reader.txt"); char[] c=new char[20]; int hasRead=0; while((hasRead=fr.read(c))!=-1) { System.out.print(new String(c,0,hasRead)); } fr.close(); }catch(Exception e){ System.out.println("出现异常:"+e); }
用法三:使用try-with-resource自动关闭资源
在编写IO操作时经常会忘记关闭IO流,JDK7提供了新的try-with-resource语法来自动关闭IO流,语法格式如下:
try(声明和创建资源){ 使用资源来处理文件 }
下面程序使用FileReader和Writer将“Hello World”写入文件reader.txt,并读取打印出来。
public static void main(String[] args) throws IOException { //把内容写入到文件中 try(FileWriter fw = new FileWriter("reader.txt");){ String str = "欢迎来到山东商职"; fw.write(str.toCharArray()); }catch(Exception e){ System.out.println("出现异常:"+e); } //把内容显示在屏幕上 try(FileReader fr= new FileReader("reader.txt")){ char[] c=new char[20]; int hasRead=0; while((hasRead=fr.read(c))!=-1) { System.out.print(new String(c,0,hasRead)); } }catch(Exception e){ System.out.println("出现异常:"+e); } }
BufferedReader和BufferedWriter
使用FileRead和FileWriter从文件中逐个地读取/写入字符,效率比较低下,因此一般将该类对象包装到缓冲流(BufferedReader和BufferedWriter)中进行操作。BufferedReader和BufferedWriter与BufferedInputStream和BufferOutputStream操作类似,使用缓冲方式文本读取/写入,初次之外还提供了readLine()和newLine()方法,从而提高了读写的效率。
下面程序使用BufferedReader和BufferedWriter完成文件的写入和读取。
public static void main(String[] args) throws IOException { // 把内容写入到文件中 try (FileWriter fw = new FileWriter("reader.txt"); BufferedWriter bw = new BufferedWriter(fw);) { String str = "hello world"; bw.write(str); bw.newLine(); bw.write("good good study"); } // 把内容显示在屏幕上 try (FileReader fr = new FileReader("reader.txt"); BufferedReader br = new BufferedReader(fr)) { while (br.ready()) { System.out.println(br.readLine()); } } }
返回 Java程序设计