当前位置: 移动技术网 > IT编程>开发语言>Java > 笔记-迎难而上之Java基础进阶5

笔记-迎难而上之Java基础进阶5

2019年05月26日  | 移动技术网IT编程  | 我要评论

lambda表达式无参数无返回值的练习

//定义一个接口
public interface cook{
    public abstract void makefood();
}
public class cookdemo{
    public static void main(string[] args){
    //使用匿名内部类来调用invokecook方法
        invokecook(new cook(){
            //重写方法
            public void makefood(){
                system.out.println("云想衣裳花想容");
            }
        });
        
        //使用lambada表达式来调用invokecook方法
        //格式()->{}
        invokecook(()->{system.out.println("春风扶槛露华浓");});
        
    }
    private static void invokecook(cook cook){
        cook.makefood();
    }
    
}

有参数练习

import java.util.*;
public class arraysdemo{
    public static void main(string[] args){
        //要求数组按照年龄排序
        person[] arr = {
            new person("貂蝉",18),
            new person("西施",19),
            new person("王昭君",16),
        };
        //对数组的对象进行排序
        arrays.sort(arr, new comparator<person>(){
            //重写接口comparator的方法
            public int compare(person o1,person o2){
                return o1.getage()-o2.getage();
            }
        });
        for(person p :arr){
            system.out.println(p);
        }
        //lambda表达式,简化匿名内部类
        
        arrays.sort(arr,(person o1,person o2)->{
            return o1.getage()-o2.getage();
            
        });
        
        for(person p :arr){
            system.out.println(p);
        }
        
    }
}
public interface calculator{
    public abstract int calc(int a,int b);
}
public class calculatordemo{
    public static void main(string[] args){
        //使用匿名内部类实现接口对象
        invokecalc(10,20,new calculator(){
            public int calc(int a,int b){
                return a+b;
            }
        });
        
        //lambda表达式
        invokecalc(110,120,(int a,int b)->{
            return a+b;
        });
    }
    public static void invokecalc(int a,int b,calculator c){
        int sum = c.calc(a,b);
        system.out.println(sum);
    }
}

file类

java.io.file类

文件和目录路径名的抽象表示形式。

java把电脑中的文件和文件夹封装为了一个file类,我们可以使用file类对文件和文件进行操作,我们可以使用file类的方法来:

  1. 创建一个文件/文件夹
  2. 删除文件/文件夹
  3. 获取文件/文件夹
  4. 判断文件/文件夹是否存在
  5. 对文件夹进行遍历
  6. 获取文件的大小

file类的静态成员变量

file.pathseparator;是路径分隔符 windows用表示分号 ; 表示路径分隔符 linux 用冒号 : 表示路径分隔符

file.separator :是文件名称分隔符 windows   linux /

绝对路径和相对路径

绝对路径:是一个完整的路径,以盘符开始的路径

绝对路径:是一个简化的路径,相对指的是相对于当前项目的根路径

注意:路径不区分大小写,两个反斜杠代表着一个普通的反斜杠

file类的构造方法

import java.io.file;
//创建file对象,只是把字符串路径封装为file对象,不考虑路径的真假情况

public class filedemo{
    public static void main(string[] args){
    show3();
    
    }
    private static void show3(){
        //file类的第三个构造方法
        //file(file parent,string child)
        file parent = new file("c:\\");
        file file = new file(parent ,"hello.java");
        system.out.println(file);//c:\hello.java
    }
    private static void show2(string parent,string child){
        //file类的第二个构造方法
        //file(string parent ,string child)
        file file = new file(parent,child);
        system.out.println(file);//c:\demo\a.txt
    }
    
    private static void show1(){
        //file类的第一个构造方法file(string pathname)
        //这个构造方法,会通过将给定路径名字符串转换为及抽象路径名来创建一个新的file实例
        file f1 = new file("e:\\study\\demo12\\a.txt");
        system.out.println(f1);
        file f2 = new file("e:\\study\\demo12");
        system.out.println(f2);
    }
}

file类的获取功能方法

