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();
}
}

笔记-迎难而上之Java基础进阶5的更多相关文章

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

    Collection集合 数组的长度是固定的,集合的长度是可变的 数组中存储的是同一类型的元素,可以存储基本数据类型值.集合存储的都是对象.而且对象的类型可以不一致. 集合框架 import java ...

  2. 笔记-迎难而上之Java基础进阶-终

    使用Stream流的方式,遍历集合 import java.util.*; public class StreamDemo{ public static void main(String[] args ...

  3. 笔记-迎难而上之Java基础进阶8

    函数式接口 函数式接口:有且只有一个抽象方法的接口,(可以包含其他默认,静态,私有方法) //函数式接口的使用,创建一个函数式接口 public interface FunctionalInterfa ...

  4. 笔记-迎难而上之Java基础进阶7

    序列化流 把对象以流的方式写入到文件中保存,叫做对象的序列化 把文件中保存的对象,以流的方式读取出来,叫做对象大反序列化 对象的序列化流_ObjectOutputtream继承自OutputStrea ...

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

    Set集合 import java.util.*; public class HashSetDemo{ public static void main(String[] args){ //Set接口的 ...

  6. 笔记-迎难而上之Java基础进阶3

    统计字符串中每一个不同的字符 import java.util.*; //统计字符串每一个字符出现的字数 public class StringDemo{ public static void mai ...

  7. 笔记-迎难而上之Java基础进阶6

    import java.io.*; public class InputStreamDemo{ public static void main(String[] args) throws IOExce ...

  8. 笔记-迎难而上之Java基础进阶4

    内部类创建多线程 //匿名内部类创建多线程 public class InnerClassThread{ public static void main(String[] args){ //回忆一下之 ...

  9. 第二十八节:Java基础-进阶继承,抽象类,接口

    前言 Java基础-进阶继承,抽象类,接口 进阶继承 class Stu { int age = 1; } class Stuo extends Stu { int agee = 2; } class ...

随机推荐

  1. SimpleDateFormat优化写法

    在一个读取数据库数据导出到excel文件的例子当中,每次处理一个时间信息的时候,就需要创建一个SimpleDateFormat实例对象,然后再丢弃这个对象.大量的对象就这样被创建出来,占用大量的内存和 ...

  2. request response cookie session

    request 1. url传递参数 1)参数没有命名, 如: users/views def weather(request, city, year): print(city) print(year ...

  3. 利用for循环和range输出9 * 9乘法口诀表

    li = [2, 3, 4, 5, 6, 7, 8, 9, 10] for i in li: for j in range(1, i): print('{0} * {1} = {2}'.format( ...

  4. JAVA基础篇—多态

    class ColaEmployee父类 package com.cola; public class ColaEmployee { private String name; private int ...

  5. Ubuntu下安装anaconda和pycharm

    折腾了一上午,终于装好了,如下:Python环境的安装: 安装anaconda 建议去https://www.anaconda.com/download/#linux直接用Ubuntu界面的搜狐浏览器 ...

  6. spoj104 HIGH - Highways 矩阵树定理

    欲学矩阵树定理必先自宫学习一些行列式的姿势 然后做一道例题 #include <iostream> #include <cstring> #include <cstdio ...

  7. Selenium WebDriver- 操作JavaScript的prompt弹窗(使用率低)

    #encoding=utf-8 import unittest import time from selenium import webdriver from selenium.webdriver i ...

  8. 利用Python分析羊车门问题

    题目描述:有3扇关闭的门,一扇门后面停着汽车,其余门后是山羊,只有主持人知道每扇门后面是什么.参赛者可以选择一扇门,在开启它之前,主持人会开启另外一扇门,露出门后的山羊,然后允许参赛者更换自己的选择. ...

  9. C++ POST方式访问网页

    bool PostContent(CString strUrl, const CString &strPara, CString &strContent, CString &s ...

  10. mq类----1

    MQ.php <?php /** * Created by PhpStorm. * User: brady * Date: 2017/12/6 * Time: 14:42 * * amqp协议操 ...