package heima;
import java.io.File;
public class P268 {
public static void main(String[] args) {
//File(String pathname):通过将给定的路径名字符串转换为 抽象 路径名来创建新的File
File f1 = new File("C:\\test\\java.txt");
System.out.println(f1);
//File(String parent,String child):从父路径名字符串和子路径名字符串创建新的File
File f2 = new File("C:\\test","java.txt");
System.out.println(f2);
//File(file parent,String child):从父 抽象 路径名和子路径名字符串创建新的File
File f3 = new File("C:\\test");
File f4 = new File(f3,"java.txt");
System.out.println(f4);
}
}
三个构造方法,效果一致
package heima;
import java.io.File;
import java.io.IOException;
public class P273 {
public static void main(String[] args) throws IOException {
//1.创建一个文件java.txt
File f1 = new File("C:\\itcast\\java.txt");
System.out.println(f1.createNewFile());
System.out.println("---------");
//2.创建一个目录JavaSE
File f2 = new File("C:\\itcast\\JavaSE");
System.out.println(f2.mkdir());
System.out.println("---------");
//
//3.创建一个多级目录JavaWEB\\HTML
File f3 = new File("C:\\itcast\\JavaWEB\\HTML");
System.out.println(f3.mkdirs());
System.out.println("---------");
//
//4.创建一个文件javase.txt
File f4 = new File("C:\\itcast\\javase.txt");
System.out.println(f4.createNewFile());
//System.out.println(f4.mkdir());
}
}
1.一开始使用createNewFile报错
需要自行抛出异常
2.创建完文件再创建路径,报错
package heima;
import java.io.File;
public class P274 {
public static void main(String[] args) {
File f = new File("C:\\itcast\\java.txt");
//判断
System.out.println(f.isDirectory());
System.out.println(f.isFile());
System.out.println(f.exists());
System.out.println(f.getAbsoluteFile());
System.out.println(f.getPath());
System.out.println(f.getName());
System.out.println("----------");
File f2 = new File("C:\\itcast");
String[] strArray = f2.list();
for (String str:strArray){
System.out.println(str);
}
System.out.println("-----------");
File[] fileArray = f2.listFiles();
for (File file:fileArray){
if (file.isFile()){
System.out.println(file.getName());
}
}
}
}
package heima;
import java.io.File;
import java.io.IOException;
public class P275 {
public static void main(String[] args) throws IOException {
//1.创建 与 删除java.txt文件
File f1 = new File("C:\\itcast\\java.txt");
System.out.println(f1.delete());
System.out.println("---------");
//2.创建 与 删除itcast目录
File f2 = new File("C:\\itcast\\itcast");
System.out.println(f2.mkdir());
System.out.println(f2.delete());
System.out.println("--------");
//3.创建目录itcast,然后在该目录下创建一个文件java.txt
File f3 = new File("C:\\itcast\\itcast");
System.out.println(f3.mkdir());
File f4 = new File("C:\\itcast\\itcast\\java.txt");
System.out.println(f4.createNewFile());
//4.删除上述目录itcast
System.out.println(f4.delete());
System.out.println(f3.delete());
}
}
当被删除目录下存在文件时,直接删除,输出false
package heima;
public class P276 {
public static void main(String[] args) {
//1 1 2 3 5 8
int[] arr = new int[20];
arr[0] = 1;
arr[1] = 1;
for (int i = 2;i<arr.length;i++){
arr[i] = arr[i-1] + arr[i-2];
}
System.out.println(arr[19]);
System.out.println();
}
//使用递归解决
//StackOverflowError:当堆栈溢出发生时抛出一个应用程序递归太深(一般是递归没有设置出口)
public static int f(int n){
if (n==1 || n==2){
return 1;
}
else {
return f(n-1)+f(n-2);
}
}
}
package heima;
import java.io.File;
public class P278 {
public static void main(String[] args) {
//创建一个File对象
File srcFile = new File("C:\\Users\\路聃\\Desktop\\Java");
//调用方法
}
//定义一个方法,用于获取给定目录下的所有内容
public static void getAllFilePath(File srcFile){
//获取指定目录下的所有文件的File数组
File[] fileArray = srcFile.listFiles();
//遍历该File数组
if (fileArray!=null){
for (File file:fileArray){
//判断该File对象是否目录
if (file.isDirectory()){
getAllFilePath(file);
}
else {
//不是,获取绝对路径输出s
System.out.println(file.getAbsoluteFile());
}
}
}
}
}
网课:
自己:
package heima;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class P280 {
public static void main(String[] args) throws IOException {
//创建字节输出流对象
FileOutputStream fos = new FileOutputStream("study\\1.txt");
//抛出异常FileNotFoundException
//write(int b):将指定的字节写入此文件输出流。
fos.write(97);
//需要抛出异常 IOException
//最后释放资源
fos.close();
}
}
疑问:为啥自己写idea内的相对路径就不成功
//FileOutputStream(String name):创建文件输出流以指定的名称写入文件。
FileOutputStream fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\1.txt");
// 等价于
// FileOutputStream fos = new FileOutputStream(new File("C:\\Users\\路聃\\Desktop\\Java\\1.txt"));
//FileOutputStream(File file):创建文件输出流以写入由指定的 File对象表示的文件。
File file = new File("C:\\Users\\路聃\\Desktop\\Java\\1.txt");
FileOutputStream fos2 = new FileOutputStream(file);
//和上面的方法等价
//write(int b):将指定的字节写入此文件输出流。
fos.write(97);
fos.write(98);
fos.write(99);
fos.write(100);
fos.write(101);
alt+enter自动生成左边内容
//write(byte[] b):将 b.length个字节从指定的字节数组写入此文件输出流。
// byte[] bys = {97,98,99,100,101};
byte[] bys = "abcde".getBytes();
fos.write(bys);
输出同上
//write(byte[] b, int off, int len):将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
// fos.write(bys,0,bys.length); //输出所有内容
fos.write(bys,1,3);
1.换行
package heima;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class P282 {
public static void main(String[] args) throws IOException {
//创建字节输出流对象
FileOutputStream fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\fos.txt");
//写数据
for (int i = 0;i<10;i++){
fos.write("hello".getBytes());
fos.write("\r".getBytes());
}
//释放资源
fos.close();
}
}
FileOutputStream fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\fos.txt",true);
作用:使每一个异常来源条理清晰
(虽然写程序来说,常用抛出操作)
package heima;
import java.io.FileOutputStream;
import java.io.IOException;
public class P283 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\fos.txt");
fos.write("hello".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
//加入finally来释放资源
if (fos != null){
try {
fos.close();
}catch (IOException e){
e.printStackTrace();
}
}
}
}
}
package heima;
import java.io.FileInputStream;
import java.io.IOException;
public class P284 {
public static void main(String[] args) throws IOException {
//创建字节输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\fos.txt");
//调用字节输入流对象的读数据方法 int read()
//第一次读取数据
int by = fis.read();
System.out.println(by);
System.out.println((char)by);
//第二次读取数据
by = fis.read();
System.out.println(by);
System.out.println((char)by);
//再多读取两次
by = fis.read();
System.out.println(by);
by = fis.read();
System.out.println(by);
//释放资源
fis.close();
}
}
//利用”到达文件末尾:-1“这一特性,进行循环
System.out.println("输入流的循环");
int by1 = fis.read();
while (by1 != -1){
System.out.println((char)by1);
by1 = fis.read();
}
//优化
System.out.println("循环读取的优化");
int by2;
while ((by2=fis.read())!=-1){
System.out.print((char) by2);
}
会自己读取换行符,所以print也可以换行
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
class Scratch {
public static void main(String[] args) throws IOException {
//根据数据源创建字节输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\fos.txt");
//根据数据源创建字节输出流对象
FileOutputStream fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\窗.txt");
//读写数据:复制文本文件
int by;
while ((by= fis.read())!=-1){
fos.write(by);
}
//释放资源
fos.close();
fis.close();
}
}
package heima;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class P286 {
public static void main(String[] args) throws IOException {
//创建字节输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\fos.txt");
//调用字节输入流对象的读数据方法
// int read(byte[] b):从该输入流读取最多b.length个字节的数据到一个字节数组中
byte[] bys = new byte[5];
//第一次读取数据
int len = fis.read(bys);
System.out.println(len);
System.out.println(new String(bys));
//第二次读取数据
len = fis.read(bys);
System.out.println(len);
System.out.println(new String(bys));
//第三次读取数据
len = fis.read(bys);
System.out.println(len);
System.out.println(new String(bys));
//释放资源
fis.close();
}
}
解析:
System.out.println(new String(bys,0,len));
byte[] bys = new byte[1024];
int len;
while ((len=fis.read(bys))!=-1){
System.out.print(new String(bys,0,len));
}
package heima;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class P287 {
public static void main(String[] args) throws IOException {
//输入流对象
FileInputStream fis = new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\main.png");
//输出流对象
FileOutputStream fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\main.png");
//复制图片(一次读取一个字节数组,一次写入一个字节数组)
byte[] bys = new byte[1024];
int len;
while ((len= fis.read(bys))!=-1){
fos.write(bys,0,len);
}
//释放资源
fos.close();
fis.close();
}
}
作用:提高效率
字节流输出流
//字节缓冲输出流 BufferedOutputStream(OutputStream out)
// FileOutputStream fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\bos.txt");
// BufferedOutputStream bos = new BufferedOutputStream(fos);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\\\Users\\\\路聃\\\\Desktop\\\\Java\\\\bos.txt"));
//写数据
bos.write("hello\r\n".getBytes());
bos.write("world\r\n".getBytes());
//释放资源
bos.close();
字节缓冲输入流
//字节流缓冲输入流:BufferedInputStream(InputStream in)
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\bos.txt"));
// //一次读取一个字节数据
int by;
while ((by=bis.read())!=-1){
System.out.print((char) by);
}
//一次读取一个字节数组数据
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
System.out.println(new String(bys,0,len));
}
主程序:
public static void main(String[] args) throws IOException {
//记录开始时间
long startTime = System.currentTimeMillis();
//复制视频
// method1();
// method2();
// method3();
method4();
//记录结束时间
long endTime = System.currentTimeMillis();
System.out.println("共耗时:"+(endTime - startTime)+"毫秒");
}
public static void method1() throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\1.mp4");
FileOutputStream fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\1.mp4");
int by;
while ((by = fis.read())!=-1){
fos.write(by);
}
//释放
fos.close();
fis.close();
}
public static void method2() throws IOException {
FileInputStream fis = new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\1.mp4");
FileOutputStream fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\1.mp4");
byte[] bys = new byte[1024];
int len;
while ((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
}
//释放
fos.close();
fis.close();
}
public static void method3() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\1.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\路聃\\Desktop\\2.mp4"));
int by;
while ((by = bis.read())!=-1){
bos.write(by);
}
//释放
bos.close();
bis.close();
}
public static void method4() throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\1.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\Users\\路聃\\Desktop\\2.mp4"));
byte[] bys = new byte[1024];
int len;
while ((len=bis.read(bys))!=-1){
bos.write(bys,0,len);
}
//释放
bos.close();
bis.close();
}
//字符流
String s = "中文";
// byte[] bys = s.getBytes();//[-28, -72, -83, -26, -106, -121]
// byte[] bys = s.getBytes("UTF-8");//[-28, -72, -83, -26, -106, -121]
byte[] bys = s.getBytes("GBK");//[-42, -48, -50, -60]
System.out.println(Arrays.toString(bys));
总结:
package heima;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
public class P292 {
public static void main(String[] args) throws UnsupportedEncodingException {
//定义一个字符集
String s = "中国";
//编码:使用平台的默认字符集将该String编码为一系列字节,将结果存储到新的字节数组中
byte[] bys = s.getBytes();//[-28, -72, -83, -27, -101, -67]
// byte[] bys = s.getBytes("UTF-8");//[-28, -72, -83, -27, -101, -67]
// byte[] bys = s.getBytes("GBK");//[-42, -48, -71, -6]
System.out.println(Arrays.toString(bys));
//解码:通过使用平台的默认字符集解码指定的字节数组来构造新的String
String ss = new String(bys);//中国
System.out.println(ss);
}
}
//解码:通过使用平台的默认字符集解码指定的字节数组来构造新的String
// String ss = new String(bys);//中国
// String ss = new String(bys,"UTF-8");//中国
String ss = new String(bys,"GBK");//涓浗
System.out.println(ss);
编码输入:
package heima;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class P293 {
public static void main(String[] args) throws IOException {
//OutputStreamWriter(OutputStream out) 创建一个使用默认字符编码的OutputStreamWriter。
//OutputStreamWriter(OutputStream out, String charsetName) 创建一个使用命名字符集的OutputStreamWriter。
// way1:
// FileOutputStream fos = new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\osw.txt");
// OutputStreamWriter osw = new OutputStreamWriter(fos);
// way2:
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\osw.txt"));
// way3:
// OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\osw.txt"),"UTF-8");
// err:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\osw2.txt"),"GBK");
osw.write("中国");
osw.close();
}
}
网课:使用GDK时乱码
我:GDK时仍然写入“中国”
//GDK读取
// InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\osw2.txt"));
//UTF-8读取
InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\osw2.txt"),"GBK");
//一次读取一个字符数据
int ch;
while ((ch=isr.read())!=-1){
System.out.println((char)ch);
}
isr.close();
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\write.txt"));
//write(int c) 写一个字符
osw.write(97);
此时记事本中没有任何内容:
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\路聃\\Desktop\\Java\\write.txt"));
//write(int c) 写一个字符
osw.write(97);
// void flush() 刷新流
osw.flush();
因为使用OutputStreamWriter,一开始数据是写在缓冲区的,记事本看不到,需要fush才能刷新写入
//write(int c) 写一个字符
osw.write(97);
// void flush() 刷新流
osw.flush();
osw.write(98);
osw.flush();
- 使用flush()刷新流
- 在末尾close()释放资源
//write(char[] cbuf) 写入一个字符数组
char[] chs = {'a','b','c','d','e'};
osw.write(chs);
char[] chs = {'a','b','c','d','e'};
osw.write(chs,1,3);
// osw.write("abcde",0,"abcde".length());//abcde
osw.write("abcde",1,3);
也可以读取.java文件:
package heima;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
public class P295 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\P293.java"));
// //read() 读一个字符
// int ch;
// while ((ch=isr.read())!=-1){
// System.out.print((char) ch);
// }
//read(char[] cbuf) 将字符读入数组
char[] chs = new char[1024];
int len;
while ((len = isr.read(chs))!=-1){
System.out.println(new String(chs,0,len));
}
isr.close();
}
}
package heima;
import java.io.*;
public class P296 {
public static void main(String[] args) throws IOException {
//创建一个字符集
InputStreamReader isr = new InputStreamReader(new FileInputStream("C:\\Users\\路聃\\Desktop\\Java\\P293.java"));
//根据目的地创建字符输出流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("C:\\Users\\路聃\\Desktop\\Copy.java"));
//复制文件
// //1.一次读写一个字符数据
// int ch;
// while ((ch=isr.read())!=-1){
// osw.write(ch);
// }
//2.一次读写一个字符数组数据
char[] chs = new char[1024];
int len;
while ((len=isr.read(chs))!=-1){
osw.write(chs);
}
}
}
package heima;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class P297 {
public static void main(String[] args) throws IOException {
//创建字符输入流对象
FileReader fr = new FileReader("C:\\Users\\路聃\\Desktop\\Java\\P293.java");
//创建字符输出流对象
FileWriter fw =new FileWriter("C:\\Users\\路聃\\Desktop\\Copy.java");
// //读写数据,复制文件
// int ch;
// while ((ch=fr.read())!=-1){
// fw.write(ch);
// }
char[] chs = new char[1024];
int len;
while ((len=fr.read(chs))!=-1){
fw.write(chs,0,len);
}
//释放资源
fw.close();
fr.close();
}
}
输出:
package heima;
import java.io.*;
public class P298 {
public static void main(String[] args) throws IOException {
// BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Java\\bw.txt"));
// bw.write("hello\r\n");
// bw.write("world\r\n");
// bw.close();
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\bw.txt"));
// int ch;
// while ((ch=br.read())!=-1){
// System.out.print((char) ch);
// }
char[] chs = new char[1024];
int len;
while ((len= br.read(chs))!=-1){
System.out.print(new String(chs,0,len));
}
br.close();
}
}
注意:
使用字符数组时,print和println效果一样
写入缓冲流的优势:
- 不需要同时创建输入输出流,两者分开时间创建也可以
- 提高效率
package heima;
import java.io.*;
public class P299 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\P293.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Copy.java"));
// int ch;
// while ((ch=br.read())!=-1){
// bw.write(ch);
// }
char[] chs = new char[1024];
int len;
while ((len= br.read(chs))!=-1){
System.out.print(new String(chs,0,len));
}
//释放
bw.close();
br.close();
}
}
package heima;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class P300 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Java\\bw.txt"));
for (int i = 0;i<10;i++){
bw.write("hello"+i);
// bw.write("\r\n");
bw.newLine(); //相当于上面的语句
bw.flush();
}
}
}
package heima;
import java.io.*;
public class P300 {
public static void main(String[] args) throws IOException {
// BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Java\\bw.txt"));
//
// for (int i = 0;i<10;i++){
// bw.write("hello"+i);
bw.write("\r\n");
// bw.newLine(); //相当于上面的语句
// bw.flush();
// }
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\bw.txt"));
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
br.close();
}
}
package heima;
import java.io.*;
public class P301 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\路聃\\Desktop\\Java\\P293.java"));
BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\路聃\\Desktop\\Copy.java"));
//使用字符缓冲流特有功能实现
String line;
while ((line=br.readLine())!=null){
bw.write(line);
bw.newLine();
bw.flush();
}
//释放资源
bw.close();
br.close();
}
}