IO流
一.File类
1.File 类对象的创建
File file=new File("绝对地址OR相对地址");
将文件或目录封装成Java的一个对象
2.File类常用方法
exist():判断文件或目录是否存在;
isFile();判断是文件
isDirectory();判断是否为目录
length();返回文件的长度
creatNewFile();创建新文件,不会抛异常
getPath();获取文件的相对路径
getAbsolutePath();获取文件的绝对路径
delete();删除文件,不会抛异常
public class FileDemo { //c:\1\2\3\1.txt 绝对路径 //2\3\1.txt 相对路径 public void create(File file){ if(!file.exists()){ try { file.createNewFile(); System.out.println("文件已创建!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //显示文件信息 public void showFileInfo(File file){ if(file.exists()){ if(file.isFile()){ System.out.println("该文件名:"+file.getName()); System.out.println("相对路径:"+file.getPath()); System.out.println("绝对路径:"+file.getAbsolutePath()); System.out.println("文件大小:"+file.length()+"字节"); } if(file.isDirectory()){ System.out.println("file是目录!"); } }else{ System.out.println("文件或目录不存在"); } } //删除文件 public void delete(File file){ if(file.exists()){ file.delete(); System.out.println("文件已删除!"); } } public static void main(String args[]){ FileDemo filedemo=new FileDemo(); File file=new File("E:/marenfei/test.txt"); filedemo.create(file); filedemo.showFileInfo(file); filedemo.delete(file); } }
二.字节输入(输出)流
1.FileInputStream 文件字节输入流,适用于对纯文本进行读操作
2.FileOutofStream 文件字节输出流,向外写操作
public class OutPutAndInput { public static void main(String[] args) throws Exception { String oldFile="D:/mrf.txt"; String newFile="D:/marenfei.txt"; FileInputStream fis=new FileInputStream(oldFile); //输入流对象 FileOutputStream fos=new FileOutputStream(newFile); int data=0; byte bytes[]=new byte[1024]; while((data=fis.read(bytes))!=-1){ fos.write(bytes,0,data); } fos.close(); fis.close(); } }
三、字符输入(输出)流
1.FileReader 字符输入流(以字符为单位)
public class StreamReaderTest { public static void main(String[] args) throws Exception { //创建流对象 FileInputStream fis=new FileInputStream("file/marenfei.txt"); //声明字节类型的数组 byte bytes[]=new byte[1024]; //处理业务逻辑 StringBuilder sb=new StringBuilder(); int data=bytes.length;//read方法的返回值如果不为-1的情况下 证明有内容可读 while(fis.available()!=0){ //avaiables:代表着输入流中可读字节数 data=fis.read(bytes, 0, data); String str=new String(bytes); sb.append(str); } System.out.println(sb); //关闭流,释放文件资源 fis.close(); } }
2.FileWriter 字符输出流
public class StreamWriterTest { public static void main(String[] args) { FileOutputStream fos=null; //默认会把文件的内容清空并重新写入 try { fos=new FileOutputStream("D:\\marenfei.txt",true); String word="好好学习 天天向上"; fos.write(word.getBytes()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
四.缓冲流
1.用户无需手动创建数组,自带缓冲区
2.BufferReader 缓冲输入流
public class ReaderTest { public static void main(String[] args) throws Exception { BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("file/强哥.txt"))); //按照行的终止符:换行符 String readLine = br.readLine(); System.out.println(readLine); } }
3.PrintWriter 输出流相比于BufferWriter功能更加强大
public class WriterTest { public static void main(String[] args) throws Exception { PrintWriter pw=new PrintWriter(new FileOutputStream("file/强哥.txt") pw.println("我叫强哥"); pw.println("我叫程程"); pw.flush();//强制刷新缓冲区中的字符或字节 pw.close(); } }
五.二进制输入(输出)流
1.DataInputStream 二进制输入流
2.DataOutputStream 二进制输出流
public class InputStreamTest { public static void main(String[] args) throws Exception { //分别实例化输入流和输出流对象 DataInputStream dis=new DataInputStream(new FileInputStream("file/强哥.jpg")); DataOutputStream dos=new DataOutputStream(new FileOutputStream("D:\\山间的风.jpg")); byte bytes[]=new byte[1024]; int data=0; while((data=dis.read(bytes))!=-1){ dos.write(bytes, 0, data); } dos.close(); dis.close(); } }
六.序列化与反序列化
1.ObjectInputStream 反序列化
2.ObjectOutputStream 序列化
3.trasient 修饰将不会被序列化,类必须实现Serializable接口
Student.java //该类实现了Serializable接口
public class Student implements Serializable{ private static final long serialVersionUID = 1L; private transient String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(String name, int age) { super(); this.name = name; this.age = age; } public Student() { super(); // TODO Auto-generated constructor stub } @Override public String toString() { return "Student [name=" + name + ", age=" + age + "]"; } }
SerializeTest.java
public class SerializeTest { public static void main(String[] args) throws Exception { Student stu=new Student("强哥",18); ObjectOutputStream ois=new ObjectOutputStream(new FileOutputStream("file/强哥.txt")); ois.writeObject(stu); ois.close(); ObjectInputStream ois=new ObjectInputStream(new FileInputStream("file/强哥.txt")); Student stu=(Student)ois.readObject(); System.out.println(stu); } }