首页 技术 正文
技术 2022年11月19日
0 收藏 470 点赞 4,432 浏览 7914 个字

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();
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,410
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,183
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,903