MrHoangIt

Java04 Day 7 InputOutputStream.java

Mar 26th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.23 KB | None | 0 0
  1. package InputOutputStream;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8.  
  9. public class InputOutputStream {
  10.    
  11.     public String readInfo(String path){
  12.         String str = "";
  13.         FileInputStream in = null;
  14.         try{
  15.             File f = new File(path);
  16.             in = new FileInputStream(f);
  17.             int i=0;
  18.             while(( i=in.read())>0){
  19.                 char ch = (char) i;
  20.                 str += ch;
  21.             }
  22.             in.close();
  23.             return str;
  24.         }catch(Exception ex){
  25.             Logger.getLogger(InputOutputStream.class.getName()).log(Level.SEVERE, null, ex);
  26.         }
  27.         return "";
  28.     }
  29.    
  30.     public void write(String content, String path){
  31.         FileOutputStream out = null;
  32.         try{
  33.             File f = new File(path);
  34.             out = new FileOutputStream(f);
  35.             char[] chs = content.toCharArray();
  36.             for(char ch:chs){
  37.                 out.write((int)ch);
  38.             }
  39.             out.close();
  40.         }catch(Exception ex){
  41.             System.out.println("Loi khong the ghi file");
  42.         }
  43.     }
  44.    
  45.     public static void main(String arg[]) {
  46.         InputOutputStream obj = new InputOutputStream();
  47.         String str = obj.readInfo("C:/test.txt");
  48.         obj.write(str, "D:/copy.txt");
  49.         System.out.println("Da copy thanh cong noi dung sau: ");
  50.         System.out.println(str);
  51.     }
  52. }
Add Comment
Please, Sign In to add comment