文件
1. 两种路径
//1,绝对路径 从盘符开始的路径
File file1 = new File("D:\\fil\\a.txt");
//2,相对路径 相对于工程的路径
// ../返回上一层目录
File file2 = new File("./fil/a.txt ");
2. File常用的方法
File file1 = new File("./fil");
file1.exists()); //文件或目录是否存在
file1.length()); //文件大小 字节个数
file1.getName()); //文件或目录名称
file1.canRead()); //是否可读
file1.canWrite()); //是否可写
file1.isFile()); //是文件吗
file1.isDirectory()); //是目录吗
file1.lastModified()); //最近修改时间
file1.getParent()); //获取父路径
file1.getPath()); //获取路径
file1.getAbsolutePath()); //获取绝对路径
3. 斐波那契数列
public static int fibonacci(int num) {
if (num == 0 || num == 1) {
return num;
}
// 递归调用
return fibonacci(num - 1) + fibonacci(num - 2);
}
IO
1. 字节流
1.1 文件字节流输入
1. //流对象要进行关闭 fileInputStream.close();放到finally中
FileInputStream fileInputStream = new FileInputStream("./fil/a.txt");
//读取数据
int num;
while ((num = fileInputStream.read()) != -1) {
System.out.println(num);
}
//一次性读取所有字节数据 到字节数组中;如果是大文件 有可能报内存溢出
byte[] readAllBytes = fileInputStream.readAllBytes();
//readNBytes 只读取指定字节个数的数据出来
byte[] bytes = fileInputStream.readNBytes(10);
2. //try 新语法 自动关闭流对象
try (FileInputStream fileInputStream = new FileInputStream("./fil/a.txt")) {
byte[] bytes = fileInputStream.readAllBytes();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
1.2 文件字节流输出
private static void method5() {
//文件字节输出流 如果文件不存在会自动创建 前提条件是父路径要存在否则报异常 append:false覆盖 append:true追加
try (FileOutputStream fileOutputStream = new FileOutputStream("./fil/out.txt", true)) {
fileOutputStream.write(97);
byte[] bytes = "abcdefg".getBytes();
fileOutputStream.write(bytes);
fileOutputStream.write(bytes, 0, 6);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
1.3 高效/缓存字节流
//缓存字节输出流什么时候会真正的写?1 内部数组满了 2 流对象关闭 会把缓存中的数据一次性更新到硬盘文件中
try (BufferedInputStream bufferedInputStream =
new BufferedInputStream(new FileInputStream("./fil/a.txt"));
BufferedOutputStream bufferedOutputStream =
new BufferedOutputStream(new FileOutputStream("./fil/out.txt"))) {
//read方法依然是每次读取一个字节数据出来 如果读到流的末尾返回值是-1
int num;
while ((num = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(num);
}
//readAllBytes 一次性读取所有字节数据
//byte[] bytes = bufferedInputStream.readAllBytes();
//byte[] bytes1 = bufferedInputStream.readNBytes(10);
//BufferedInputStream.transferTo(bufferedOutputStream)内容拷贝
bufferedOutputStream.write(97);
bufferedOutputStream.write("abc".getBytes());*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2. 字符流
2.1 文件字符流
try (FileReader fileReader = new FileReader("./fil/a.txt");
// 如果文件不存在能自动创建 append:true 表示追加
FileWriter fileWriter = new FileWriter("./fil/out.txt")) {
//read一次读取一个字符的数据进来 如果读取完毕返回值是-1
int num;
while ((num = fileReader.read()) != -1) {
//char->int
System.out.println((char) num);
}
fileWriter.write(97);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
2.2 高效字符流
//什么时候真正的写? 1 数组满了 2 流对象关闭
try (BufferedReader bufferedReader =
new BufferedReader(new FileReader("./fil/a.txt"));
BufferedWriter bufferedWriter =
new BufferedWriter(new FileWriter("./fil/out.txt"))) {
// read 一次读取一个字符的数据进来 如果读取完毕返回值是-1
//int read = bufferedReader.read();
//readLine一次性读取一行数据;如果读取完毕返回值是null
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
bufferedWriter.write(100);
//文件内容拷贝
bufferedReader.transferTo(bufferedWriter);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
标签:file1,Java,字节,num,IO,new,txt,fil
From: https://www.cnblogs.com/jh0129/p/17228463.html