必威体育Betway必威体育官网
当前位置:首页 > IT技术

OutputStreamWriter、PrintWriter和BufferedWriter区别

时间:2019-10-10 07:44:50来源:IT技术作者:seo实验室小编阅读:53次「手机版」
 

outputstreamwriter

一、是否有无追加模式(是否从已有文件末尾追加)

outputstreamwriter:有

printwriter:无

BufferedWriter:有

二、是否能能控制编码

outputstreamWriter:能

PrintWriter:能

BufferedWriter:不能

三、是否能控制封包大小

OutputStreamWriter:不能

PrintWriter:不能

BufferedWriter:能

四、封装的是Writer(是字符数据)还是数据流(是二进制数据)

OutputStreamWriter:数据流

PrintWriter:数据流

BufferedWriter:writer

代码示例:

        OutputStreamWriter osw=null;
        PrintWriter pw=null;
        BufferedWriter bw=null;
        try{
            osw=new OutputStreamWriter(new FileOutputStream("d:/write1.txt",false),"GBK");
            pw=new PrintWriter("d:/write1.txt","GBK");
            bw=new BufferedWriter(new FileWriter("d:/write1.txt",false));
            String str="9.    分别使用OutputStreamWriter、PrintWriter和BufferedWriter";
            String str2="类将三行字符串写入到文本文件中,";
            String str3="仔细分析有何差别";

            String str4="youedkdkdkdkdlkaldalelrladl";
            String str5="youedkdkdkdkdlkaldalelrladl";
            String str6="youedkdkdkdkdlkaldalelrladl";

            String str7="ssssssssssssssssssdtfwserewdd";
            String str8="ssssssssssssssssssdtfwserewdd";
            String str9="ssssssssssssssssssdtfwserewdd";

            pw.println(str);
            pw.println(str2);
            pw.println(str3);
            pw.flush();
            pw.close();
            osw.write(str4+"\r\n");
            osw.write(str5+"\r\n");
            osw.write(str6+"\r\n");
            osw.close();

            bw.write(str7);
            bw.newLine();
            bw.write(str8);
            bw.newLine();
            bw.write(str9);
            bw.flush();
            bw.close();


        }
        catch(UnsupportedEncodingException e){
            e.printstacktrace();
        }
        catch(IOException e){
            e.printStackTrace();
        }

java POST请求远程HTTP接口

package com.oemp.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.inputstreamreader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Post Method
 */
public class HttpPostUrl {

    /**
     * 向指定URL发送POST请求
     * @param url
     * @param paramMap
     * @return 响应结果
     */
    public static String sendPost(String url, Map<String, String> paramMap) {
        PrintWriter out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setrequestproperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent","Mozilla/4.0 (compatible; MSIE 6.0; windows NT 5.1;SV1)");
            // conn.setRequestProperty("Charset", "UTF-8");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());

            // 设置请求属性
            String param = "";
            if (paramMap != null && paramMap.size() > 0) {
                Iterator<String> ite = paramMap.keySet().iterator();
                while (ite.hasNext()) {
                    String key = ite.next();// key
                    String value = paramMap.get(key);
                    param += key + "=" + value + "&";
                }
                param = param.substring(0, param.length() - 1);
            }

            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(
                    new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.err.println("发送 POST 请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输出流、输入流
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

    /**
     * 数据流post请求
     * @param urlStr
     * @param xmlInfo
     */
    public static String doPost(String urlStr, String strInfo) {
        String reStr="";
        try {
            URL url = new URL(urlStr);
            URLConnection con = url.openConnection();
            con.setDoOutput(true);
            con.setRequestProperty("pragma:", "no-cache");
            con.setRequestProperty("Cache-Control", "no-cache");
            con.setRequestProperty("content-Type", "text/xml");
            OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
            out.write(new String(strInfo.getBytes("utf-8")));
            out.flush();
            out.close();
            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"));
            String line = "";
            for (line = br.readLine(); line != null; line = br.readLine()) {
                reStr += line;
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return reStr;
    }


    /**
     * 测试主方法
     * @param args
     */
    public static void main(String[] args) {
        Map<String, String> mAPParam = new HashMap<String, String>();
        mapParam.put("name", "张三");
        mapParam.put("validation","test");
        String pathUrl = "http://localhost/testPost.action";
        String result = sendPost(pathUrl, mapParam);
        System.out.println(result);

    }

}

相关阅读

text/html和text/plain的区别

text/html和text/plain的区别1、text/html的意思是将文件的content-type设置为text/html的形式,浏览器在获取到这种文件时会自动调

setBackground(),setBackgroundResource(),setBackgro

setBackground(),setBackgroundResource(),setBackgroundColor()和setBackgroundDrawable()这几个方法都可以对控件的颜色进行设

人机交互 (HCI) 和交互设计 (Interaction Design) 的

作为一个PM当然非常需要注重产品在使用过程中的一些交互体验,可是广义的人机交互和交互设计又有什么区别呢?让我们来看看知乎里别人

C#里面Console.Write()和Console.WriteLine()有什么区别

  Console.Write()和Console.WriteLine()都是System.Console提供的方法,两着主要用来将输出流由指定的输出装置(默认为屏幕)显

IDS与IPS的区别是什么?

IDS (入侵检测系统)IDS是英文“Intrusion Detection Systems”的缩写,中文意思是“入侵检测系统”。专业上讲就是依照一定的安全策略

分享到:

栏目导航

推荐阅读

热门阅读