File f1 = new File("E:\\itcast\\java.txt");
//boolean createNewFile()如果文件不存在则创建文件并返回true,如果文件存在就不创建文件并返回false
System.out.println(f1.createNewFile());
File f2 = new File("E:\\itcast\\javaee");
//boolean mkdir(),如果目录不存在则创建目录并返回true,如果目录存在就不创建目录并返回false
System.out.println(f2.mkdir());
//boolean mkdirs(),创建多级目录
File f3 = new File("E:\\itcast\\javaWEB\\html");
System.out.println(f3.mkdirs());
//文件删除
File f1 =new File("E:\\Idea_Java_Code\\algorithm_practice\\src\\File_practice\\java.txt");
System.out.println(f1.delete());
如果一个目录中有内容(目录或文件),不能直接删除,应该先删除目录中的内容,最后才能删除目录
File f1 = new File("E:\\itcast\\java.txt");
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
System.out.println(f1.exists());
System.out.println(f1.getAbsolutePath());
System.out.println(f1.getPath());
System.out.println(f1.getName());
File f2 = new File("E:\\itcast");
//String[] list():返回此抽象路径名表示的目录中的文件和mu'l的名称字符串数组
String[] list = f2.list();
for (String str:list){
System.out.println(str);
}
File[] files = f2.listFiles();
for(File file:files){
//打印出每个文件的绝对路径名
System.out.println(file);
if(file.isFile()){
System.out.println(file.getName());
}
}
File f1 = new File("E:\\itcast\\java.txt");
File f2 = new File("E:\\itcast\\ok.txt");
//注意renameTo(File file)应该传入File对象
System.out.println(f1.renameTo(f2));
I/O流就是指数据的传输
如果文件通过记事本打开我们能读懂其中的内容,就使用字符流,否则使用字节流
字节流抽象基类:
使用字节输出流写入数据:
//FileOutputStream(String name):创建文件输出流以指定的文件名称写入文件
//此方法调用系统功能创建了文件,创建了字节输出流对象,让字节输出流对象指向创建好的文件
//如果需要追加写入则FileOutputStream fileOutputStream = new FileOutputStream("E:\\itcast\\ok.txt",true);
FileOutputStream fileOutputStream = new FileOutputStream("E:\\itcast\\ok.txt");
//写入97
//fileOutputStream.write(97);这里写入的是ascii码对应的内容
fileOutputStream.write("97".getBytes());
//字节数组
byte[] bytes = "abcde".getBytes();
//写入bytes数组中的1至3
fileOutputStream.write(bytes,1,3);
//"\n".getBytes(),实现在写入数据时换行
fileOutputStream.write("\n".getBytes());
//void close():关闭此文件输出流并释放与此流相关的任何系统资源
fileOutputStream.close();
使用字节流读数据:
//创建字节输入流对象
FileInputStream fileInputStream = new FileInputStream("E:\\itcast\\ok.txt");
//读取一个数据
//调用字节输入流对象的读数据方法
int read = fileInputStream.read();
//强制转换为字符
System.out.println((char)read);
//读取文件中的全部数据
int by;
//文件中的数据以-1结束
while ((by=fileInputStream.read())!=-1){
System.out.print((char)by);
}
//释放资源
fileInputStream.close();
//字节缓冲输出流
FileOutputStream fos = new FileOutputStream("E:\\itcast\\ok.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//写数据
bos.write("jjjjjj\r\n".getBytes());
bos.close();
//字节缓冲流读数据
FileInputStream fis = new FileInputStream("E:\\itcast\\ok.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
int by;
while ((by=bis.read())!=-1){
System.out.print((char)by);
}
bis.close();
fis.close();
FileReader fr = new FileReader("E:\\itcast\\ok.txt");
BufferedReader bis = new BufferedReader(fr);
String str = "little";
String temp=bis.readLine();//用于保存每次临时读取的内容
while (temp!=null){
if(temp.contains(str)){
System.out.println(temp);
}
temp=bis.readLine();
}
bis.close();
fr.close();
FileReader fr = new FileReader("E:\\itcast\\ok.txt");
BufferedReader br = new BufferedReader(fr);
String oldStr = "little";
String newStr="large";
//内存流,作为临时流
CharArrayWriter temp = new CharArrayWriter();
String line = null;
while ((line=br.readLine())!=null){
if(line.contains(oldStr)){
System.out.println(line);
//替换原来的字符,这里要用replaceAll因为要替换所有的
line = line.replaceAll(oldStr,newStr);
System.out.println(line);
}
temp.write(line);
//换行
temp.append("\n");
}
br.close();
FileWriter out = new FileWriter("E:\\itcast\\ok.txt");
//讲内存流中的内容写入原文件
temp.writeTo(out);
out.close();
将上面代码中替换的内容改为空即可
line = line.replaceAll(oldStr,"");
https://www.cnblogs.com/tengqiuyu/p/6849097.html
https://blog.csdn.net/ssyan/article/details/6079660
/*有8个学生,有如下信息:名字,年龄,成绩。请从键盘输入8位同学的信息,存入文件cs.txt,再从文件中读取出数据,并对8位同学按照成绩由高到低排序,输出成绩第二高的同学的信息。*/
public static void main(String[] args) throws IOException {
// writeGrade();
readGrade();
}
public static class Student{
private String name;
private int age;
private int grade;
//省略get、set、toString、constructor方法
}
public static void writeGrade() throws IOException {
Map<Integer,Student> map = new HashMap<Integer, Student>();
Scanner scanner = new Scanner(System.in);
for(int i=1;i<=2;i++){
Student student = new Student();
System.out.print("请输入第"+i+"个学生的姓名");
student.setName(scanner.next());
System.out.print("请输入第"+i+"个学生的年龄");
student.setAge(scanner.nextInt());
System.out.print("请输入第"+i+"个学生的成绩");
student.setGrade(scanner.nextInt());
map.put(i,student);
}
FileWriter fw = new FileWriter("E://itcast//cs.txt");
BufferedWriter bw = new BufferedWriter(fw);
for(int i=1;i<=2;i++){
bw.write(map.get(i).getName());
bw.write(" ");
//注意这里写入整形数据需要将整形转为字符型
bw.write(String.valueOf(map.get(i).getAge()));
bw.write(" ");
bw.write(String.valueOf(map.get(i).getGrade()));
bw.write("\n");
}
bw.close();
fw.close();
}
public static void readGrade() throws IOException {
FileReader fr = new FileReader("E://itcast//cs.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
Student[] students = new Student[8];
for(int i=0;i<8;i++){
students[i]=new Student();
}
int lineNum=0;
while (line!=null){
String[] split = line.split(" ");
students[lineNum].setName(split[0]);
students[lineNum].setAge(Integer.parseInt(split[1]));
students[lineNum].setGrade(Integer.parseInt(split[2]));
lineNum++;
line=br.readLine();
}
for(Student student:students){
System.out.println(student.toString());
}
System.out.println("---------------------------");
//采用冒泡排序对学生成绩进行排序
for(int i=0;i<8;i++){
for(int j=i+1;j<8;j++){
if(students[i].getGrade()<students[j].getGrade()){
//交换两学生信息
Student temp = new Student(students[j].getName(),students[j].getAge(),students[j].getGrade());
students[j].setName(students[i].getName());
students[j].setAge(students[i].getAge());
students[j].setGrade(students[i].getGrade());
students[i].setName(temp.getName());
students[i].setAge(temp.getAge());
students[i].setGrade(temp.getGrade());
}
}
}
for(Student student:students){
System.out.println(student.toString());
}
}