java IO 入门例子
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.File; 2 3 /** 4 * File类型基本操作 5 */ 6 7 /** 8 * @author Administrator 9 * 10 */ 11 public class FileDemo { 12 13 /** 14 * @param args 15 */ 16 public static void main(String[] args) { 17 // TODO Auto-generated method stub 18 final String PATH = "c:\\text.txt"; 19 File file = new File(PATH); 20 if (file.exists()) { 21 if (file.isFile()) { 22 System.out.println("名称:" + file.getName()); 23 System.out.println("相对路径:" + file.getPath()); 24 System.out.println("绝对路径:" + file.getAbsolutePath()); 25 System.out.println("文件大小:" + file.length() + "字节"); 26 } else if (file.isDirectory()) { 27 System.out.println("这是一个目录!"); 28 } 29 } else { 30 System.out.println("文件不存在!"); 31 } 32 } 33 34 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.FileInputStream; 2 import java.io.FileOutputStream; 3 4 /** 5 * 使用字节流写入文件、读取文件。 6 */ 7 /** 8 * @author Administrator 9 * 10 */ 11 public class ByteStreamDemo { 12 13 /** 14 * @param args 15 * @throws Exception 16 */ 17 public static void main(String[] args) throws Exception { 18 // TODO Auto-generated method stub 19 final String PATH = "c:\\text.txt"; 20 21 FileOutputStream fos = null; 22 try { 23 String content = "王云鹏是帅哥!"; 24 byte[] contents = content.getBytes(); 25 fos = new FileOutputStream(PATH); 26 fos.write(contents, 0, contents.length); 27 fos.flush(); 28 } catch (Exception e) { 29 // TODO: handle exception 30 throw e; 31 } finally { 32 if (fos != null) { 33 fos.close(); 34 } 35 } 36 37 FileInputStream fis = null; 38 try { 39 fis = new FileInputStream(PATH); 40 int length = fis.available(); 41 byte[] contents = new byte[length]; 42 while(fis.read(contents, 0, length)!=-1){ 43 String content = new String(contents); 44 System.out.println(content); 45 } 46 } catch (Exception e) { 47 // TODO: handle exception 48 throw e; 49 } 50 finally{ 51 if(fis!=null){ 52 fis.close(); 53 } 54 } 55 } 56 57 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.DataInputStream; 2 import java.io.DataOutputStream; 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 6 /** 7 * 采用二进制流方式写入文件、读取文件。 8 */ 9 10 /** 11 * @author Administrator 12 * 13 */ 14 public class BinaryStreamDemo2 { 15 16 /** 17 * @param args 18 * @throws Exception 19 */ 20 public static void main(String[] args) throws Exception { 21 // TODO Auto-generated method stub 22 final String PATH = "c:\\text.txt"; 23 24 java.io.FileOutputStream fos = null; 25 java.io.DataOutputStream dos = null; 26 try { 27 fos = new FileOutputStream(PATH); 28 dos = new DataOutputStream(fos); 29 30 dos.writeInt(1); 31 dos.writeLong(2); 32 dos.writeUTF("王云鹏是帅哥!"); 33 dos.flush(); 34 35 } catch (Exception e) { 36 // TODO: handle exception 37 throw e; 38 } finally { 39 if (dos != null) { 40 dos.close(); 41 } 42 if (fos != null) { 43 fos.close(); 44 } 45 } 46 47 java.io.FileInputStream fis = null; 48 java.io.DataInputStream dis = null; 49 try { 50 fis = new FileInputStream(PATH); 51 dis = new DataInputStream(fis); 52 53 int i = dis.readInt(); 54 long l = dis.readLong(); 55 String content = dis.readUTF(); 56 System.out.println(i); 57 System.out.println(l); 58 System.out.println(content); 59 } catch (Exception e) { 60 // TODO: handle exception 61 throw e; 62 } finally { 63 if (dis != null) { 64 dis.close(); 65 } 66 if (fis != null) { 67 fis.close(); 68 } 69 } 70 } 71 72 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.DataInputStream; 2 import java.io.DataOutputStream; 3 import java.io.FileInputStream; 4 import java.io.FileOutputStream; 5 6 /** 7 * 采用二进制流方式写入文件、读取文件。 8 */ 9 10 /** 11 * @author Administrator 12 * 13 */ 14 public class BinaryStreamDemo { 15 16 /** 17 * @param args 18 * @throws Exception 19 */ 20 public static void main(String[] args) throws Exception { 21 // TODO Auto-generated method stub 22 java.io.FileOutputStream fos = null; 23 java.io.DataOutputStream dos = null; 24 java.io.FileInputStream fis = null; 25 java.io.DataInputStream dis = null; 26 try { 27 fis = new FileInputStream("c:\\source.jpg"); 28 dis = new DataInputStream(fis); 29 30 fos = new FileOutputStream("c:\\desc.jpg"); 31 dos = new DataOutputStream(fos); 32 33 int temp = -1; 34 while((temp=dis.read())!=-1){ 35 dos.write(temp); 36 } 37 } catch (Exception e) { 38 // TODO: handle exception 39 throw e; 40 } 41 finally 42 { 43 if(dos!=null) 44 { 45 dos.close(); 46 } 47 if(fos!=null){ 48 fos.close(); 49 } 50 if(dis!=null){ 51 dis.close(); 52 } 53 if(fis!=null){ 54 fis.close(); 55 } 56 } 57 System.out.println("copy file success!"); 58 } 59 60 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.FileReader; 2 import java.io.FileWriter; 3 import java.io.IOException; 4 import java.util.Arrays; 5 6 /** 7 * 使用字符流写入文件、读取文件。 8 */ 9 10 /** 11 * @author Administrator 12 * 13 */ 14 public class CharStreamDemo { 15 16 /** 17 * @param args 18 * @throws Exception 19 */ 20 public static void main(String[] args) throws Exception { 21 // TODO Auto-generated method stub 22 final String PATH = "c:\\text.txt"; 23 24 FileWriter fw = null; 25 try{ 26 fw = new FileWriter(PATH); 27 fw.write("王云鹏是帅哥!"); 28 fw.flush(); 29 }catch(IOException ex){ 30 throw ex; 31 } 32 finally{ 33 if(fw!=null){ 34 fw.close(); 35 } 36 } 37 38 FileReader fr = null; 39 try { 40 fr = new FileReader(PATH); 41 char[] chars = new char[1024]; 42 StringBuilder sb = new StringBuilder(); 43 int length = 0; 44 while((length=fr.read(chars))!=-1){ 45 if(length==1024){ 46 sb.append(chars); 47 }else 48 { 49 char[] less = Arrays.copyOf(chars, length); 50 sb.append(less); 51 } 52 Arrays.fill(chars, ' ');//清空数组 53 } 54 System.out.println(sb.toString()); 55 56 } catch (Exception e) { 57 // TODO: handle exception 58 throw e; 59 } 60 finally{ 61 if(fr!=null){ 62 fr.close(); 63 } 64 } 65 } 66 67 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 import java.io.BufferedReader; 2 import java.io.BufferedWriter; 3 import java.io.FileReader; 4 import java.io.FileWriter; 5 6 /** 7 * 使用字符流+缓冲器写入文件、读取文件。 8 */ 9 10 /** 11 * @author Administrator 12 * 13 */ 14 public class CharBufferStreamDemo { 15 16 /** 17 * @param args 18 * @throws Exception 19 */ 20 public static void main(String[] args) throws Exception { 21 // TODO Auto-generated method stub 22 final String PATH = "c:\\text.txt"; 23 24 FileWriter fw = null; 25 BufferedWriter bw = null; 26 try { 27 fw = new FileWriter(PATH); 28 bw = new BufferedWriter(fw); 29 bw.write("王云鹏是帅哥!"); 30 bw.newLine(); 31 bw.write("王云鹏是北大青鸟趟城中心的帅哥!"); 32 bw.flush(); 33 } catch (Exception e) { 34 // TODO: handle exception 35 throw e; 36 } finally { 37 if (bw != null) { 38 bw.close(); 39 } 40 if (fw != null) { 41 fw.close(); 42 } 43 } 44 45 FileReader fr = null; 46 BufferedReader br = null; 47 try { 48 fr = new FileReader(PATH); 49 br = new BufferedReader(fr); 50 String line = null; 51 while ((line = br.readLine()) != null) { 52 System.out.println(line); 53 } 54 } catch (Exception e) { 55 // TODO: handle exception 56 throw e; 57 } finally { 58 if (br != null) { 59 br.close(); 60 } 61 if (fr != null) { 62 fr.close(); 63 } 64 } 65 } 66 67 }