IO操作
输入输出:站在程序角度,输入(Input)-读, 输出(Ouput)-写
使用FIle类操作文件或者目录
File类的常见方法
方法名
说明
boolean exists()
判断文件或者目录是否存在
boolean isFile()
判断是否是文件
boolean isDirectory()
判断是否是目录
String getPath()
返回此对象表示的文件的相对路径
String getAbsolutePath()
返回此对象表示的文件的绝对路径名
String getName()
返回此对象表示的文件或者目录的名称
boolean delete()
删除此对象指定的文件或目录
boolean createNewFile()
创建名称的空文件,不创建文件夹
long leng()
返回文件的长度,的那位为字节,如果文件不存在返回OL
使用字节流读写文本文件 字节输入流(读文件): InputStream-FileInputStream-read()
FileInputSream对象的read()方法有两种方法:read()=======read(byte[])
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 public class test { public static void main (String[] args) { File file = new File("d:/mary/demo.txt" ); FileInputStream fileInputStream = null ; try { fileInputStream = new FileInputStream(file); byte [] bytes = new byte [1024 ]; int d; while ((d = fileInputStream.read(bytes)) != -1 ){ for (int i = 0 ; i<d; i++){ System.out.print((char )bytes[i]); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
字节输出流(写文件) OuputSteam-FileOutStream-write()
FileOutputStream这个类有两个构造,个是只添加路径或者file对象,另一个是还可以追加一个true,意思就是在原有的内容上添加新内容。
FileOutputStream写入方法 write(byte[])=====write(byte[] ,int off int len);
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public class TestFileOutputStream { public static void main (String[] args) { File file = new File("d:/mary/demo.txt" ); FileOutputStream fileOutputStream = null ; try { fileOutputStream = new FileOutputStream(file,true ); String str = "好好学习,天天向上呀" ; byte [] strs = str.getBytes(); fileOutputStream.write(strs,0 ,strs.length); System.out.println("ok啦" ); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
使用字符流读写文本文件 Reder类是是字符输入流的基类-抽象类
字符输入流 Rader-InputStreamReader-FileReader-BufferedReader
FileReader FileReader是Reader类的孙子
FileReader(File file) ==== FileReader(String name)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public class TestFileReader { public static void main (String[] args) { FileReader fileReader = null ; try { fileReader = new FileReader("D:/mary/demo.txt" ); StringBuffer stringBuffer = new StringBuffer(); char [] bytes = new char [1024 ]; int i ; while ((i = fileReader.read(bytes)) != -1 ){ stringBuffer.append(bytes); } System.out.println(stringBuffer); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }
InputStream是reader的子类
它在创建实例时,要传进一个FileInputStream类,还可以指定编码格式
InputStreamReader(InputStream in)======InputStreamReader(InputStream in , String charsetName)
此类和FileReader一样,就是在创建时候穿的参数不同
获取本地平台的字符编码
1 System.out.println(System.getProperty("file.encoding" ));
BufferedReder BufferedReader是Reader的子类,创建实例时,传进Reader类,它是带有缓冲区的字符输入流,按行读取文件,提高了文件读取效率。
构造方法:BufferedReader(Reader in)
方法:readLine()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class TestBufferedReader { public static void main (String[] args) { FileInputStream fileInputStream = null ; InputStreamReader inputStreamReader = null ; BufferedReader bufferedReader = null ; try { fileInputStream = new FileInputStream("d:/mary/demo.txt" ); inputStreamReader = new InputStreamReader(fileInputStream,"utf-8" ); bufferedReader = new BufferedReader(inputStreamReader); String s = null ; while ((s = bufferedReader.readLine()) != null ){ System.out.println(s); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { bufferedReader.close(); inputStreamReader.close(); fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
关闭流时,后开先关
字符输出流 Writer类是字符输出流的基类-抽象类
Writer-OutpusStreamWriter-FileWriter-BufferedWriter
FileWriter 构造方法:FileWriter(File file)====FileWriter(String name) ,加Boolean的true是在原有文件追加文件
常用方法: write(String str)====write(String str, int off, int len)====void close()====void flush()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class TestFileWrite { public static void main (String[] args) { FileWriter fileWriter = null ; try { fileWriter= new FileWriter("d:/mary/demo.txt" ); String str = "Holle,world!!!" ; fileWriter.write(str); fileWriter.flush(); } catch (IOException e) { e.printStackTrace(); }finally { try { fileWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } }
OutpusStreamWriter 构造: OutputStreamWriter(OutputStream out)==== OutputStreamWriter(OutputStream out ,String charsetName)
创建这个实例时候可以指定字符编码格式,代码和FlieWrirer一样。
BufferedWriter BufferedWriter是Writer类的子类,它带有缓冲区。
构造: BufferedWriter(Writer out) 方法:write()====newLine()===fllush()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 public class TestBufferedWriter { public static void main (String[] args) { FileOutputStream fileOutputStream = null ; OutputStreamWriter outputStreamWriter = null ; BufferedWriter bufferedWriter = null ; try { fileOutputStream = new FileOutputStream("d:/mary/demo.txt" ); outputStreamWriter = new OutputStreamWriter(fileOutputStream,"utf-8" ); bufferedWriter = new BufferedWriter(outputStreamWriter); bufferedWriter.write("哈哈哈哈" ); bufferedWriter.newLine(); bufferedWriter.write("我是第二行" ); bufferedWriter.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { bufferedWriter.close(); outputStreamWriter.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
使用字节流读写二进制文件 DataOutputStream、DataInputStream
DataOutputStream传参是FileOutputStream,DataInputStream传参是FileInputStream
其方法跟这两个是一样的。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 public class TestDataInputAndOutput { public static void main (String[] args) { FileInputStream fileInputStream = null ; DataInputStream dataInputStream = null ; FileOutputStream fileOutputStream = null ; DataOutputStream dataOutputStream = null ; try { fileInputStream = new FileInputStream("d:/mary/myimg.jpg" ); dataInputStream = new DataInputStream(fileInputStream); fileOutputStream = new FileOutputStream("d:/mary/myqi.jpg" ); dataOutputStream = new DataOutputStream(fileOutputStream); int tepe; while ((tepe = dataInputStream.read()) != -1 ){ dataOutputStream.write(tepe); } System.out.println("复制完毕" ); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { dataInputStream.close(); fileInputStream.close(); dataOutputStream.close(); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } }
使用序列化和反序列化保持和恢复对象信息 ObjectOutputStream-序列化
ObjectInputStream-反序列化
在序列化对象时候,必须要把类实现Serializable接口。否则会抛出NotSerializableException异常
在反序列化对象时候,要把反序列化出来的对象强转成你自己定义的类。
在序列化和反序列化时候如果不希望某个成员变量被序列化时,可以加入transient这个关键字可以不被反序列化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 public class StudentTest { public static void main (String[] args) { Student stu = new Student(1 ,"Mary" ,"地球村" ); StudentTest.deserialization("d:/Student.txt" ); } public static void serialize (String Path , Student stu) { File file = new File(Path); FileOutputStream fos = null ; ObjectOutputStream oos = null ; try { fos = new FileOutputStream(file); oos = new ObjectOutputStream(fos); oos.writeObject(stu); oos.flush(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { try { oos.close(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void deserialization (String Path) { File file = new File(Path); FileInputStream fis = null ; ObjectInputStream ois = null ; try { fis = new FileInputStream(file); ois = new ObjectInputStream(fis); Student stu = (Student) ois.readObject(); System.out.println(stu); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); }finally { try { ois.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }