Io flow exercise
*According to the flow direction: input flow, output flow
Divided according to the file type : byte stream, character stream
Io flow operation steps:
1. Handling exceptions
2. Specific reading and writing
3. Close flow
Common flow:
Input byte stream: InputStream
output byte stream: OutputStream
file input byte stream: FileInputStream
file output byte stream: FileOutputStream
buffer input byte stream: BufferedInputStream
buffer output byte stream: BufferedOutputStream
Input character stream: FileReader
output character stream: FilieWriter
buffer input character stream: BufferedReader
buffer output character stream: BufferedWriter
Object input stream: ObjectInputStream
Object output stream: ObjectOutputStream
Common methods:
read() take out the file from the computer disk
write() save the value or file to the computer disk
close() close the stream
newLine wrap
Serialization and deserialization of objects in and out of files
Serialization: The process of converting an object into a sequence of bytes is called serialization of the object. (It is common to save files)
Deserialization: The process of restoring a byte sequence to an object is called object deserialization.
Only objects of classes that implement the Serializable and Externalizable interfaces can be serialized
public static void main(String[] args) throws Exception{
FileOutputStream fos = new FileOutputStream("E:\\A\\student.txt");
// 输出对象流
ObjectOutputStream oos = new ObjectOutputStream(fos);
Student s1 = new Student(1, "张三", 18, 16.0);
// 以序列化的形式,将对象写到文件 二进制文件
oos.writeObject(s1);
oos.close();
fos.close();
System.out.println("结束" );
}
Exercise 1
Read a sequence of bytes from a source input stream, then deserialize them into an object, and return it.
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream("E:\\A\\student.java");
// 输入对象流
ObjectInputStream ois = new ObjectInputStream(fis);
// 读取一个对象: 将字节反序列化为对象
Student o = (Student) ois.readObject( );
System.out.println(o );
ois.close();
}
Exercise 2
Create a student object: attribute sid sage sname score
// write a method addOne(Student s, String file) to write the information of s to the destination file file
One line for each student: 1001-Han Meimei-18-98.5
Write a method deleteOne( int sid, String file) in the file specified line data deletion sid
Note that the file can not be associated with the input and output streams
creates an intermediate file to modify the information read from the source file is written to the intermediate file
delete the source file is renamed intermediate File
Write a method addOne (Student s, String file) to write the information of s to the destination file file
private static void addOne(Students s, String file) throws IOException {
//创建输出流
BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
bw.write(s.getSid()+"-"+s.getSage()+"-"+s.getSname()+"-"+s.getScore());
bw.newLine();
//关流
bw.close();
}
Exercise 3
//Write a method deleteOne(int sid,String file) to delete the row data of the specified sid in the file
private static void deleteOne(int i, String file) throws IOException {
File file1 = new File(file);
//输出流
BufferedReader br = new BufferedReader(new FileReader(file1));
//创建一个临时文件
File file2 = new File("E://A//f.txt");
BufferedWriter bw = new BufferedWriter(new FileWriter(file2 ,true));
String c = null;
while ((c = br.readLine())!= null){
String s = c;
//根据给定字符拆分
String[] split = s.split("-");
System.out.println("拆分后字符串:"+ split[0]);
int a = Integer.parseInt(split[0]);
if (a==i){
continue;
}
bw.write(c);
bw.newLine();
}
//关流
br.close();
bw.close();
//删除原有的数据
boolean delete = file1.delete( );
System.out.println(delete );
//临时文件重命名
file2.renameTo(new File(file));
}
Exercise 4:
Design method, pass in the file/folder path, delete the file/folder
//设计方法,传入文件/文件夹路径删除该文件,文件夹
public static void test(File file) {
//判断是否存在
if (file.exists()) {
//得到文件数组
File[] files = file.listFiles();
//遍历所有的子目录和文件
for (File f : files) {
//判断此抽象路径名表示的文件是否是一个标准文件。
if (f.isFile()) {
f.delete();//是文件就删除
} else {
test(f);//如果是文件夹就递归这里进入子文件夹中
}
}
file.delete();//删除文件夹
}
}
Exercise 5
//Homework: Design method, pass in an integer n and return the result of its factorial
public static int jc(int n){
if (n==1){
return 1;
}else {
return n * jc(n - 1);
}
}
Exercise 6
Write a method to get the byte size of the parameter file/folder
public static void getLength(File file) {
//判断是否是标准文件
if(!(file.exists())){
//不是标准文件
System.out.println("文件路径错误");
}
//判断是否是文件夹
if(file.isDirectory()){
//得到文件数组
File[] files = file.listFiles();
//遍历文件数组
for (File f:files){
//递归
getLength(f);
}
}
a+=file.length();
}
Exercise 7
Design method:
List all the notepad files in the A folder (judging the string suffix)
//全局静态属性,集合用来存储所有的文件名
static ArrayList<String> arrs = new ArrayList<>();
public static ArrayList job1(File file){
if(file.exists()) {
//获取文件数组
File[] files = file.listFiles();
//遍历文件数组,获得文件名
for (File f : files) {
//判断名字是不是。txt结尾
if (f.getName().endsWith(".txt")) {
arrs.add(f.getName());
} else {
job1(f);
}
}
return arrs;
}
System.out.println("文件路径错误");
return null;
}
Exercise 8
Delete all files in folder A
private static void job2(File file) {
//判断该文件是否存在
if (file.exists()){
//得到文件数组
File[] files = file.listFiles();
//遍历所有子目录和文件
for(File f:files) {
//判断文件是不是一个标准文件
if (f.isFile()) {
//是的话删除该文件
f.delete();
} else {
//不是文件就递归到子文件夹里,进行删除文件
job2(f);
}
}
return;
}
System.out.println("文件路径错误");
}
Exercise 9
Delete specified folder
private static void job3(File file) {
//判断该文件是否存在
if (file.exists()){
//得到文件数组
File[] files = file.listFiles();
//遍历所有子目录和文件
for(File f:files){
//判断文件是不是一个标准文件
if (f.isFile()){
//是的话删除该文件
f.delete();
}else {
//不是文件就递归到子文件夹里,进行删除文件
job3(f);
}
}
//删除文件夹
file.delete();
return;
}
System.out.println("文件路径错误");
}
Exercise 10
Use the character stream to convert the data in the a file and write it to the b file. Convert uppercase to lowercase Lowercase convert to uppercase Delete numbers
public static void changeFile(String filea,String fileb) throws Exception {
//缓冲字符输入流
BufferedReader br = new BufferedReader(new FileReader(filea));
//缓冲区字符输出流
BufferedWriter bw = new BufferedWriter(new FileWriter(fileb,true));
int c=-1;
while ((c=br.read())!= -1){
if(65<=c&&c<=90){
c+=32;
bw.write(c);
continue;
}else if(97<=c&& c<=122){
c-= 32;
bw.write(c);
continue;
}else if(48<=c && c<= 57){
continue;
}
bw.write(c);
}
br.close();
bw.close();
}
}
What is not well written, please give me some advice! !