注意
绝对路径:从盘符开始
File file = new File("D:/code/TestPhotot");
相对路径:不带盘符,默认直接直接到当前工程下的目录寻找文件
File file = new File("Demo/src/data");
public class Test {
public static void main(String[] args) {
//1.创建File对象(指定文件路径)
//路径写法:
//E:\\TestPhoto\\xifu.jpg
//E:/TestPhoto/xifu.jpg
File file = new File("E:\\TestPhoto\\xifu.jpg");
File f = new File("E:"+File.separator+"TestPhoto"+File.separator+"xifu.jpg");//相对安全,可以跨平台使用
System.out.println(file.length());//输出文件的字节大小
//2.File常见对象,支持绝对路径,支持相对路径(重点)
File file1 = new File("E:\\TestPhoto\\jichi.jpg");//绝对路径
System.out.println(file1.length());
File file2 = new File("Demo/src/data");//相对路径(当前工程目录下的)
System.out.println(file2.length());
//3.File创建文件,可以是文件,也可以是文件夹
File file3 = new File("E:/TestPhoto");
System.out.println(file3.exists());//判断文件夹是否存在
}
}
public class Test {
public static void main(String[] args) {
//1.创建File对象
File file = new File("E:/TestPhoto/xifu.jpg");
//2.获取绝对路径
System.out.println(file.getAbsoluteFile());
//3.获取文件定义的时候使用的路径
System.out.println(file.getPath());
//4.获取文件的名称(带后缀)
System.out.println(file.getName());
//5.获取文件的大小:字节个数
System.out.println(file.length());
//6.获取文件的最后修改时间
System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(file.lastModified()));
//7.判断是文件还是文件夹
System.out.println(file.isFile());//true
System.out.println(file.isDirectory());//false
//8.判断路径是否存在
System.out.println(file.exists());
File f = new File("E:/TestPhoto");
System.out.println(f.isFile());//false
System.out.println(f.isDirectory());//true
}
}
public class Test {
public static void main(String[] args) throws Exception{
File file = new File("Demo/src/data02.test");
//1.创建新文件,创建成狗true,反之false
System.out.println(file.createNewFile());//true
//2.mkdir创建一级目录
File file1 = new File("E:/TestPhoto/aaa");
System.out.println(file1.mkdir());//true 【了解即可,不怎么会用,后面使用到的文件也是自动创建好的】
//3.mkdirs创建多级目录(重点)
File file2 = new File("E:/TestPhoto/bbb/ccc");
System.out.println(file2.mkdirs());//true
//4.删除文件或者空文件夹
System.out.println(file1.delete());//删除上面刚创建的aaa
//5.只能删除空文件夹,不能删除非空文件夹
File file3 = new File("E:/TestPhoto");
System.out.println(file3.delete());//false
}
}
listFiles方法注意事项:
public class Test01 {
public static void main(String[] args) {
//1.定位一个目录,获取该目录下一级文件名称并遍历
File file = new File("E:/TestPhoto");
String[] list = file.list();
for (String s : list) {
System.out.println(s);
}
//2.获取目录下一级文件对象并遍历
File file1 = new File("E:/");
File[] files = file1.listFiles();
for (File file2 : files) {
System.out.println(file2.getName());
}
}
}
public class Test {
public static void main(String[] args) {
test();
}
public static void test(){
System.out.println("=========test被执行==========");
test();//递归,会产生死循环
}
}
【案例】
public class Test02 {
public static void main(String[] args) {
//计算阶乘
System.out.println(f(5));
}
public static int f(int n){
if(n == 1){
return 1;
}
return f(n - 1)*n;
}
}
【递归算法三大要素】
【案例】
猴子第一天摘下若干桃子,当即吃了一半,觉得不过瘾,于是又多吃了一个,第二天又吃了前天剩余桃子数量的一半,再多吃一个。后面每天都吃前天剩余桃子数量的一半,再多吃一个,等到第10天的时候发现桃子只有1个了
public class Test04 {
/**
* f(x) - f(x)/2 - 1 = f(x+1)
* 2f(x) - f(x) -2 = 2f(x+1)
* f(x) = 2f(x+1) + 2;
*/
public static void main(String[] args) {
System.out.println(f(1));
}
public static double f(double n){
if(n == 10){
return 1;
}
return 2*f(n+1) + 2;
}
}
【案例】:文件搜索
public class Test05 {
public static void main(String[] args) {
File file = new File("D:/");
searchFile(file,"wegame.exe");
}
/** 创建方法,搜搜某个目录下是否有我要找的文件
* @Param File dir
* @Param String fileName
*/
public static void searchFile(File dir,String fileName){
//1.判断dir是否是目录
if(dir!=null && dir.isDirectory()){
//2.是——获取目录下一级文件对象
File[] files = dir.listFiles();
//3.是否存在一级文件对象,随后再开始遍历
if(files!=null && files.length > 0){
for (File file : files) {
//6.判断当前文件对象是文件还是文件夹
if(file.isFile()){
//7.判断是否是自己要查找的
if(file.getName().equals(fileName)){
System.out.println("查找文件的绝对路径为:"+file.getAbsoluteFile());
return;
}
}
//8.是文件夹,则递归继续寻找
searchFile(file,fileName);
}
}
}else {
//System.out.println("当前搜索的位置不是文件夹!"+dir.getAbsoluteFile());
}
}
}
【案例】
public class Test06 {
public static int totalNumber;//总共的啤酒数量
public static int lastCoverNumber;//上一次剩余的盖子数量
public static int lastBottleNumber;//上一次剩余的瓶子数量
public static void main(String[] args) {
buy(10);
System.out.println("总数量:"+totalNumber);
System.out.println("剩余的盖子数量:"+lastCoverNumber);
System.out.println("剩余的瓶子数量:"+lastBottleNumber);
}
public static void buy(int money){
//存了买了几瓶的数据
int buyNumber = money / 2;
totalNumber += buyNumber;
//本轮剩余的盖子数和瓶子数量
int coverNumber = lastCoverNumber + buyNumber;
int bottleNumber = lastBottleNumber + buyNumber;
//吧盖子和瓶子换算成钱
int allMoney = 0;
if(coverNumber >= 4){
allMoney += 2;
}
lastCoverNumber = coverNumber%4;
if(bottleNumber >= 2){
allMoney += 2;
}
lastBottleNumber = bottleNumber%2;
if(allMoney >= 2){
buy(allMoney);
}
}
}
public class Test {
public static void main(String[] args) throws Exception{
//1.编码:吧文字转为字节(使用指定的编码)
String str = "abc马浩楠";
byte[] bytes = str.getBytes();//默认编码字符集为:UTF-8【英文字节统一为1,UTF-8的中文为:一个字是3
System.out.println(str.length());
System.out.println(Arrays.toString(bytes));
//2.解码:吧字节转为文字(编码前和编码后使用的字符集必须一致,否则乱码)
String result = new String(bytes);
System.out.println(result);
//指定字符集
System.out.println("======================================");
byte[] gbks = str.getBytes("GBK");//GBK字符集:英文字母一个为1,中文一个为2
System.out.println(gbks.length);
System.out.println(Arrays.toString(gbks));
//解码
//String rs = new String(gbks);//默认编码为UTF-8 而转字节的为GBK,乱码
String rs = new String(gbks,"GBK");
System.out.println(rs);
}
}
public class Test {
public static void main(String[] args) throws Exception {
//1.创建一个文件字节流输入流与文件接通
InputStream is = new FileInputStream("Demo/src/data");
//2.读取字节并输出
/*System.out.println((char)is.read());
System.out.println((char)is.read());
System.out.println((char)is.read());
System.out.println((char)is.read());*///文件当前只有3个,所以读取到第四个的时候会返回-1
//3.使用循环遍历 改进读取
int b;
while ((b = is.read()) != -1) {
System.out.println((char)b);
}
}
}
//data:ab1
public class Test02 {
public static void main(String[] args) throws Exception{
//1.创建输入流对象与源文件接通
InputStream is = new FileInputStream("Demo/src/data02");
//2.读取字符数组,一次读取三个字符
/*byte[] bytes = new byte[3];
int count = is.read(bytes); //记录读取字节数
System.out.println("读取的字节数:"+count);
String str = new String(bytes);
System.out.println(str);
int count1 = is.read(bytes); //记录读取字节数
System.out.println("读取的字节数:"+count1);
String str1 = new String(bytes);
System.out.println(str1);
int count2 = is.read(bytes); //记录读取字节数
System.out.println("读取的字节数:"+count2);
String str2 = new String(bytes);
System.out.println(str2);
int count3 = is.read(bytes); //记录读取字节数
System.out.println("读取的字节数:"+count3);//读取数量为3,但未读的为2个字节,所以最后的一个字节是上一组读取没被覆盖的
//解决:读取多少出多少
String str3 = new String(bytes,0,count3);
System.out.println(str3);*/
//3.改进,使用循环遍历
int count;//记录读取数量
byte[] bytes = new byte[3];//每次读取3个字节
while((count = is.read(bytes)) > 0){
System.out.println(new String(bytes,0,count));
}
}
}
//data02:abc爱efgbb
【存在问题】
【方式一】
【方式二】
public class Test03 {
public static void main(String[] args) throws Exception{
//1.创建文件字节输入流与源文件接通
File file = new File("Demo/src/data03");
InputStream is = new FileInputStream(file);
//2.定义数组,长度与读取文件大小刚好一致【避免因为文件字节不是3的倍数中遇见中文而乱码】
/*byte[] bytes = new byte[(int) file.length()];
int length = is.read(bytes);
System.out.println("读取字节:"+length);
System.out.println("文件大小:"+file.length());
System.out.println(new String(bytes));*/
//改进:使用Api直接读取全部字节
byte[] bytes = is.readAllBytes();
System.out.println(new String(bytes));
}
}
public class Test {
public static void main(String[] args) throws Exception{
//1.创建文件字节输出流与目标文件接通
OutputStream os = new FileOutputStream("Demo/src/out.txt");//每次创建,就会清空该文件之前的东西,再写新数据
//OutputStream os = new FileOutputStream("Demo/src/out.txt",true);多的参数true:表示不清空之前的东西,继续写新的数据
//2.写数据出去
//a.public void write(int a):写一个字节出去
os.write('a');
os.write(97);
os.write("\r\n".getBytes());//换行:单独\n可能导致有些系统不兼容
//os.write('马');乱码
//b.public void write(byte[] buffer):写一个字节数组出去
byte[] buffer = {'a',97,98,99};
os.write(buffer);
os.write("\r\n".getBytes());//换行
//c.public void write(byte[] buffer,int pos,int len):写一个字节数组的一部分出去
byte[] buffer1 = {'a',97,98,99,100};
os.write(buffer1,0,3);
os.write("\r\n".getBytes());//换行
//写中文
byte[] buffer2 = "我是中国人".getBytes();
//byte[] buffer2 = "我是中国人".getBytes("GBK");写出写入不一致,会导致乱码
os.write(buffer2);
os.write("\r\n".getBytes());//换行
//刷新数据:刷新后可以继续使用流
//os.flush();
//释放资源,自带刷新数据,但释放资源后,流不能继续使用
os.close();
}
}
public class Test {
public static void main(String[] args) {
try {
//1.创建字节输入流与文件接通
InputStream is = new FileInputStream("E:\\TestPhoto\\17dfcc00fc81573fd8e77177768e0502.mp4");
//2.创建字节输出流
OutputStream os = new FileOutputStream("E:\\TestPhoto\\copy.mp4");
//3.定义字节数组转移数据
byte[] buffer = new byte[1024];
int len;//记录读取次数
while((len = is.read(buffer)) != -1){
//转移数据
os.write(buffer,0,len);
}
System.out.println("复制成功!");
//关闭流
os.close();
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) {
InputStream is = null;
OutputStream os = null;
try {
//1.创建字节输入流与文件接通
is = new FileInputStream("E:\\TestPhoto\\17dfcc00fc81573fd8e77177768e0502.mp4");
//2.创建字节输出流
os = new FileOutputStream("E:\\TestPhoto\\copy.mp4");
//3.定义字节数组转移数据
byte[] buffer = new byte[1024];
int len;//记录读取次数
while((len = is.read(buffer)) != -1){
//转移数据
os.write(buffer,0,len);
}
System.out.println("复制成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
//关闭流
try {
//加上非空校验,以防流并没有找到文件无法产生(避免空指针异常)
if(os!=null) os.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(is!=null) is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public class Way02 {
public static void main(String[] args) {
try(
//这里只能存放资源对象,用完会自动关闭(close),自动释放资源(即使出现异常,也会执行这个操作)
//1.创建字节输入流与文件接通
InputStream is = new FileInputStream("E:\\TestPhoto\\17dfcc00fc81573fd8e77177768e0502.mp4");
//2.创建字节输出流
OutputStream os = new FileOutputStream("E:\\TestPhoto\\copy.mp4");
) {
//3.定义字节数组转移数据
byte[] buffer = new byte[1024];
int len;//记录读取次数
while((len = is.read(buffer)) != -1){
//转移数据
os.write(buffer,0,len);
}
System.out.println("复制成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Way03 {
//这种方式,吧资源流放在try的外面,但还需要去try/catch,否则只能抛出去
public static void main(String[] args) throws Exception{
//1.创建字节输入流与文件接通
OutputStream os = new FileOutputStream("E:\\TestPhoto\\copy.mp4");
//2.创建字节输出流
InputStream is = new FileInputStream("E:\\TestPhoto\\17dfcc00fc81573fd8e77177768e0502.mp4");
try(os;is) {
//3.定义字节数组转移数据
byte[] buffer = new byte[1024];
int len;//记录读取次数
while((len = is.read(buffer)) != -1){
//转移数据
os.write(buffer,0,len);
}
System.out.println("复制成功!");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) throws Exception{
//1.创建字符输入流与文件接通
Reader r = new FileReader("Demo/src/data03");
//2.读取一个字节输出控制台,如果没可读字符返回-1
/*int code = r.read();
System.out.println((char)code);*/
//3.使用循环读取所有字节
int code;
while ((code = r.read()) != -1) {
System.out.println((char)code);
}
}
}
public class Test02 {
public static void main(String[] args) throws Exception{
//1.创建字符输入流与文件接通
Reader r = new FileReader("Demo/src/data03");
//2.用循环,每次读取一个字符数组的数据
char[] buffer = new char[1024]; //是1024K字节,而不是1024KB
int len;//记录每次读取的数量,以防多读(读多少,输出多少)
while((len = r.read(buffer)) != -1){
System.out.println(new String(buffer,0,len));
}
}
}
public class Test {
public static void main(String[] args) throws Exception{
//1.创建字符输出流与文件接通
//Writer w = new FileWriter("Demo/src/out.txt");//覆盖管道
Writer w = new FileWriter("Demo/src/out.txt",true);//追加管道【不清空之前的数据,继续拼接】
//a.public void write(int c):写一个字符出去
w.write(97);
w.write('b');
w.write('马');
w.write("\r\n");//换行
//b.public void wirte(String c):写一个字符串出去
w.write("马浩楠");
w.write("\r\n");//换行
//c.public void write(char[] buffer):写一个字符数组出去
w.write("张林燕".toCharArray());
w.write("\r\n");//换行
//d.public void write(String c,int pos,int len):写字符串的一部分出去
w.write("I am tired,but I am poor so I must every effort to earn。暗黑哈哈哈",0,55);
//e.public void write(char[] buffer,int pos,int len):写字符数组的一部分出去
w.write("爱你",0,1);
//w.flush();//刷新数据,还可以继续使用流
w.close();//关闭流,释放资源,包含了刷新舒徐,但不能再继续使用流
}
}