import java.io.file;
public class filedemo1{
    public static void main(string[] args){
        show4();
    }
    private static void show4(){
        //public long length();返回文件的大小
        file file = new file("e:\\study\\demo12\\filedemo.class");
        long along = file.length();
        system.out.println(along);
        //若没有存在的文件则放回0
        
    }
    private static void show3(){
        //public string getname();返回此file表示的文件或目录的名称
        file file1 = new file("e:\\study\\demo12\\a.txt") ;
        string name1 = file1.getname();
        system.out.println(name1);// a.txt
        file file2 = new file("a.txt");
        string name2 = file2.getname();
        system.out.println(name2);// a.txt
    }
    
    private static void show2(){
        //public string getpath();返回的是路径的名称
        //传递的是什么就返回什么
        file file = new file("e:\\study\\demo12\\a.txt") ;
        string path1 = file.getpath();
        system.out.println(path1);//   e:\study\demo12\a.txt
        file path2 = new file("a.txt");
        system.out.println(path2);// a.txt
    }
    
    private static void show1(){
        //第一种获取功能演示
        //public string getabsolutepath(),返回的是绝对路径,
        //传递的是绝对路径
        file file1 = new file("e:\\study\\demo12\\a.txt");
        //返回来的也是绝对路径
        string path1 = file1.getabsolutepath();
        system.out.println(path1);//  e:\study\demo12\\a.txt
        //传递的是相对路径
        file file2 = new file("a.txt");
        string path2 = file2.getabsolutepath();
        //返回来的也是相对路径
        system.out.println(path2);//  e:\study\demo12\a.txt
    }
}

file类判断功能的方法

import java.io.file;
public class filedemo2{
    public static void main(string[] args){
        show2();
    }
    //public boolean isdirectory;判断是否是文件夹
    //public boolean isfile();判断是否是文件
    public static void show2(){
        file file1 = new file("e:\\study\\demo12");
        //先判断是否存在文件或文件夹
        if(file1.exists()){
            //判断是文件还是文件夹
            system.out.println(file1.isdirectory());
            system.out.println(file1.isfile());
            
        }
    }
    //public boolean exists();判断文件或者文件夹是否存在
    public static void show1(){
        file file1 = new file("e:\\study\\demo12");
        system.out.println(file1.exists());
        file file2 = new file("e:\\study\\demo12\\filedemo1.java");
        system.out.println(file2.exists());
    }
}   
file类的创建删除功能
package demo15;

import java.io.file;
public class filedemo3 {
    public static void main(string[] args) throws exception{
        show3();
    }
    private static void show3(){
        //delete()删除文件或者文件夹,不走回收站
        //如果文件夹中有内容会删除失败
        file file1 = new file("e:\\study\\demo12\\新建文件夹\\111\\2222\\33");
        boolean b1 = file1.delete();
        system.out.println(b1);
    }
    /*private static void show2(){
        //mkdir();创建单级别文件夹
        //mkdirs();创建多级文件夹
        file file1 = new file("e:\\study\\demo12\\新建文件夹");
        boolean b = file1.mkdir();
        file file2 = new file("e:\\study\\demo12\\新建文件夹\\111\\2222\\33");
        file2.mkdirs();

    }*/

    private static void show1() throws exception{
        //createnewfile() 文件不存在是创建一个新的文件,存在则不创建
        //在指定的路径上创建文件
        file file1 = new file("e:\\study\\demo12\\a.txt");
        boolean b = file1.createnewfile();
        system.out.println(b);
    }
}
遍历文件夹
import java.io.file;
public class filedemo4{
    public static void main(string[] args){
        show2();
    }
    public static void show1(){
        //string[] list(),返回的是一个string数组
        //file[] listfiles() ,返回的是一个file数组
        file file = new file("e:\\study\\demo12");
        string[] arr = file.list();
        for(string filename : arr){
            system.out.println(filename);
        }
    }
    public static void show2(){
        //string[] list(),返回的是一个string数组
        //file[] listfiles() ,返回的是一个file数组
        file file = new file("e:\\study\\demo12");
        file[] arr = file.listfiles();
        for(file filename : arr){
            system.out.println(filename);
        }
    }
}

递归

//遍历文件夹
import java.io.file;
public class allfiledemo{
    public static void main(string[] args){
        file afile = new file("e:\\test");
        showfile(afile);
    }
    public static void showfile(file file){
        //只要有文件夹就把你变成文件数组
        file[] files = file.listfiles();
        //遍历文件数组
        for(file f : files){
            if(f.isdirectory()){
                showfile(f);
            }else{
                system.out.println(f);
            }
        }
        
    }
} 

过滤器的使用

import java.io.*;

//过滤器的原理
//listfiles方法会对构造器传递的目录进行遍历,把文件/文件夹封装成file对象
//listfiles方法会把每一个封装的对象传递给accept,
public class filefilterimp implements filefilter{
    //这里的file pathname由listfiles遍历的每一个封装对象提供过来,
    //
    public boolean accept(file pathname){
    //判断传递过来的file对象是不是文件夹,是文件夹的话,就return true,让他封装成文件对象再一次传递过来
        if(pathname.isdirectory()){
            return true;
        }
        string name = pathname.getname();
        //把字符串,转换成小写
        name = name.tolowercase();
        //调用string类中的方法endswith判断字符串是否已.java结尾
        boolean b = name.endswith(".java");
        return b;
    }
}
//遍历文件夹

import java.io.*;
public class filedemo5{
    public static void main(string[] args){
        file afile = new file("e:\\test");
        showfile(afile);
    }
    public static void showfile(file file){
        //把文件或文件夹封装成file对象
        file[] files = file.listfiles(new filefilterimp());//传递过滤器对象
        //遍历文件数组
        for(file f : files){
            //是文件夹就要递归
            if(f.isdirectory()){
                showfile(f);
            }else{
                system.out.println(f);
            }
        }
        
    }
} 

//学这个过滤器有一点懵了,主要原因在于对类,类的方法熟悉度不够,更主要的原因是头脑转不过弯来………………出去转两圈才拐过弯,,,,,,学的有的吃力,

io流

输入流:把数据从其他设备上读取到内存中的流

输出流:把数据从内存中写出到其他设备上的流

输入流 输出流
字节流 字节输入流 inputstream 字节输出流 outputstream
字符流 字符输入流 reader 字符输出流 writer
字节输出流写入数据到文件
import java.io.*;
public class outputstreamdemo{
    public static void main(string[] args) throws ioexception{
        //先创建一个fileoutputstream对象,构造方法传递数据源
        fileoutputstream fos = new fileoutputstream("e:\\study\\demo12\\a.txt");
        //调用fileoutputstream对象中的方法write,把数据写入到文件中
        //任意的文本编辑器在打开文件的时候,都会查询编码表,把字节转换为字符表示
        //硬盘中存储的一切数据都是字节
        fos.write(97);// a
        //关闭流
        fos.close();
    }
}
字节输出流写多个字节的方法
import java.io.*;
public class outputstream1{
    public static void main(string[] args) throws ioexception{
        fileoutputstream fos = new fileoutputstream(new file("e:\\study\\demo12\\b.txt"));
        
        fos.write(49);
        fos.write(48);
        fos.write(48);//100
        
        //一次写多个字节的方法write(byte[] b)
        byte[] bytes = {65,66,67,68,69};//abcde
        fos.write(bytes);
        //写一部分字节write(byte[] b, int off,int len)
        fos.write(bytes,1,2);//bc
        //写入中文的方法,使用string类中的方法吧字符串,转换成字节数组
        byte[] bytes2 = "云想衣裳花想容".getbytes();
        fos.write(bytes2);//云想衣裳花想容
        
        fos.close();
    }
}

字节输出流的续写与换行

import java.io.*;
public class outputstreamdemo3{
    public static void main(string[] args) throws ioexception{
        //不加true的话每一次执行程序都会覆盖掉之前写的内容
        fileoutputstream fos = new fileoutputstream("e:\\study\\demo12\\c",true);
        for(int i=1;i<=10;i++){
            //把字符串变成字节数组,传递进去
            fos.write("云想衣裳花想容".getbytes());
            //  换行符  \r\n
            fos.write("\r\n".getbytes());
        }
        fos.close();
    }
}

字节输入流读取字节

import java.io.*;
public class inputstreamdemo{
    public static void main(string[] args) throws ioexception{
        //参数传递要读取的文件路径或文件
        fileinputstream fis = new fileinputstream("c.txt");
        int len=0;
        //当读取到文件末尾时会返回-1
        while((len=fis.read())!=-1){
            system.out.println(len);
        
        }
        fis.close();
    }
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网