Java IO操作
目錄
- File
- InputStream
- OutputStream
- Writer
- Reader
- 流轉換
- BufferedReader & BufferedWriter
- BufferedInputStream & BufferedOutputStream
- PrintStream
- ObjectStream
- ByteArrayStream
- DataStream
- StringStream
File類的使用
public static void main(String[] args) { //File.separator 表示分隔符 File file1 = new File("D:"+File.separator+"yyd"+File.separator+"cdut.txt"); //路徑分隔符 // String s = File.pathSeparator; //文件是否存在 if(!file1.exists()){ try { //創建一個新文件 boolean b = file1.createNewFile(); System.out.println("創建文件:"+b); } catch (IOException e) { e.printStackTrace(); } } //刪除文件 //System.out.println(file1.delete()); //得到文件的上一級路徑 System.out.println(file1.getParent()); //判斷一個路徑是否是文件夾 System.out.println("是否是目錄:"+file1.isDirectory()); ////判斷一個路徑是否是文件 System.out.println("是否是文件:"+file1.isFile()); File file2 = new File("d:\\yydt"); //列出文件夾中的所有文件名 String[] fileName = file2.list(); for (String s : fileName) { System.out.println(s); } //列出文件夾中的所有文件,以File數組返回 File[] files = file2.listFiles(); for (File file : files) { System.out.println(file.getPath()+"---"+file.length()); } //創建文件夾 File file3 = new File("d:\\zhangsan\\lisi"); file3.mkdirs(); //重命名 File file4 = new File("d:\\zhangsan\\wangwu"); file3.renameTo(file4); }
IO
InputStream
/** * 字節輸入流的讀取方式三:每次讀取指定大小的字節 */ public static void read3(){ try { File f = new File("d:\\1.txt"); //構造一個字節輸入流對象 InputStream in = new FileInputStream(f); //指定每次要讀取的字節數組 byte[] bytes = new byte[10]; int len = -1;//每次讀取的實際長度 StringBuilder sb = new StringBuilder(); while((len = in.read(bytes))!=-1){ sb.append(new String(bytes,0,len)); } //關閉 in.close(); //輸出 System.out.println(sb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 字節輸入流的讀取方式二:一次性讀取所有字節 */ public static void read2(){ try { File f = new File("d:\\1.txt"); //構造一個字節輸入流對象 InputStream in = new FileInputStream(f); //根據文件的大小構造字節數組 byte[] bytes = new byte[(int)f.length()]; int len = in.read(bytes); System.out.println(new String(bytes)); System.out.println("len="+len); //關閉 in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 字節輸入流的讀取方式一:每次讀取一個字節 */ public static void read1(){ try { //構造一個字節輸入流對象 InputStream in = new FileInputStream("d:\\1.txt"); int b = -1;//定義一個字節,-1表示沒有數據 while((b=in.read())!=-1){ System.out.print((char)b); } //關閉 in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // read1(); // read2(); read3(); }
OutputStream
/** * 字節輸出流的方式二:每次輸出指定大小的字節 */ public static void write2() { try { // 創建一個文件字節輸出流對象(參數true表示追加輸出) OutputStream out = new FileOutputStream("d:\\1.txt", true); // String info = "hello,xiaobai"; String info = "one car come,one car go,two car pengpeng,one car die!"; byte[] bytes = info.getBytes(); out.write(bytes);// 輸出一個字節數組 // out.write(bytes,0,5);//輸出一個字節數組中的指定範圍的字節 // 關閉流 out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 字節輸出流的方式一:每次輸出一個字節 */ public static void write1() { try { // 創建一個文件字節輸出流對象 OutputStream out = new FileOutputStream("d:\\1.txt"); String info = "hello,IO"; byte[] bytes = info.getBytes(); for (int i = 0; i < bytes.length; i++) { // 向文件中輸出 out.write(bytes[i]); } // 關閉流 out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // write1(); write2(); System.out.println("success"); }
Writer
/** * 字符輸出流方式一:以字符數組方式輸出 */ public static void writer1(){ File f = new File("d:\\2.txt"); try { //構造一個字符輸出流對象(true表示追加輸出) Writer out = new FileWriter(f,true); String info = "good good study,day day up!"; //向文件中輸出 // out.write(info.toCharArray()); out.write(info); //關閉流 out.close(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { writer1(); }
Reader
/** * 字符輸入流方式一:使用指定大小的字符數組輸入 */ public static void reader1(){ File f = new File("d:\\1.txt"); try { //構造一個字符輸入流對象 Reader in = new FileReader(f); char[] cs = new char[20]; int len = -1; StringBuffer sb = new StringBuffer(); while((len = in.read(cs))!=-1){ sb.append(new String(cs,0,len)); } //關閉流 in.close(); System.out.println(sb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 使用字節流讀取文本文件 */ public static void byteReader(){ File f = new File("d:\\1.txt"); try { InputStream in = new FileInputStream(f); byte[] bytes = new byte[20]; int len = -1; StringBuffer sb = new StringBuffer(); while((len = in.read(bytes))!=-1){ sb.append(new String(bytes,0,len)); } in.close(); System.out.println(sb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // byteReader(); reader1(); }
流轉換
/** * 使用轉換流,把字節流轉換成字符流 */ public static void reader(){ try { //構造字節輸入流 InputStream in = new FileInputStream("d:\\1.txt"); //通過字節輸入流構造一個字符輸入流 Reader reader = new InputStreamReader(in); char[] cs = new char[50]; int len = -1; StringBuilder sb = new StringBuilder(); while((len=reader.read(cs))!=-1){ sb.append(new String(cs,0,len)); } //關閉流 reader.close(); in.close(); System.out.println(sb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 使用轉換流,把字符流轉換成字節流輸出 * OutputStreamWriter */ public static void writer(){ try { //構造一個字節輸出流 OutputStream out = new FileOutputStream("d:\\3.txt"); String info = "山不在高,有仙則名;學JAVA,沒威哥不行"; //通過字節輸出流構造一個字符輸出流 Writer writer = new OutputStreamWriter(out); writer.write(info);//輸出 //關閉流 writer.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // writer(); reader(); }
BufferedReader & BufferedWriter
/** * 使用緩衝流實現讀取操作 */ public static void reader(){ try { Reader r = new FileReader("d:\\5.txt"); //根據字符輸入流構造一個字符緩中流 BufferedReader br = new BufferedReader(r); char[] cs = new char[512]; int len = -1; StringBuilder sb = new StringBuilder(); while((len=br.read(cs))!=-1){ sb.append(new String(cs,0,len)); } br.close(); r.close(); System.out.println(sb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 使用緩衝流實現寫入操作 */ public static void write(){ try { Writer w = new FileWriter("d:\\5.txt"); //根據字符輸出流構造一個字符緩衝流 BufferedWriter bw = new BufferedWriter(w); bw.write("小白,怎麼了,這是,被驢踢了吧"); bw.flush();//刷新 bw.close(); w.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // write(); reader(); }
BufferedInputStream & BufferedOutputStream
public static void write(){ try { OutputStream out = new FileOutputStream("d:\\4.txt"); //根據字節輸出流構造一個字節緩衝流 BufferedOutputStream bos = new BufferedOutputStream(out); String info = "good good study , day day up"; bos.write(info.getBytes()); bos.flush();//刷新緩衝區 bos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 使用字節緩衝流進行讀取操作 */ public static void input(){ try { InputStream in = new FileInputStream("d:\\4.txt"); //根據字節輸入流構造一個字節緩衝流 BufferedInputStream bis = new BufferedInputStream(in); Reader r = new InputStreamReader(bis); char[] cs = new char[512]; int len = -1; StringBuilder sb = new StringBuilder(); while((len=r.read(cs))!=-1){ sb.append(new String(cs,0,len)); } r.close(); bis.close(); in.close(); System.out.println(sb); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // input(); write(); }
PrintStream
/** * 使用PrintWriter打印流 */ public static void print2(){ try { BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\2.txt")); PrintWriter pw = new PrintWriter(bw); pw.print("\r\n");//輸出回車加換行符 pw.println(105); pw.println("張三李四王五"); pw.flush(); pw.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 使用PrintStream打印流 */ public static void print(){ try { OutputStream out = new FileOutputStream("d:\\1.txt"); BufferedOutputStream bos = new BufferedOutputStream(out); //構造字節打印流對象 PrintStream ps = new PrintStream(bos);// ps.println(3.14f); ps.println(188); ps.println(true); ps.println("好好學習,天天向上"); //關閉流 ps.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { // print(); print2(); }
ObjectStream
/** * 類通過實現 java.io.Serializable 接口以啓用其序列化功能,標記接口,沒有任何方法 */ public class Dog implements Serializable{ private String name; private transient int age; //使用transient關鍵字聲明的屬性將不會被序列化 public Dog() { super(); } public Dog(String name, int age) { super(); this.name = name; this.age = 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; } @Override public String toString() { return "Dog [name=" + name + ", age=" + age + "]"; } }
/** * 從文件中讀取對象數組 */ public static void readerObject2(){ try { InputStream in = new FileInputStream("d:\\obj.tmp"); //根據字節輸入流構造一個對象流 ObjectInputStream ois = new ObjectInputStream(in); //讀取一個對象 Dog[] dogs = (Dog[])ois.readObject(); //關閉 ois.close(); in.close(); for (Dog dog : dogs) { System.out.println(dog); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 把一組對象序列化到文件中 */ public static void writerObject2(){ try { OutputStream out = new FileOutputStream("d:\\obj.tmp"); //根據字節輸出流構造一個對象流 ObjectOutputStream oos = new ObjectOutputStream(out); Dog[] dogs = {new Dog("小白",8),new Dog("小黑",2),new Dog("小紅",4)}; // Dog dog1 = new Dog("小白",8); // Dog dog2 = new Dog("小黑",2); // Dog dog3 = new Dog("小紅",4); oos.writeObject(dogs);//向文件寫入對象 //關閉流 oos.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 使用ObjectInputStream讀取文件中的對象 */ public static void readerObject(){ try { InputStream in = new FileInputStream("d:\\obj.tmp"); //根據字節輸入流構造一個對象流 ObjectInputStream ois = new ObjectInputStream(in); //讀取一個整數 int num = ois.readInt(); //讀取一個對象 Dog dog = (Dog)ois.readObject(); //關閉 ois.close(); in.close(); System.out.println("num="+num); System.out.println(dog); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } /** * 使用ObjectOutputStream把對象寫入文件中 */ public static void writerObject(){ try { OutputStream out = new FileOutputStream("d:\\obj.tmp"); //根據字節輸出流構造一個對象流 ObjectOutputStream oos = new ObjectOutputStream(out); //輸出數據 oos.writeInt(106); Dog dog = new Dog("小白",8); oos.writeObject(dog);//向文件寫入對象 //關閉流 oos.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // writerObject(); // readerObject(); // writerObject2(); readerObject2(); }
ByteArrayStream
/** * 使用ByteArrayOutputStream寫操作 */ public static void write(){ //創建一個字節數組輸出流對象 ByteArrayOutputStream baos = new ByteArrayOutputStream(); String info = "好好學習,天天向上!"; try { //往緩衝區中輸出數據 baos.write(info.getBytes()); baos.write(10); // baos.toByteArray(); baos.close();//關閉無效 } catch (IOException e) { e.printStackTrace(); } //根據一個字節數組構造一個字節數組輸入流 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); byte[] bytes = new byte[512]; int len = -1; StringBuilder sb = new StringBuilder(); try { while((len=bais.read(bytes))!=-1){ sb.append(new String(bytes,0,len)); } } catch (IOException e) { } System.out.println(sb); } public static void main(String[] args) { write(); }
DataStream
public static void reader(){ try { InputStream in = new FileInputStream("d:\\3.txt"); //根據字節輸入流構造一個數據輸入流 DataInputStream dis = new DataInputStream(in); int flag = dis.readInt();//讀取一個整數 String info = dis.readUTF();//讀取一個UTF編碼的字符串 //關閉流 dis.close(); in.close(); System.out.println("flag="+flag); System.out.println("info="+info); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void write(){ try { OutputStream out = new FileOutputStream("d:\\3.txt"); //根據字節輸出流構造一個數據輸出流 DataOutputStream dos = new DataOutputStream(out); dos.writeInt(1);//輸出一個整數 dos.writeUTF("好好學習天天向上..."); dos.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { // write(); reader(); }
StringStream
public static void writer(){ //寫入操作 StringWriter sw = new StringWriter(); sw.write("好好學習。。天天向上 。。。"); //----------------------------------------- //讀取操作,根據一個字符串去構造一個字符串輸入流 StringReader sr = new StringReader(sw.toString()); char[] cs = new char[10]; int len = -1; try { while((len=sr.read(cs))!=-1){ System.out.print(new String(cs,0,len)); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { writer(); }