File
File的创建方法
第一个方法createNewFile()
注意:如果不存在文件 就创建成功 如果存在 就创建失败
/*
File类 创建文件
*/
public class Demo02 {
public static void main(String[] args) throws IOException {
File file = new File("G:\\File\\java.txt");
System.out.println(file.createNewFile());//true 创建文件成功
}
}
第二个方法file.mkdir()
/*
File类 创建目录
*/
public class Demo03 {
public static void main(String[] args) throws IOException {
File file = new File("G:\\File\\java");
System.out.println(file.mkdir());//true 创建成功
}
}
第三个方法 创建多级目录
/*
创建多级目录
*/
File file1 = new File("G:\\File\\hah\\ee\\66");
System.out.println(file1.mkdirs());
}
File类的删除功能
/*
File类 创建文件
*/
public class Demo04 {
public static void main(String[] args) throws IOException {
File file = new File("java04.txt");//将在本模块中创建
// System.out.println(file.createNewFile());//true 创建文件成功
//再删除
file.delete();//删除了
}
}
递归求阶乘
/*
递归求阶乘
*/
public class Demo {
public static void main(String[] args) {
int f = f(15);
System.out.println(f);
}
public static int f(int n){
if (n==1){
return 1;
}else {
return n*f(n-1);
}
}
}
递归调用目录
/*
递归目录 输出文件的全路径名
*/
public class Demo01 {
/*结果
G:\File\hah\ee\66\新建文本文档.txt
G:\File\java\新建文件夹\新建位图图像.bmp
G:\File\java.txt
*/
public static void main(String[] args) {
File file = new File("G:\\File");
allFile(file);
}
//创建递归方法
public static void allFile(File file){
//获取所有的文件
File[] listFiles = file.listFiles();
//先判断目录是否为空
if (listFiles!=null){
for (File listFile : listFiles) {
if (listFile.isFile()){
System.out.println(listFile.getAbsolutePath());
}else{
allFile(listFile);
}
}
}
}
字节流
字节流写数据
代码
public class Demo01 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("hello.txt");
fileOutputStream.write(97);//a
}
}
字节流写数据的三种方式
代码
public class Demo02 {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("world.txt");
//写数据方式一 一次写一个
fileOutputStream.write(97);
fileOutputStream.write(98);
fileOutputStream.write(99);//abc
//f方式二 一次写一个数组
byte[] bytes = "abcde".getBytes(StandardCharsets.UTF_8);
fileOutputStream.write(bytes);//abc abcde
//方式三 写一个数组 从索引开始 到长度结束
fileOutputStream.write(bytes,1,3);//数组bytes 从索引1开始 往后写三个 abc abcde bcd
}
}
写入的两个小问题
两个小问题 写数据实现换行 实现追加写入
public class Demo03 {
public static void main(String[] args) throws IOException {
/*
两个小问题 写数据实现换行
实现追加写入
*/
FileOutputStream fileOutputStream = new FileOutputStream("world.txt", true);//加一个true就代表实现追加写书数据
for (int i = 0; i < 10; i++) {
fileOutputStream.write("hello".getBytes(StandardCharsets.UTF_8));
fileOutputStream.write("\r\n".getBytes());//加换行符
}
//释放资源
fileOutputStream.close();
}
}
运行两次 就添加了20个hello字符
异常处理
/*
写数据异常处理
*/
public class Demo04 {
public static void main(String[] args) {
FileOutputStream fileOutputStream=null;
try {
fileOutputStream = new FileOutputStream("world.txt");
fileOutputStream.write("Demo04".getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字节流读数据
一个一个读取
注意:
import lombok.SneakyThrows;
@SneakyThrows 是注解 异常注解
代码 多次读取后 文件没有内容就返回-1
/*
读数据 一个一个读取
*/
public class Demo01 {
@SneakyThrows
public static void main(String[] args) {
FileInputStream fileInputStream = new FileInputStream("hello.txt");
int read = fileInputStream.read();
System.out.println(read);
fileInputStream.close();
//多读取几次 直到文件里面没有内容了 就返回-1;
}
}
优化 使用循环实现
/*
读数据 一个一个读取
优化 通过循环来实现
*/
public class Demo02 {
@SneakyThrows
public static void main(String[] args) {
FileInputStream fileInputStream = new FileInputStream("java.txt");
int bytes;
while ((bytes=fileInputStream.read())!=-1){
System.out.print((char)bytes);
}
}
}
案例
复制文本文件
准备文件
C:\\Users\\lsc1999\\Desktop\\jdbc.properties 目录
代码
首先创建读取文件
//读取文件
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\lsc1999\\Desktop\\jdbc.properties");
然后创建写入文件
//创建本模块的写入文件 有好多个 用hello.txt
FileOutputStream fileOutputStream = new FileOutputStream("hello.txt");
代码实现
public class Demo {
@SneakyThrows
public static void main(String[] args) {
//创建本模块的写入文件 有好多个 用hello.txt
FileOutputStream fileOutputStream = new FileOutputStream("hello.txt");
//读取文件
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\lsc1999\\Desktop\\jdbc.properties");
int bytes;
while ((bytes=fileInputStream.read())!=-1){
fileOutputStream.write(bytes);//吧读的文件写入到本模块的文件中
}
}
}
一次读一个字节数组数据
代码实现
/*
读数据 一次读取一个字节数组
*/
public class Demo03 {
@SneakyThrows
public static void main(String[] args) {
FileInputStream fileInputStream = new FileInputStream("hello.txt");
byte[] bytes = new byte[1024];
int by;
while ((by=fileInputStream.read(bytes))!=-1){
System.out.print(new String(bytes,0,by));
}
}
}
复制图片
public class Demo {
@SneakyThrows
public static void main(String[] args) {
//读取图片文件
FileInputStream fileInputStream = new FileInputStream("C:\\Users\\lsc1999\\Desktop\\ll.jpg");
//创建写入文件
FileOutputStream fileOutputStream = new FileOutputStream("jj.jpg");
//一次读一个字节数组
byte[] bytes = new byte[1024];
int by;
while ((by=fileInputStream.read(bytes))!=-1){
fileOutputStream.write(bytes,0,by);
}
fileInputStream.close();
fileOutputStream.close();
}
}
字节缓冲流
BufferOutputStreamDemo
public class BufferOutputStreamDemo {
@SneakyThrows
public static void main(String[] args) {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("world.txt"));
bufferedOutputStream.write("hello\r\n".getBytes());//hello
bufferedOutputStream.write("world\r\n".getBytes());//world
//释放资源
bufferedOutputStream.close();
}
}
BufferInputStreamDemo
public class BufferInputStreamDemo {
@SneakyThrows
public static void main(String[] args) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("hello.txt"));
byte[] bytes = new byte[1024];
int by;
while ((by=bufferedInputStream.read(bytes))!=-1){
System.out.println(new String(bytes,0,by));
}
bufferedInputStream.close();
}
/*
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
jdbc.username=root
jdbc.password=111111
*/
}
案例 复制视频
public class Demo {
@SneakyThrows
public static void main(String[] args) {
//读取视频信息
BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream("C:\\Users\\lsc1999\\Desktop\\ll.mp4"));
//本模块创建复制视频
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("lll.mp4"));
byte[] bytes = new byte[1024];
int by;
while ((by = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, by);
}
bufferedInputStream.close();
bufferedOutputStream.close();
}
}
字符流
io小结
案例 把集合中的元素写到文件里面去
public class Demo {
@SneakyThrows
public static void main(String[] args) {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("hello");
arrayList.add("world");
arrayList.add("java");
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("java.txt"));
for (String s : arrayList) {
bufferedWriter.write(s);
bufferedWriter.newLine();
bufferedWriter.flush();
}
bufferedWriter.close();
}
}
案例 把文件中的元素写到集合里面并输出
public class Demo {
@SneakyThrows
public static void main(String[] args) {
BufferedReader bufferedReader = new BufferedReader(new FileReader("hello.txt"));
ArrayList<String> strings = new ArrayList<>();
String ch;
while ((ch=bufferedReader.readLine())!=null){
strings.add(ch);
}
for (String st : strings) {
System.out.println(st);
}
}
}
特殊操作流