本帖最后由 noiary 于 2014-10-16 23:15 编辑
这才是编程的真正开始!
计算机语言就是用来代替人类处理数据用的,IO流就是人类与计算机沟通的桥梁。{:3_68:}
哈哈,早睡早起,晚安各位!
今天作业:- /**
- *
- */
- package day18;
- import java.io.FileReader;
- import java.io.IOException;
- /**
- * @author always
- * FileReader 有两种读取方式
- * 1. char read();
- * 2. char[] read(char[]charArr);
- *
- */
- public class FileReaderDemo {
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- // method1();
- method2();
- }
- /*方法1*/
- public static void method1() {
- FileReader fr = null;
- try {
- fr = new FileReader("Demo.txt");
- int num = 0;
- while ((num = fr.read()) != -1) {
- System.out.print((char) num);
- }
- } catch (IOException e) {
- System.out.println(e);
- } finally {
- try {
- fr.close();
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- }
- /*方法2*/
- public static void method2() {
- FileReader fr = null;
- char[] buf = new char[1024];
- try {
- fr = new FileReader("Demo.txt");
- int num = 0;
- while ((num = fr.read(buf)) != -1) {
- System.out.println(String.valueOf(buf, 0, num));
- }
- } catch (IOException e) {
- System.out.println(e);
- } finally {
- try {
- if (fr != null)
- fr.close();
- } catch (IOException e) {
- System.out.println(e);
- }
- }
- }
- }
复制代码
|
|