笔记-迎难而上之Java基础进阶5
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类的方法来:
- 创建一个文件/文件夹
- 删除文件/文件夹
- 获取文件/文件夹
- 判断文件/文件夹是否存在
- 对文件夹进行遍历
- 获取文件的大小
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的更多相关文章
- 笔记-迎难而上之Java基础进阶1
Collection集合 数组的长度是固定的,集合的长度是可变的 数组中存储的是同一类型的元素,可以存储基本数据类型值.集合存储的都是对象.而且对象的类型可以不一致. 集合框架 import java ...
- 笔记-迎难而上之Java基础进阶-终
使用Stream流的方式,遍历集合 import java.util.*; public class StreamDemo{ public static void main(String[] args ...
- 笔记-迎难而上之Java基础进阶8
函数式接口 函数式接口:有且只有一个抽象方法的接口,(可以包含其他默认,静态,私有方法) //函数式接口的使用,创建一个函数式接口 public interface FunctionalInterfa ...
- 笔记-迎难而上之Java基础进阶7
序列化流 把对象以流的方式写入到文件中保存,叫做对象的序列化 把文件中保存的对象,以流的方式读取出来,叫做对象大反序列化 对象的序列化流_ObjectOutputtream继承自OutputStrea ...
- 笔记-迎难而上之Java基础进阶2
Set集合 import java.util.*; public class HashSetDemo{ public static void main(String[] args){ //Set接口的 ...
- 笔记-迎难而上之Java基础进阶3
统计字符串中每一个不同的字符 import java.util.*; //统计字符串每一个字符出现的字数 public class StringDemo{ public static void mai ...
- 笔记-迎难而上之Java基础进阶6
import java.io.*; public class InputStreamDemo{ public static void main(String[] args) throws IOExce ...
- 笔记-迎难而上之Java基础进阶4
内部类创建多线程 //匿名内部类创建多线程 public class InnerClassThread{ public static void main(String[] args){ //回忆一下之 ...
- 第二十八节:Java基础-进阶继承,抽象类,接口
前言 Java基础-进阶继承,抽象类,接口 进阶继承 class Stu { int age = 1; } class Stuo extends Stu { int agee = 2; } class ...
随机推荐
- Python学习笔记:time模块和datetime模块(时间和日期)
time模块 time模块通常用来操作时间戳信息(各种“秒”),常用的方法有: time.sleep(seconds):将当前程序阻塞指定秒数,然后继续运行程序. time.time():返回当前时间 ...
- matlab画图颜色设置
各种颜色属性选项选项意义选项意义'r' 红色 'm' 粉红'g' 绿色 'c' 青色'b' 兰色 'w' 白色'y' 黄色 'k' 黑色各种线型属性选项选项意义选项意义'-' 实线 '--' 虚线': ...
- 解决子线程操作UI的方法
- POJ-3278 广度优先搜索入门
#include<stdio.h> #include<stdlib.h> struct node{ int x; int s; }s[]; int main(){ ]={}; ...
- Redhat7配置yum源(本地源和网络源)
Redhat7配置yum源(本地源和网络源) 目录 一:配置本地yum源 二:配置网络yum源 YUM(Yellow dog Updater Modified): yum是基于RPM包构建的软件更 ...
- selenium - 下拉框操作
# 9. 下拉框操作# (1)等待下拉列表和下拉列表中值存在# (2)在下拉列表中选择一个值 # 三种方式# A. 获取所有的下拉列表值,然后用循环去匹配相同的值 select_by_index(下标 ...
- 添加rpmforge源 centos 7
(原创)RHEL/CentOS 7.x使用EPEL第三方yum源 时间 2014-07-22 19:45:50 服务器运维与网站架构 原文 http://www.ha97.com/5649.html ...
- [Android Studio篇][1] AS开发中遇到问题汇总
1 在android新建文件,提示权限不够,增加权限 修改工程下 main/AndroidMainfest.xml增加 <uses-permission android:name="a ...
- 《人月神话》读书笔记(2)-week3
为了确保团队中的每个人都能保持系统概念上的完整性,关于项目的书面规格说明是必不可少的.手册要描绘用户可见的一切,但不应支配实现的过程.光有规格说明也是不够的,会议也是必要的.书中提到的周例会会迅捷地给 ...
- hdu6034[模拟] 2017多校1
/*hdu6034[模拟] 2017多校1*/ //暴力模拟26个26进制数即可, 要注意进位 #include<bits/stdc++.h> using namespace std; t ...