java IO 之 字节流和字符流

1.java字节流操作文本

public class FileCopyByte {

     /**  
     * @Title: main 
     * @Description: TODO
     * @param :@param args     
     * @return :void    
     * @throws 
     */
     public static void main(String[] args) {
        File f=new File("D:/test.txt");
        InputStream is=null;
        OutputStream out=null;
        String url="d:/test1.txt";
        File outfile=new File(url);
        if(!outfile.exists()){
            
             try {
                    outfile.createNewFile();
                    System.out.println("文件创建成功!");
               } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
            
        }
        try {
               out=new FileOutputStream(outfile);
          } catch (FileNotFoundException e1) {
               e1.printStackTrace();
          }
          try {
               is=new FileInputStream(f);
          } catch (FileNotFoundException e) {
               e.printStackTrace();
          }
          byte[] b=new byte[1024];
          int i=0;
          int c;
          try {
               while((c=is.read())!=-1){
                   b[i]=(byte)c;
                   
                   out.write(b[i]);
                   if(i==b.length-1){
                      
                       b=new byte[b.length];
                       i=-1;
                   }
                   i++;
                  
               }
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          try {
               is.close();
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
         
          System.out.println("文本导入成功!");
     }
    
}

2. java字符流操作文本

public class FileImportChar {

     public static void main(String[] args) {
        File f=new File("D:/test.txt");
        Reader is=null;
        Writer out=null;
        String url="d:/test1.txt";
        File outfile=new File(url);
        if(!outfile.exists()){
            
             try {
                    outfile.createNewFile();
                    System.out.println("文件创建成功!");
               } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
               }
            
        }
      
               try {
                    out=new FileWriter(outfile);
                    is=new FileReader(f);
               } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
               }
              
          char[] b=new char[1024];
          int i=0;
          int c=0;
          try {
               while((c=is.read())!=-1){
                   b[i]=(char)c;
                   System.out.print(b[i]+"//");
                   out.write(b[i]);
                   i++;
               }
               is.close();
               out.close();
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
         
          System.out.println("文本导入成功!");
     }
    
}

3.java字符串流操作文本

 
关于文件路径问题: 
1. 若使用"D:/testall.txt" 形式 是可以正常读取到文件的
2. 若使用"D:\\testall.txt" 形式 一定要注意 "D:\testall.txt"是需要 再加一个'\'进行转义

        File file = new File("d:/test.txt");
        FileInputStream fileReader= new FileInputStream(file);
        BufferedReader br = new BufferedReader( new InputStreamReader(fileReader, "GBK" ));
        StringBuffer sb= new StringBuffer();
        while(br.ready()){
           
            sb.append(br.readLine());
           
        }
        String xml = sb.toString();
 
 
posted @ 2014-06-04 14:56  抢救菜鸟  阅读(177)  评论(0编辑  收藏  举报