`
douh
  • 浏览: 16547 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

System.in.read()和System.out.write()和println()的问题。

阅读更多
如下测试代码:
package javaio;


import java.io.IOException;
import java.io.InputStream;

/**
 * Created by IntelliJ IDEA.
 * User: MJ
 * Date: 11-10-26
 * Time: 上午11:48
 * To change this template use File | Settings | File Templates.
 */
public class InputStreamTest {
    public static void main(String[]ags){
        try{
            InputStream in=System.in;
            int indata=in.read();
            System.out.write(indata);//(i)
            System.out.println();
            System.out.println(indata);//(ii)
            byte[] hello = {53};
            System.out.write(hello);(iii)
            //System.out.write((byte )indata);
            /*OutputStream outputStream=System.out;
            outputStream.write(indata);*/
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

    }
}



运行时 输入5,回车,其结果如下:
5
5
53
5
(i)输入5时,被读到内存后,indata的值为53,因为5的ASCII码是53,所以WRITE写出的时候,把53转换成对应的字符。
(ii)当使用println(indata)时,先对indata调用Strint.valueOf(int),即等到字符串53,然后调用WRITE(byte)把字符串写到控制台。所以结果就是53.
(iii)调用write(byte[])时,第一个元素的值就是53,所以在写到控制台时,转换成ASCII码为53的5.
注:
a、控制台显示的都是经过platform's default character encoding 的值,如果没有改过平台字符编码的方式的话。
b、读到内存里的是字节码。
分享到:
评论
1 楼 飞鸿247 2012-08-07  
   错喽!!!不信  自己试试看!!!

相关推荐

    服务器下载文件

    System.out.println("--下载路径--:"+path); System.out.println("----"); System.out.println("--------"); //sPath = HttpRequest.forwardIn(restTemplate,"http://common-method/","getFileUrl",obj); ...

    JAVA程序中FTP的连接以及上传文件

    System.out.println("disconnect success"); } catch (IOException ex) { System.out.println("not disconnect"); System.out.println(ex); } } public void upload() { //this....

    javazhuabaoshili.rar

    System.out.println("********************************************"); result+="********************************************"+"\n"; /* IP数据报报文头 */ byte[] l = packet.header; String str = ...

    编写简单的代理服务器(java源码)

    System.out.println("proxy server OK"); while (true) { Socket s=ss.accept(); process p=new process(s); Thread t=new Thread(p); t.start(); } } catch (Exception e) { System.out.println(e); } ...

    IO输入输出留

    System.out.println("是否可读:"+f.canRead()); System.out.println("是否可写:"+f.canWrite()); System.out.println("是否是隐藏:"+f.isHidden()); System.out.println("文件的绝对路径:"+f....

    csv文件读下用的jar包

    System.out.println("current record: " + cr.getCurrentRecord()); // 本行内容 System.out.println("RawRecord:" + cr.getRawRecord()); // 每一列的内容 System.out.println("getValues() ");...

    java-实验报告.doc

    //创建指向out的bufferWrite流 Reader in=new FileReader(fRead); //创建指向文件fRead的in流 BufferedReader bufferRead=new BufferedReader(in); //创建指向in的bufferRead流 String str=null; while ((str=...

    struts上传图片

    System.out.println(ex.getMessage()); } } } /* * 使用org.apache.strtus.upload.FormFile实现文件的单一上传 */ private void singleUploadFile(IndexActionForm objForm) { if(objForm....

    人工智能-项目实践-搜索引擎-java实验1-实现搜索引擎的倒排索引数据结构

    如果写文本文件,推荐使用PrintWriter,当创建好PrintWriter对象后,调用其println和print方法可以将字符串一行行的写入到文本文件,使用方法与System.out.println, System.out.print完全一样 具体使用方法,请见...

    ftp实现简单的文件传输

    System.out.println("文件长度:"+(int)file.length()); s=ss.accept(); System.out.println("建立socket连接"); DataInputStream dis=new DataInputStream(new BufferedInputStream(new ...

    jcifs-1.3.12.zip

    通过jcifs实现java访问网络共享文件 jcifs 是 cifs(common internet file system) java的实现 ... System.out.println(buffer.length); } in.close(); }catch(Exception e){ e.printStackTrace(); }

    Java实现文件的压缩和解压

    System.out.println("创建目录:" + outputDirectory + File.separator + name); } else { String fileName = zipEntry.getName(); fileName = fileName.replace('\\', '/'); if (fileName.indexOf(...

    Java生成密钥的实例.rar

     System.out.println("得到单钥加密密钥");  //产生双钥的密钥对(keyPair)  KeyPairGenerator keyPairGenerator=KeyPairGenerator.getInstance("RSA"); //采用RSA算法  keyPairGenerator.initialize(1024); /...

    java压缩使用org.apache.tools.zip包解决中文文件名

    //System.out.println(entityName); outf.putNextEntry(new ZipEntry(entityName)); int len; while((len=in.read(buf))>0){ outf.write(buf,0,len); } outf.closeEntry(); in.close(); } outf.close()...

    JAVA文件压缩与解压缩实践.doc

    System.out.println("Usage:java gzip "); System.exit(1); } try { //打开需压缩文件作为文件输入流 FileInputStream fin=new FileInputStream(args[0]); //建立压缩文件输出流 FileOutputStream fout=new ...

    java将png转为gif

    这个包是从jdk1.7中将jdk的源码拆下来打的包,可以解决在1.4下无法将png完美转换成gif的问题。这个转换非常完美。测试的话,main函数里面写 ...System.out.println(UtilImage.write(image, "gif", file));

    java io读取文件到String

    System.out.println("方法1用时"+ (endTime-beginTime) + "ms"); return ret; } public static String loadAFileToStringDE2(File f) throws IOException { long beginTime = System.currentTimeMillis(); ...

    java计算器程序.doc

    java 编写计算器的简单程序//一个较为简洁的巧妙的计算器程序, import java.io.*; import java.awt.event.*; import java.awt.*; //需要解决的问题,数学... catch(Exception u) {System.out.println("出错了");}} ...

    桌面管家源码(java)

    System.out.println(in.readLine()); } } catch(Exception e) { System.out.print("输入流创建失败!"); } } } //接受各类文件 public void transmission() { try { tsf=new DataOutputStream...

    下载代码

    System.out.println(); conn.setDoInput(true); conn.connect(); is = conn.getInputStream();//打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream。 bis = new BufferedInputStream(is); }...

Global site tag (gtag.js) - Google Analytics