IO学习笔记
IO
File
概述
构造方法
代码实现:
public class FileDemo001 {
public static void main(String[] args) {
File f1 = new File("D:\\file\\file.txt");
System.out.println(f1);
File f2 = new File("D:\\file","file.txt");
System.out.println(f2);
File f3 = new File("D:\\file");
File f4 = new File(f3,"file.txt");
System.out.println(f4);
}
}
结果:
D:\file\file.txt
D:\file\file.txt
D:\file\file.txt
File类创建功能
代码实现:
public class FileDemo002 {
public static void main(String[] args) throws IOException {
File f1 = new File("D:\\file\\java.txt");
System.out.println(f1.createNewFile());
File f2 = new File("D:\\file\\javac");
System.out.println(f2.mkdir());
File f3 = new File("D:\\file\\javase\\io");
System.out.println(f3.mkdirs());
File f4 = new File("D:\\file\\javac.txt");
//要根据调用的方法来判断创建的是什么类型文件
//System.out.println(f4.mkdir());//java.txt文件夹
System.out.println(f4.createNewFile());//java.txt
}
}
结果:
true
true
true
true
File类判断与获取功能
代码实现:
public class FileDemo003 {
public static void main(String[] args) {
File f1 = new File("D:\\file\\java.txt");
System.out.println(f1.isDirectory());
System.out.println(f1.exists());
System.out.println(f1.isFile());
System.out.println(f1.getAbsolutePath());
System.out.println(f1.getName());
System.out.println(f1.getPath());
System.out.println("========分隔符======");
File f2 = new File("D:\\file");
String[] list = f2.list();
for (String s : list) {
System.out.println(s);
}
System.out.println("========分隔符======");
File[] files = f2.listFiles();
for (File f : files) {
System.out.println(f);
}
}
}
结果:
false
true
true
D:\file\java.txt
java.txt
D:\file\java.txt
========分隔符======
file.txt
java.txt
javac
javac.txt
javase
========分隔符======
D:\file\file.txt
D:\file\java.txt
D:\file\javac
D:\file\javac.txt
D:\file\javase
IO流
概述
字节流写数据
使用字节输出流写数据的步骤:
代码实现:
public class FileOutputStreamDemo001 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream("基础语法\\fos.txt");
fos.write(97);
//关闭资源
fos.close();
}
}
这里涉及到了资源关闭问题,附上链接
字节流写数据的几种方法·
代码实现:
public class FileOutputStreamDemo002 {
public static void main(String[] args) throws IOException {
FileOutputStream fos = new FileOutputStream(new File("基础语法\\fos.txt"));
//第一种
fos.write(55);
fos.write(56);
fos.write(57);
fos.write(58);
//第二种
byte[] bys = {55,56,57,58};
fos.write(bys);
//第三种
byte[] b = "abcde".getBytes();
fos.write(b,0,5);
}
}
拓展两个小问题:
字节流读数据
一次读一个字节
代码实现:
public class FileIntputStreamDemo001 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("基础语法\\fos.txt");;
int by;
while((by=fis.read())!=-1){
System.out.print((char)by);
}
fis.close();
}
}
结果:
abcde
Process finished with exit code 0
案例复制文本文件
代码实现:
一次读一个字节数组数据
代码实现:
public class FileIntputStreamDemo002 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("基础语法\\fos.txt");
byte[] bys = new byte[1024];
int len = 0;
while((len = fis.read(bys))!=-1){
System.out.println(new String(bys,0,len));
}
fis.close();
}
}
结果:
abcde
efghf
案例:复制图片
字节缓冲流
案例:复制视频
代码实现:
字符流
为什么出现字符流
字符流写数据的几种方式
public class OutputStreamWriterDemo001 {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("基础语法\\osw.txt"));
osw.write(97);
osw.flush();//刷新
char[] ch = {'a','b','c','d','e'};
osw.write(ch);
osw.write(ch,0,4);
osw.write("abcde");
osw.write("abcde",0,"abcde".length());
osw.close();
}
}
字符流读数据
代码实现:
public class OutputStreamWriterDemo002 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("基础语法\\isr.txt"));
int ch;
while ((ch = isr.read())!= -1){
System.out.print((char)ch);
}
byte[] bys = new byte[1024];
int len;
while ((len = isr.read())!= -1){
System.out.print(new String(bys,0,len));
}
}
}
案例:复制java文件
代码改进:
字符缓冲流
代码实现:
案例:复制java文件*
特有功能
案例:复制java文件