首頁  >  文章  >  Java  >  IO(字元流 字元緩衝流)

IO(字元流 字元緩衝流)

巴扎黑
巴扎黑原創
2017-06-26 09:53:501564瀏覽

FileWriter: 作用在檔案中寫入資料
使用步驟:
1.建立FW物件
FileWriter(String fileName);  要往哪個文件中寫,就把對應的檔名當作參數
2.寫入資料
wirte(String str); 寫入字串資料str
3.刷新
flush();
4.關掉資源
close();

絕對路徑與相對路徑
絕對路徑: 帶盤符的路徑就是絕對路徑 例如E:\\289\ \b.txt  ,檔案存放的位置是我們路徑所指向的位置
相對路徑: 不帶盤符的路徑就是相對路徑 例如 b.txt, 檔案存放的位置,是目前工程的路徑下

刷新:flush 作用是把流中的資料刷新到檔案中,可以在flush後面進行多次寫入

close: 關閉資源,在關閉資源之前自動執行一次刷新, 一旦關閉流就不能再寫入

FileWriter 寫資料的5 個方法
void write(String str)  // 寫入一個字串 
void write(String str,int index,int len) // 寫入字串的一部分 從index開始 持續len個
void write(int ch)  // 寫入一個字元, 使用int的原因,也可以寫入對應字元的unicode碼   a---> 97 , b --> 98  
void write(char[] chs)// 把陣列chs中的資料寫入到檔案
void write(char[] chs,int index,int len) // 把數組chs中一部分資料寫入到檔案從index開始,持續len個


















# windows:  \r\n
linux : \n
Mac : \r  


建構方法

new FileWriter("test04.txt");   覆蓋式; 新資料把檔案原有的資料全都覆寫
new FileWriter(String fileName, boolean append); 當append為true的時候就表示追加, 否則表示覆寫 
FileReader作用:從檔案讀取資料


使用步驟:

1.建立FR物件
FileReader(String fileName); 把指定的檔案名稱當作參數傳遞
2 .呼叫方法讀取資料
int read();  一次讀取一個字元,並把這個字元當作回傳值,當資料讀取完畢時傳回的結果-1
int read(char[] chs) ; 一次讀取一個字元陣列,把讀取到的資料存放到chs數組中,傳回的是本次讀取的個數

3.關閉資源

close();
int read();  一次讀取一個字元並把這個字元當作回傳值

int read(char[] chs); 一次讀取一個字元陣列,把讀取到的資料存放在chs數組中, 把本次讀取到的字符個數作為返回值,當
  數據讀取完畢的時候返回值為-1


BufferedWriter用來寫入數據,優點更有效率

 使用步驟:
1.建立BW物件

BufferedWriter bw = new BufferedWriter(new FileWriter(String fileName));

2.寫入資料 bw. write(String str); 3.刷新
bw.flush()
4.關閉
bw.close()
bw.newLine();// 插入一個換行, 根據不同的系統插入對應系統的換行符號


BufferedReader用來讀取資料, 優點更有效率

 使用步驟:

1.建立BR物件
BufferedReader br = new BufferedReader(new FileReader(String fileName));
2.讀取資料

br.read()
br.read(char[] chs)
3.關閉
br .close();
readLine 一次讀取一行的內容,但是不讀取換行,當資料讀取完畢的時候返回null



IO程式碼練習

