File 概述:(操作文件的)
File 是表示一个文件夹或文件路径,路径分为绝对路径和相对路径。
绝对路径:(固定的路径,从盘符开始:E:\Swing NetBeans\a)
相对路径:(相对于某个位置,就是在自己的目录下或者和自己在同一个目录)
构造方法:
File(String pathname):根据一个路径得到File对象
File(String parent, String child):根据一个目录和一个子文件/目录得到File对象
File(File parent, String child):根据一个父File对象和一个子文件/目录得到File对象
File(URI uri): 通过将给定的 file: URI 转换为一个抽象路径名来创建一个新的 File 实例
public class FileUpload {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file = new File("D:\\Test\\aaawe");
// File file2 = new File(parent, child);
// File file3 = new File(parent, child);
File file4 = new File("D:\\Test\\aaa","ass");
//exists()判断文件夹是否存在,存在true
if(file.exists()==false){
file.mkdirs();//没有文件夹,创建
}
if(file4.exists()==false){
file4.mkdirs();
}
}
}
I/O流
Input/Output 输入与输出 读与写操作
2.基本分类:
字节流:是以byte为单位进行读写操作,可以操作任何文件。
字符流:字符(2个字节)为单位进行读写操作,只能读写文本文件。
数据流动分类(程序角度)
输入流:从一个文件当中读取数据到程序中,读文件
输出流:从程序中把数据输出到文件中,写文件
2.1字节流:
InputStream:(输入流)
常用方法:read(); 从输入流中读取数据的下一个字节
read(byte[] b);从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。
read(byte[] b,int off,int len); 将输入流中最多 len 个数据字节读入 byte 数组。
close(); 关闭此输入流并释放与该流关联的所有系统资源。
public static void FileInput(){
File file = new File("D:\\Test\\aaawe\\was.txt");
try {
//吧文件放入输入流中
InputStream inputStream = new FileInputStream(file);
//缓冲数组,给1024个字符长度。用完再new一个byte数组
byte [] b = new byte[1024];
int len = 0;//定义一个len来接收输入流读的字节
try {
//inputStream.read(b):读字节,当把字节读完后,read就会返回一个-1
//跳出当前循环
while((len=inputStream.read(b))!=-1){
// System.out.print((char)len);
// 分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
//String(char[] value, int offset, int count)
// 数组,读入数据的缓冲区 开始遍历字节的位置 读取的最大字节量
String info = new String(b, 0, len);
System.out.println(info);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
OutputStream:(输出流)此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器
常用方法:write(byte[] b) 将 b.length 个字节从指定的 byte 数组写入此输出流。
write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。
write(int b) 将指定的字节写入此输出流。
flush() 刷新此输出流并强制写出所有缓冲的输出字节。
close()关闭此输出流并释放与此流有关的所有系统资源。
public static void Output(){//输出流
//File file = new File("D:\\Ases\\was.txt");
File file = new File("D:\\Test\\aaawe\\was.txt");
try {
//吧文件方法哦输出流
OutputStream os = new FileOutputStream(file);
//字符串
String str = "其实你真的好帅,但是没有人爱你";
//吧字符串放到byte数组中,缓存区
byte[] bytes = str.getBytes();
try {
//吧字节写入输出流
os.write(bytes, 0, bytes.length);
//刷新缓存,输出字节
os.flush();
//关闭输出流
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("找不到文件异常");
}
}
一个完整的输入,输出操作。这些代码都是大部分固定格式的
public static void main(String[] args) {
//System.out.println("Hello world");
try {
//实例化输入流,找到要输入的文件位置
InputStream is = new FileInputStream("D:\\Test\\aaa\\aaass\\Demo.java");
//实例化输出流,找到要输出的文件位置
OutputStream os = new FileOutputStream("D:\\Test\\aaa\\ass\\we.java ");
int len = 0;
//缓存数组,给1024个字节,用完就会再给一个byte数组
byte [] b = new byte[1024];
// len读到的字节数量,读完,没字节读,read返回-1
while((len=is.read(b))!=-1){
//写出去,b:缓存数组 0:开始写出的字节位置 len:写出的字节数()
os.write(b, 0, len);
}
//System.out.println(b[2]);
os.flush();//清空输出流的缓存
is.close();//关闭输入流
os.close();//关闭输出流
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("找不到文件");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2.2字符流
输入:Reader(用于读取字符流的抽象类)
常用方法:read() 读取单个字符
read(char[] cbuf) 将字符读入数组。
read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。
reset() 重置该流。
close() 关闭该流并释放与之关联的所有资源。
输出:Writer
常用方法:write(char[] cbuf) 写入字符数组。
write(char[] cbuf, int off, int len) 写入字符数组的某一部分。
write(int c) 写入单个字符。
write(String str) 写入字符串。
write(String str, int off, int len) 写入字符串的某一部分。
flush()/close() 刷新该流的缓冲 关闭流