java中IO流总结
java io流总体结构
File类
1.使用File类的方法创建E:\haha目录
public static void main(String[] args) {
// File dir=new File("E:\\test.txt");
File dir=new File("E:"+File.separator+"test.txt");
dir.mkdir();//创建目录
try {
dir.createNewFile();//创建文件
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(File.pathSeparator);//系统之间的目录分隔符
System.out.println(File.separator);//系统默认使用的分隔符
}
2 .创建c:\haha\hehe\test.txt
首先创建c:\haha目录
在创建test.txt文件
File parent=new File("E:"+File.separator+"haha"+File.separator+"hehe");
File child=new File(parent,"test.txt");
if(!parent.exists()){//检查父目录是否已经存在
// parent.mkdir();一次只能创建一个目录
parent.mkdirs();//一次能创建多个目录
child.createNewFile();//创建子文件
System.out.println("文件创建成功");
}
else{
child.createNewFile();
}
for(int i=0;i<100;i++){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("文件的绝对路径"+child.getAbsolutePath());
System.out.println("文件的姓名:"+child.getName());
System.out.println(child.getParent());//获得父目录的路径名
// 删除文件
child.delete();
System.out.println("删除成功!");
File file=new File("D:\\石光辉");
File [] files=file.listFiles();//返回此抽象路径名下的所有文件
for (File f: files) {
System.out.println(f.getName());
}
System.out.println(file.isDirectory());//判断是否为目录
long a=file.lastModified();//获得此文件最后一次修改的时间
Date date=new Date(a);
System.out.println(date);
3.文件拷贝的例子,使用的是字节流
字节输入流:Inputstream 子类:FileInputStream(File file)
字节输出流:OutputStream 子类:FileOutputStream(File file)
里面的方法请详细看API文档
// 文件的拷贝
File src = new File("E:" + File.separator + "haha" + File.separator + "src.jpg");// 源文件
File des = new File("E:" + File.separator + "haha" + File.separator + "des.jpg");// 目标文件
InputStream in = null;
OutputStream out = null;
// FileInputStream fin;
// FileOutputStream fout;
try {
in = new FileInputStream(src);//字节输入流对象
out = new FileOutputStream(des);//字节输出刘对象
// 读取源文件
byte[] buf = new byte[1024];// 缓冲区
int temp;// 获取每次读取的字节的个数
try {
while ((temp = in.read(buf)) != -1) {// 判断是否读到文件的结尾
out.write(buf);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
4.使用字节输出流向文件中写内容,并用字节输入流将内容从文件中读取出来
public static void main(String[] args) throws FileNotFoundException {
String s="床前明月光\r\n疑是地上霜\r\n举头望明月\r\n我是郭德纲";
File file=new File("D:"+File.separator+"test.txt");
OutputStream out=null;
InputStream in=null;
out =new FileOutputStream(file);//字节输出流对象
in=new FileInputStream(file);//字节输入流对象
try {
byte [] bytes=s.getBytes();
out.write(bytes); //把字符串转换成字节数组 写入文件
out.close();
// 写完了之后 读取文件
byte [] buf=new byte[(int)file.length()];
in.read(buf);//把文件内容读到缓冲区
String temp=new String(buf);
System.out.println(temp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
字符流输入流:Reader 子类:InputStreamReader(InputStream in)
字符输出流:Writer 子类:OutputStreamWriter(OutputStream out)
FileReader类 :用来读取字符文件的便捷类
具体使用参考API文档
public class ReaderDemo {
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream=new FileOutputStream("D:\\sgh.txt");//字节输出流对象
//使用字符输出流向文件中写内容
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);//字符输出流对象
outputStreamWriter.write("asdfgh");
outputStreamWriter.close();
fileOutputStream.close();
//使用字符输入流从文件中读取数据
FileInputStream fileInputStream=new FileInputStream("D:\\sgh.txt");//字节输入流对象
InputStreamReader inputStreamReader=new InputStreamReader(fileInputStream);//字符输入流对象
int i;
//每次读取1个字符
while((i=inputStreamReader.read())!=-1){
System.out.println((char) i);
}
inputStreamReader.close();
fileInputStream.close();
5.缓冲流
缓冲输入流:BufferedReader(Reader in)
缓冲输出流:BufferedWriter(Writer out)
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(new FileInputStream(new File("D:\\dgh.txt"))));//缓冲输入流对象
String s= bufferedReader.readLine();//每次读取一行
System.out.println(s);
String string;
int i=0;
while((string=bufferedReader.readLine())!=null){//判断是否读到每一行的末尾
System.out.println(s);
i++;
}
bufferedReader.close();
}