一,输入,输出的流向
二.File类
import java.io.File;
import java.io.IOException;
public class Filr {
public static void main(String[] args) throws IOException {
//构造方法
//File file = new File("D:\\demo\\a.txt");//根据一个路径得到File对象
//File file1 = new File("D:\\demo","a.txt");//根据一个目录和一个子文件/目录得到File对象
File file2 = new File("a");//这个路径可以是文件夹的路径,还可以是不存在的路径
//File file3 = new File(file2,"a.txt");//根据一个父File对象和一个子文件/目录得到File对象
//三种方式效果一样
//成员方法
//创建功能
File f = new File("c.txt");//创建文件 如果存在这样的文件,就不创建了
boolean newFile = f.createNewFile();
boolean mkdir = file2.mkdir();//创建文件夹,如果存在这样的文件夹,就不创建了
//删除功能
//boolean delete = f.delete();
//注:要删除一个文件夹,请注意该文件夹内不能包含文件或者文件夹
//file2.delete();
//重命名功能
File f2 = new File("b.txt");
f.renameTo(f2);//如果路径名相同,就是改名。
//判断功能
boolean directory = f.isDirectory();//判断是否是目录
System.out.println(directory);
boolean file = f.isFile();//判断是否是文件
/*public boolean exists():判断是否存在
public boolean canRead():判断是否可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是否隐藏
*/
//基本路径:
String absolutePath = f.getAbsolutePath();//获取绝对路径
String path = f.getPath();//获取相对路径
String name = f.getName();//获取名称
long length = f.length();//获取长度
String[] list = f.list();//返回该路径下所有的文件/文件夹名称
File[] files = f.listFiles();//返回返回该文件夹下的所有的文件对象
}
}
三,IO流的的分类
①根据操作单位:字节流 字符流
②根据方向:字节流 字符流
字节输入流:InputStream--抽象父类-不能实例化
FileInputStream--文件字节输入流,构造函数的参数:File、String
BuffereInputStream--高效字节输入流
import java.io.*;
public class Demo {
public static void main(String[] args) throws IOException {
//method1();//普通字节流读取
method2();//高效字节流读取
}
private static void method2() throws IOException {
InputStream in=null;
try {
//new BufferedInputStream(new FileInputStream("b.txt"));
in= new BufferedInputStream(new FileInputStream(new File("b.txt")));
int b;
while((b=in.read())!=-1){
System.out.println(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
in.close();
}
}
private static void method1() throws IOException {
InputStream in=null;
try {
in = new FileInputStream(
new File("b.txt"));
//InputStream in = new FileInputStream("a.txt");
System.out.println(in.read());
System.out.println(in.read());
System.out.println(in.read());
/*while(true){
if (in.read()!=-1){
System.out.println(in.read());
}
}*///不可以,这种写法会跳着读,一部分数据用来做判断条件。没有打印,会丢失数据
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
in.close();
}
}
}
字节输出流:OutputStream--抽象父类,不能实例化
FileOutputStream--文件字节输出流
BufferedOutputStream--高校字节输出流
import java.io.*;
public class Demo3 {
public static void main(String[] args) throws IOException {
//method1();
method2();
}
private static void method2() throws IOException {
OutputStream out =null;
try {
out =new BufferedOutputStream(new FileOutputStream(new File("b.txt")));
out.write(1);
out.write(2);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
out.close();
}
}
private static void method1() {
OutputStream out = null;
try {
out=new FileOutputStream(new File("b.txt"));
out.write(97);
out.write(98);
out.write(99);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字符输入流:Reader --抽象类--不能实例化
FileReader--文件字节输入流
BufferedRrader--高校字节输入流
import java.io.*;
public class Demo2 {
public static void main(String[] args) throws IOException {
// method1();
method2();
}
private static void method2() throws IOException {
Reader in= null;
try {
in=new BufferedReader(new FileReader(new File("b.txt")));
int b;
while((b=in.read())!=-1){
System.out.println(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
in.close();
}
}
private static void method1() throws IOException {
Reader in=null;
//创建对象
try {
//Reader in = new FileReader("b.txt");
in = new FileReader(new File("b.txt"));
int b;
//读取内容
while((b=in.read())!=-1){
System.out.println(b);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关流
in.close();
}
}
}
字符输出流:Writer
字符输出流:Writer--抽象弗列不能实例化
FileWriter
BufferedWriter
import java.io.*;
public class Demo4 {
public static void main(String[] args) {
method1();
method2();
}
private static void method2() {
BufferedWriter out=null;
try {
out = new BufferedWriter(new FileWriter(new File("b.txt")),true);//(false)覆盖和(true)追加
out.write(2);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static void method1() {
Writer out =null;
try {
out =new FileWriter(new File("b.txt"));
out.write(5);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}