Home> Java> javaTutorial> body text

IO (character stream character buffer stream)

巴扎黑
Release: 2017-06-26 09:53:50
Original
1545 people have browsed it

FileWriter: Function to write data to a file
Usage steps:
1. Create FW object
FileWriter(String fileName); Where to go When writing to a file, use the corresponding file name as a parameter
2. Write data
wirte(String str); Write string data str
3. Refresh
flush();
4. Close the resource
close();

Absolute path and relative path
Absolute path: The path with a drive letter is an absolute path, such as E:\\289\ \b.txt , the location where the file is stored is the location pointed by our path
Relative path: The path without a drive letter is a relative path. For example, b.txt , the location where the file is stored is the path of the current project

Refresh:flush is used to refresh the data in the stream to the file. You can write multiple times after flush.

close:Close the resource. Automatically perform a refresh before closing the resource. Once the stream is closed, it can no longer be written.

FileWriter 5 ways to write data
void write(String str) //Write a String
void write(String str, int index, int len) // Write a part of the string starting from index and lasting for len
void write(int ch) // Write a character, using int Reason, you can also write the unicode code of the corresponding character a---> 97, b --> 98
void write(char[] chs)//Write the data in the array chs to the file
void write(char[] chs,int index,int len) //Write part of the data in the array chs to the file starting from index and lasting for len

newline character
windows: \r\n
linux : \n
Mac : \r

Construction method
new FileWriter("test04.txt"); Overlay ; The new data overwrites all the original data in the file
new FileWriter(String fileName, boolean append); When append is true, it means appending, otherwise it means overwriting

FileReader function: Read data from a file
Usage steps:
1. Create an FR object
FileReader(String fileName); Pass the specified file name as a parameter
2 .Call the method to read the data
int read(); Read one character at a time and use this character as the return value. When the data reading is completed, the result returned is -1
int read(char[] chs) ; Read one character array at a time, store the read data into the chs array, and return the number read this time
3. Close the resource
close();

Read
int read(); Read one character at a time and use this character as the return value

int read(char[] chs); Read one character array at a time and store the read data In the chs array, the number of characters read this time is used as the return value. When
the data reading is completed, the return value is -1

BufferedWriter is used to write data. Advantages more efficient
Usage steps:
1. Create BW object
BufferedWriter bw = new BufferedWriter(new FileWriter(String fileName));
2. Write data
bw. write(String str);
3. Refresh
bw.flush()
4. Close
bw.close()
bw.newLine();// Insert a newline, according to Different systems insert line breaks corresponding to the system

BufferedReader is used to read data, with the advantage of being more efficient
Usage steps:
1. Create a BR object
BufferedReader br = new BufferedReader(new FileReader(String fileName));
2. Read data
br.read()
br.read(char[] chs)
3.Close
br .close();
readLine reads the contents of one line at a time, but does not read newlines. When the data reading is completed, null is returned

IO code practice
* Requirement 1:
* Read the data from the specified file, store it in the collection and display it
*
* 1. Observe the file and create a class to describe the user's information
* 2. Create a collection to store user information
* 3. Create a BR object
* 4. Read the file and add the composed object to the collection
* 5. Close the resource
* 6. Traverse And show

1 public static void test01() throws IOException { 2 ArrayList list = new ArrayList(); 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 }
Copy after login

Requirement 2:
Determine whether the specified user name and password are in the specified file
Let the user enter a user name and password to determine whether the user name and password are in the specified file In the specified file
1. Observe the file and create a class to describe the user’s information
2. Create a collection to store the user’s information
3. Create a BR object
4. Read the file, Add the component objects to the collection
5. Close the resource
6. Prompt the user to enter the user name and password
7. Search the collection with the user name and password entered by the user. If found, it will be in the file, otherwise Indicates absence

1 public static void test02() throws IOException { 2 ArrayList list = new ArrayList(); 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 }
Copy after login

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

1 public static void test03() throws IOException{ 2 3 ArrayList list = new ArrayList(); 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 }
Copy after login

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

1 public static void main(String[] args) throws IOException { 2 3 ArrayList list = new ArrayList(); 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 }
Copy after login

IO版学生管理系统

1 public static void main(String[] args) throws IOException { 2 String fileName = "list.txt"; 3 // 初始化数据 4 // 创建集合容器 5 ArrayList list = new ArrayList(); 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 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 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 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 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 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 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 }
Copy after login

The above is the detailed content of IO (character stream character buffer stream). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!