IO流
1.文件的创建方式
//创建文件夹
// 方式一
File ioDemo = new File("E:\\IODemo");
ioDemo.mkdir();
// 方式二
File ioDemo02 = new File("E:\\", "IODemo02");
ioDemo02.mkdir();
// 方式三
File ioDemo03 = new File(new File("E:\\"), "IODemo03");
ioDemo03.mkdir();
//创建文件
File file = new File("E:\\IODemo02", "demo.txt");
file.createNewFile();
2.File的基本方法
File file = new File("E:\\IODemo02", "demo.txt");
System.out.println(file.createNewFile());
System.out.println("getName="+file.getName());
System.out.println("getPath="+file.getPath());
System.out.println("getAbsolutePath="+file.getAbsolutePath());
System.out.println("isFile="+file.isFile());
System.out.println("isDirectory="+file.isDirectory());
System.out.println("exists="+file.exists());
3.字节输入输出流
// 字节输入流
// 绑定文件
FileInputStream fileInputStream = new FileInputStream("E:\\IODemo02\\FileInputStreamDemo.txt");
int a = 0;
// 对fileInputStream进行遍历,fileInputStream.read()返回值为值的ASCii值
while ((a = fileInputStream.read()) != -1) {
// 将结果转为字符形式
System.out.print((char)a);
}
if (fileInputStream != null){
// 关闭流
fileInputStream.close();
}
// 字节输出流
// 绑定文件
FileOutputStream fileOutputStream = new FileOutputStream("E:\\IODemo02\\FileOutputStreamDemo.txt");
// 添加内容
byte[] bytes = {'a','b','c','d'};
// 写入数据
fileOutputStream.write(bytes);
// 关闭流
if (fileOutputStream != null){
fileInputStream.close();
}