Day23
一,file类
1.1file类的初识
import java.io.File;
import java.text.SimpleDateFormat;
public class Test01 {
public static void main(String[] args) {
File file = new File("C:\\Users\\何小宝\\Desktop\\hhy.txt");
System.out.println("获取文件名:" + file.getName());
System.out.println("获取绝对路径:" + file.getAbsolutePath());
System.out.println("获取文件大小(字节):" + file.length());
System.out.println("获取文件是否可读:" + file.canRead());
System.out.println("获取文件是否可写:" + file.canWrite());
System.out.println("获取文件是否隐藏:" + file.isHidden());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String datetime = sdf.format(file.lastModified());
System.out.println("获取最后的修改时间:" + datetime);
}
}
1.2获取绝对路径与相对路径
import java.io.File;
public class Test02 {
public static void main(String[] args) {
File file = new File("hhy.txt");
System.out.println("获取相对路径:" + file.getPath());
System.out.println("获取绝对路径:" + file.getAbsolutePath());
}
}
1.3创建文件
import java.io.File;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
File file = new File("file01\\hhy.txt");
if(!file.exists()){
file.createNewFile();
}
}
}
1.4创建单级文件夹
import java.io.File;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
File file = new File("file01\\hhy.txt");
File parentFile = file.getParentFile();
if(!parentFile.exists()){
parentFile.mkdir();
}
if(!file.exists()){
file.createNewFile();
}
}
}
1.5创建多级文件夹
import java.io.File;
import java.io.IOException;
public class Test03 {
public static void main(String[] args) throws IOException {
File file = new File("file01\\file02\\file03\\hhy.txt");
File parentFile = file.getParentFile();
if(!parentFile.exists()){
parentFile.mkdirs();
}
if(!file.exists()){
file.createNewFile();
}
}
}
1.6输出指定文件夹下的所有信息
import java.io.File;
import java.text.SimpleDateFormat;
public class Test01 {
public static void main(String[] args) {
File file = new File("D:\\2023");
File[] listFiles = file.listFiles();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
for (File f : listFiles) {
System.out.println(f.getName() + " -- " + sdf.format(f.lastModified()));
}
}
}
1.7输出指定文件夹下指定后缀的文件信息
import java.io.File;
public class Test02 {
public static void main(String[] args) {
File file = new File("D:\\2023");
File[] listFiles = file.listFiles();
for (File f : listFiles) {
String name = f.getName();
if (f.isFile() && name.endsWith(".txt")) {
System.out.println(name);
}
}
}
}
1.8输出指定文件夹下指定后缀的文件信息并过滤
import java.io.File;
public class Test04 {
public static void main(String[] args) {
File file = new File("D:\\2023");
search(file, ".jpg");
}
public static void search(File file,String suffix){
File[] listFiles = file.listFiles();
for (File f : listFiles) {
if(f.isDirectory()){
search(f, suffix);
}else if(f.isFile()){
String name = f.getName();
if(name.endsWith(suffix)){
System.out.println(name + " -- " + f.getAbsolutePath());
}
}
}
}
}
1.9列出当前目录下与子目录下所有符合条件的文件信息
import java.io.File;
public class Test04 {
public static void main(String[] args) {
File file = new File("D:\\2023");
search(file, ".jpg");
}
public static void search(File file,String suffix){
File[] listFiles = file.listFiles();
for (File f : listFiles) {
if(f.isDirectory()){
search(f, suffix);
}else if(f.isFile()){
String name = f.getName();
if(name.endsWith(suffix)){
System.out.println(name + " -- " + f.getAbsolutePath());
}
}
}
}
}
二,IO流
1,字节流
1.1文件字节输出流
import java.io.FileOutputStream;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("io.txt");
fos.write("123abcdef".getBytes(), 3, 6);
fos.close();
}
}
import java.io.FileOutputStream;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("io.txt",true);
fos.write("123abcdef".getBytes());
fos.close();
}
}
1.2文件字节输出流–处理异常
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test04 {
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream("io.txt",true);
fos.write("123abcdef".getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.3文件字节输入流
import java.io.FileInputStream;
import java.io.IOException;
public class Test05 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("io.txt");
int read = fis.read();
System.out.println((char)read);
read = fis.read();
System.out.println((char)read);
read = fis.read();
System.out.println((char)read);
read = fis.read();
System.out.println((char)read);
read = fis.read();
System.out.println((char)read);
read = fis.read();
System.out.println((char)read);
read = fis.read();
System.out.println(read);
fis.close();
}
}
import java.io.FileInputStream;
import java.io.IOException;
public class Test06 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("io.txt");
int read;
while((read = fis.read()) != -1){
System.out.println((char)read);
}
fis.close();
}
}
import java.io.FileInputStream;
import java.io.IOException;
public class Test07 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("io.txt");
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
fis.close();
}
}
1.4文件字节输入流–处理异常
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test08 {
public static void main(String[] args){
FileInputStream fis = null;
try {
fis = new FileInputStream("io.txt");
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
1.5拷贝文件
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy02 {
public static void main(String[] args) {
try(FileInputStream fis = new FileInputStream("水野朝阳.mp4");
FileOutputStream fos = new FileOutputStream("copy.mp4");){
byte[] bs = new byte[1024];
int len;
while((len = fis.read(bs)) != -1){
fos.write(bs, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.6带缓冲区的字节输出流与输出流—拷贝文件
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("io.txt",true));
bos.write("123abcdef".getBytes());
bos.close();
}
}
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("io.txt"),2048);
byte[] bs = new byte[1024];
int len;
while((len = bis.read(bs)) != -1){
System.out.println(new String(bs, 0, len));
}
bis.close();
}
}
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public static void main(String[] args) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream("水野朝阳.mp4"));
bos = new BufferedOutputStream(new FileOutputStream("copy.mp4"));
byte[] bs = new byte[1024];
int len;
while((len = bis.read(bs)) != -1){
bos.write(bs, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
2,字符流
2.1字符输出转换流,字符输入转换流,拷贝文件
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Test01 {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("io.txt",true),"GBK");
osw.write("123abc木头人", 3, 6);
osw.close();
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test03 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("io.txt"),"GBK");
char[] cs = new char[1024];
int len;
while((len = isr.read(cs)) != -1){
System.out.println(new String(cs,0,len));
}
isr.close();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class Copy {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("小说.txt"), "GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("copy.txt"), "GBK");
char[] cs = new char[1024];
int len;
while((len = isr.read(cs)) != -1){
osw.write(cs, 0, len);
}
isr.close();
osw.close();
}
}
2.2文件字符输出流,文件字符输入流,拷贝文件
import java.io.FileWriter;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
FileWriter fw = new FileWriter("io.txt",true);
fw.write("123abc木头人");
fw.close();
}
}
import java.io.FileReader;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("io.txt");
char[] cs = new char[1024];
int len;
while((len = fr.read(cs)) != -1){
System.out.println(new String(cs, 0, len));
}
fr.close();
}
}
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class Copy {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
FileReader fr = new FileReader("小说.txt");
FileWriter fw = new FileWriter("copy.txt");
char[] cs = new char[1024];
int len;
while((len = fr.read(cs)) != -1){
fw.write(cs, 0, len);
}
fr.close();
fw.close();
}
}
2.3带缓冲区字符输出流,带缓冲区字符输入流,拷贝文件
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("io.txt",true));
bw.write("123abc木头人");
bw.close();
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test02 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("io.txt"), "GBK"));
char[] cs = new char[1024];
int len;
while((len = br.read(cs)) != -1){
System.out.println(new String(cs, 0, len));
}
br.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class Copy01 {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
BufferedReader br = new BufferedReader(new FileReader("小说.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
char[] cs = new char[1024];
int len;
while((len = br.read(cs)) != -1){
bw.write(cs, 0, len);
}
br.close();
bw.close();
}
}
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class Copy02 {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
BufferedReader br = new BufferedReader(new FileReader("小说.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
String readLine;
while((readLine = br.readLine()) != null){
bw.write(readLine);
bw.newLine();
}
br.close();
bw.close();
}
}