* 需求一:
* 從指定檔案讀取出來儲存到集合中並展示
* ### * 1.觀察檔 建立一個類別描述使用者的資訊### * 2.建立一個集合用來儲存使用者的資訊### * 3.建立一個BR物件### * 4.讀取檔案,組成物件加入到集合中### * 5.關閉資源### * 6.遍歷並展示######
 1 public static void test01() throws IOException { 2         ArrayList<User> list = new ArrayList<User>(); 3  4         // 创建一个BR对象 5         BufferedReader br = new BufferedReader(new FileReader("Users.txt")); 6         String line; 7         while ((line = br.readLine()) != null) { 8             // 切割 9             String[] uu = line.split(",");10             User u = new User(uu[0], uu[1]);11             list.add(u);12         }13         br.close();14 15         for (int i = 0; i < list.size(); i++) {16             User user = list.get(i);17             System.out.println(user.getName() + "   " + user.getPwd());18         }19     }
######需求二:### 判斷指定的使用者名稱和密碼是否在指定的檔案中### 讓使用者輸入一個使用者名稱和密碼判斷這個使用者名稱和密碼是否在指定的檔案中### 1.觀察檔 建立一個類別描述使用者的資訊### 2.建立一個集合用來儲存使用者的資訊### 3.建立一個BR物件### 4.讀取檔案,組成物件加入到集合中### 5.關閉資源### 6.提示使用者輸入使用者名稱和密碼### 7.拿使用者輸入的使用者名稱和密碼去集合中尋找, 如果找到就表示在檔案中, 否則表示不在######
 1 public static void test02() throws IOException { 2         ArrayList<User> list = new ArrayList<User>(); 3  4         // 创建一个BR对象 5         BufferedReader br = new BufferedReader(new FileReader("Users.txt")); 6         String line; 7         while ((line = br.readLine()) != null) { 8             // 切割 9             String[] uu = line.split(",");10             User u = new User(uu[0], uu[1]);11             list.add(u);12         }13         br.close();14 15         Scanner sc = new Scanner(System.in);16         System.out.println("请输入用户名");17         String name = sc.next();18         System.out.println("请输入密码");19         String pwd = sc.next();20 21         // 定义标记22         int index = -1;23         for (int i = 0; i < list.size(); i++) {24             User user = list.get(i);25             if (name.equals(user.getName()) && pwd.equals(user.getPwd())) {26                 // 修改标记27                 index = i;28                 break;29             }30         }31         // 判断笔记32         if (index == -1) {33             //34             System.out.println("用户名或者密码错误");35         } else {36             System.out.println("恭喜你猜对了");37         }38 39     }
###

 

需求三:
让用户输入一个用户名和密码 判断这个用户名和密码是否在指定的文件中 如果不在就一直猜 直到猜对为止
1.观察文件  创建一个类 描述用户的信息
2.创建一个集合用来存储用户的信息
3.创建一个BR对象
4.读取文件,组成对象 添加到集合中
5.关闭资源
6.提示用户输入用户名和密码
7.拿用户输入的用户名和密码 去集合中查找, 如果找到就表示在文件中, 否则表示不在
8.使用循环进行反复判断

 1 public static void test03() throws IOException{ 2  3         ArrayList<User> list = new ArrayList<User>(); 4  5         // 创建一个BR对象 6         BufferedReader br = new BufferedReader(new FileReader("Users.txt")); 7         String line; 8         while ((line = br.readLine()) != null) { 9             // 切割10             String[] uu = line.split(",");11             User u = new User(uu[0], uu[1]);12             list.add(u);13         }14         br.close();15 16         Scanner sc = new Scanner(System.in);17         while(true){18             19             System.out.println("请输入用户名");20             String name = sc.next();21             System.out.println("请输入密码");22             String pwd = sc.next();23 24             // 定义标记25             int index = -1;26             for (int i = 0; i < list.size(); i++) {27                 User user = list.get(i);28                 if (name.equals(user.getName()) && pwd.equals(user.getPwd())) {29                     // 修改标记30                     index = i;31                     break;32                 }33             }34             // 判断笔记35             if (index == -1) {36                 //37                 System.out.println("用户名或者密码错误 ,请重新输入用户名和密码 ");38             } else {39                 System.out.println("恭喜你猜对了");40                 break;41             }42 43             44         }45     }

需求四:
让用户输入一个用户名和密码 判断这个用户名和密码是否在指定的文件中 如果不在就一直猜 直到猜对为止 用户只能猜3次 每次失败提示还能猜几次
1.观察文件  创建一个类 描述用户的信息
2.创建一个集合用来存储用户的信息
3.创建一个BR对象
4.读取文件,组成对象 添加到集合中
5.关闭资源
6.提示用户输入用户名和密码
7.拿用户输入的用户名和密码 去集合中查找, 如果找到就表示在文件中, 否则表示不在
8.使用循环进行反复判断

 1     public static void main(String[] args) throws IOException { 2  3         ArrayList<User> list = new ArrayList<User>(); 4  5         // 创建一个BR对象 6         BufferedReader br = new BufferedReader(new FileReader("Users.txt")); 7         String line; 8         while ((line = br.readLine()) != null) { 9             // 切割10             String[] uu = line.split(",");11             User u = new User(uu[0], uu[1]);12             list.add(u);13         }14         br.close();15 16         Scanner sc = new Scanner(System.in);17         int count = 0; 
18         while(true){19             20             System.out.println("请输入用户名");21             String name = sc.next();22             System.out.println("请输入密码");23             String pwd = sc.next();24             count++;25             // 定义标记26             int index = -1;27             for (int i = 0; i < list.size(); i++) {28                 User user = list.get(i);29                 if (name.equals(user.getName()) && pwd.equals(user.getPwd())) {30                     // 修改标记31                     index = i;32                     break;33                 }34             }35             // 判断笔记36             if (index == -1) {37                 if(count < 3){38                     System.out.println("用户名或者密码错误 ,请重新输入用户名和密码  还可以猜测的次数是 :"+(3 - count ));39                     40                 }else{41                     System.out.println("别猜了 练习管理员吧 !!!");42                     break;43                 }44                 //45             } else {46                 System.out.println("恭喜你猜对了");47                 break;48             }49 50             51         }52         53     }

IO版学生管理系统

  1 public static void main(String[] args) throws IOException {  2         String fileName = "list.txt";  3         // 初始化数据  4         // 创建集合容器  5         ArrayList<Student> list = new ArrayList<Student>();  6         //从文件中读取数据存入到集合  7         readFromFile(list, fileName);  8 //        // ====================测试数据===================  9 //        Student s1 = new Student("9001", "张三", "18", "北京"); 10 //        Student s2 = new Student("9002", "张三3", "18", "北京"); 11 //        Student s3 = new Student("9003", "张三4", "18", "北京"); 12 //        list.add(s1); 13 //        list.add(s2); 14 //        list.add(s3); 15 //        // ====================测试数据=================== 16  17         System.out.println("----------------------欢迎使用管理系统--------------------------"); 18         // 死循环 19         while (true) { 20             // 接收用户的选择 21             int user = chose(); 22  23             // 根据用户的输入调用功能 24             switch (user) { 25             case 1: 26                 show(list); 27                 break; 28             case 2: 29                 add(list); 30                 save2File(list, fileName); 31                 break; 32             case 3: 33                 upd(list); 34                 save2File(list, fileName); 35                 break; 36             case 4: 37                 del(list); 38                 save2File(list, fileName); 39                 break; 40             case 5: 41                 System.out.println("欢迎下次再来哦 老板"); 42                 System.exit(0); 43                 break; 44  45             default: 46                 System.out.println("呵呵"); 47                 break; 48             } 49         } 50     } 51     //把数据从文件中读取出来 存入到集合  52     public static void readFromFile(ArrayList<Student> list,String fileName) throws IOException{ 53         // 创建BR对象 54                 BufferedReader br = new BufferedReader(new FileReader(fileName)); 55  56                 String line; 57                 while ((line = br.readLine()) != null) { 58                     // 9003,阿拉并,20,迪拜 59                     // 把读取到的一行信息 切割成各个字段 60                     String[] ss = line.split(","); 61                     // 把散装的数组组成对象 62                     Student s = new Student(ss[0], ss[1], ss[2], ss[3]); 63                     // 把对象添加到集合中 64                     list.add(s); 65                 } 66  67                 // 关闭资源 68                 br.close(); 69                 System.out.println("初始化完毕"); 70     } 71     // 把集合中的数据 写入到文件  72     public static void save2File(ArrayList<Student> list ,String fileName) throws IOException{ 73         //创建BW对象 74                 BufferedWriter bw = new BufferedWriter(new FileWriter(fileName)); 75                  76                 //遍历集合获取学生信息, 写入到文件  77                 for (int i = 0; i < list.size(); i++) { 78                     Student tmp = list.get(i); 79                     // 9001,张三,18,北京 80                     //使用sb按照指定的格式拼装学生的信息  81                     StringBuilder sb = new StringBuilder(); 82                     sb.append(tmp.getId()).append(",") 83                     .append(tmp.getName()).append(",") 84                     .append(tmp.getAge()).append(",") 85                     .append(tmp.getHome()); 86                     bw.write(sb.toString()); 87                     bw.newLine();// 换行 88                     bw.flush(); 89                 } 90                  91                 //6.关闭资源  92                 bw.close(); 93     } 94      95      96     public static int chose() { 97         // 展示菜单 98         System.out.println("===================================="); 99         System.out.println("1.展示学生信息");100         System.out.println("2.添加学生信息");101         System.out.println("3.修改学生信息");102         System.out.println("4.删除学生信息");103         System.out.println("5.退出学生信息管理系统");104         System.out.println("请输入功能序号");105         System.out.println("====================================");106         // 接收用户的输入107         Scanner sc = new Scanner(System.in);108         return sc.nextInt();109 110     }111 112     public static void del(ArrayList<Student> list) {113         // 提示输入学号114         Scanner sc = new Scanner(System.in);115         System.out.println("请输入学号");116         String id = sc.next();117         // 查找118         // 查找119         // 定义标记120         int index = -1;121         // 查找 关键位置修改标记122         for (int i = 0; i < list.size(); i++) {123             Student s = list.get(i);124             if (id.equals(s.getId())) {125                 // 找到126                 index = i;127                 break;128             }129         }130         //判断标记 131         if(index == -1){132             System.out.println("无这个学号的学生 请重新选择功能");133         }else{134             //删除135             list.remove(index);136             System.out.println("删除完毕");137         }138 139     }140 141     public static void upd(ArrayList<Student> list) {142         // 提示输入学号143         Scanner sc = new Scanner(System.in);144         System.out.println("请输入学号");145         String id = sc.next();146         // 查找147         // 定义标记148         int index = -1;149         // 查找 关键位置修改标记150         for (int i = 0; i < list.size(); i++) {151             Student s = list.get(i);152             if (id.equals(s.getId())) {153                 // 找到154                 index = i;155                 break;156             }157         }158         // 判断标记159         if (index == -1) {160             // 没找到161             System.out.println("没有这个学号的学生 请重新选择功能  ");162         } else {163             System.out.println("请输入新姓名");164             String name = sc.next();165             System.out.println("请输入新年龄");166             String age = sc.next();167             System.out.println("请输入新家乡");168             String home = sc.next();169             Student s = new Student(id, name, age, home);170             list.set(index, s);171             System.out.println("修改完毕");172         }173 174     }175 176     public static void add(ArrayList<Student> list) {177         // 提示输入学号178         Scanner sc = new Scanner(System.in);179         System.out.println("请输入学号");180         String id = sc.next();181         // 去重182         while (true) {183             // 拿着用户输入的id去集合中查找,如果没有相等的id说明合法, 否则提示不合法并继续输入继续去重,直到合法位置184             // 定义标记185             int index = -1;186             // 查找 关键位置改变标记187             for (int i = 0; i < list.size(); i++) {188                 Student student = list.get(i);189                 if (id.equals(student.getId())) {190                     // 有重复191                     index = i;192                     break;193                 }194             }195             // 判断标记196             if (index == -1) {197                 // 无重复198                 break;199             } else {200                 // 有重复201                 System.out.println("您输入的学号 重复了  请重新输入学号");202                 id = sc.next();203             }204         }205 206         System.out.println("请输入姓名");207         String name = sc.next();208         System.out.println("请输入年龄");209         String age = sc.next();210         System.out.println("请输入家乡");211         String home = sc.next();212         Student s = new Student(id, name, age, home);213         list.add(s);214         System.out.println("添加完毕");215     }216 217     public static void show(ArrayList<Student> list) {218         // 判断219         if (list.size() == 0) {220             System.out.println("系统中无学生信息, 请选择添加");221         } else {222             System.out.println("===================学生信息如下======================");223             System.out.println("学号\t\t姓名\t\t年龄\t\t家乡");224             for (int i = 0; i < list.size(); i++) {225                 Student s = list.get(i);226                 System.out.println(s.getId() + "\t\t" + s.getName() + "\t\t" + s.getAge() + "\t\t" + s.getHome());227             }228         }229         System.out.println("展示完毕");230     }

 

以上是IO(字元流 字元緩衝流)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn