2020/4/9java学习日记
今日复习:
学生信息管理系统中删除功能的实现
public static void shanchu(ArrayList<student> array){
Scanner sc=new Scanner(System.in);
System.out.println("请输入您要删除的学号");
String xh=sc.nextLine();
int index=-1;
for(int x=0;x<array.size();x++){
student s=array.get(x);
if(s.getSid().equals(xh)){
index=x;
break;
}
}
if(index!=-1){
array.remove(index);
System.out.println("删除成功");
}else{
System.out.println("没有查找到您要删除的学号,请重新输入");
}
}
今日学习:
- IO流-FileWriter
import java.io.FileWriter;
import java.io.IOException;
public class io01 {
public static void main(String[] args) throws IOException {
FileWriter fw=new FileWriter("d:\\a.txt",true);
for(int i=0;i<11;i++){
fw.write(i+1+"helloworld");
fw.write("\n");
}
fw.flush();
fw.close();
}
}
- IO流-FileReader
一次读一个字符:int read();
一次读一个字符数组:int read(char[]cbuf);
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class iofilereader {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("d:\\b.txt");
char[]chs=new char[1024];
//没搞懂为啥老师说这里要填1024或者1024的倍数
int cs;
while((cs=fr.read(chs))!=-1){
//System.out.println(cs);
System.out.println(new String(chs,0,cs));
}
fr.close();
}
}
- 将一个Java文件复制到另一个Java文件中
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class iolianxi {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("io01.java");
FileWriter fw=new FileWriter("copy2.java");
int len;
char[]chs=new char[1024];
//为啥要定义1024或1024的倍数?
//为啥要循环,直接定一个非常大的字符数组,一次不就解决了吗?
while((len=fr.read(chs))!=-1){
fw.write(chs, 0, len);
}
fr.close();
fw.close();
}
}