码迷,mamicode.com
首页 > 其他好文 > 详细

基本数据类型-位运算-字符集-流

时间:2018-09-05 21:53:42      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:tin   tom   oop   指定   center   fileinput   -128   iso   字符流   

基本数据类型-位运算-字符集-流

1、基本类型

类型 字节数 范围
byte 1 -128 ~ 127
short 2 -32768 ~32767
int 4 \(- 2^{31}\) ~ $ 2^{31} - 1$
long 8
float 4
double 8
boolean 1
char 2

注意:short、char、byte参与运算时直接抬升到int型。

思考题:

  • 0是整数还是负数?
  • 字节数-128在内存中的存储形态是如何的?
  • 字节范围为何是从-128到127之间,而不是-127到正的128?

2、位运算

运算符 说明
&
|
^ 异或
~ 取反
<< 左移
>> 右移
>>> 无符号右移动

3、补码

正数的取反 + 1。

4、&& 和 & 区别

&&存在短路,&无短路操作,&通过位运算完成。

5、字符集

字符集 表示字节数
asc 1个字节的7位
gb2312 2字节
gbk 2字节
big5 5个字节
iso8859-1 1个字节8位
utf-8 中文3个字节
unicode 2个字节,含有两个字节的头

任何字符集都是asc的超集。

6、练习

  1. 找出自己的名字对应的unicode
  2. 定义函数,取出整数内存中的存储形态对应的16进制字符串
  3. 定义函数,取出整数内存中的存储形态对应的2进制字符串
  4. java中unicode字符的全量输出

7、UML

Unified Modeling Language,同一建模语言,使用图形化元素描述事物以及之间的关系。

8、Rose

rose是UML建模软件中的一种,IBM公司开发,业界使用比较普遍。Rose的安装步骤如下:

  1. 管理员运行setup.exe文件

  2. 指定安装目录,不要使用中文和空格

  3. 装后导入license文件

  4. 解决启动rose出现缺少dll文件问题

    4.1)进入rose安装目录的Common目录下

    4.2)复制suite objects.dll + license.dll文件到C:\Windows\System32\和C:\Windows\SysWOW64\下

  5. 启动Rose

  6. OK

9、OOP

面向对象编程的特征:

  • 封装
  • 继承
  • 多态

10、IO

10.1 IO划分

输入输出流,操纵文件的读写过程。java的流架构图如下:

技术分享图片

IO类型的划分为:

  • 数据类型

    • 字节流

      InputStream / OutputStream

    • 字符流

      Reader / Writer

  • 方向

    • 输入流

      InputStream / Reader

    • 输出流

      OutputStream / Writer

  • 功能

    • 缓冲区流

      BufferedInputStream / BufferedReader / BufferedOutputStream / BufferedWriter

    • 转换流

      InputStreamReader

10.2 内存流

/**
  * 内存流
  */
@Test
public void writeByteArrayOutputStream() throws Exception {
  ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
  baos.write("abc".getBytes("unicode"));
  byte[] bytes = baos.toByteArray();
  baos.close();
  System.out.println(bytes.length);
}

10.3 压缩流

/**
  * 压缩流
  */
@Test
public void testZip() throws Exception {
  FileOutputStream fos = new FileOutputStream("d:\\java\\xxx.zip") ;
  ZipOutputStream zos = new ZipOutputStream(fos) ;

  byte[] buf = new byte[1024] ;
  int len = -1 ;

  File dir = new File("d:\\java\\zip") ;
  File[] files = dir.listFiles();
  for(File f : files){
    if(f.isFile()){
      //文件名
      String fname = f.getName() ;
      FileInputStream fin = new FileInputStream(f) ;
      zos.putNextEntry(new ZipEntry(fname));
      while((len = fin.read(buf)) != -1){
        zos.write(buf , 0 , len);
      }
      fin.close();
    }
  }
  zos.close();
  fos.close();
}

/**
  * 压缩流
  */
@Test
public void testUnzip() throws Exception {
  FileInputStream fin = new FileInputStream("d:\\java\\xxx.zip") ;
  ZipInputStream zis = new ZipInputStream(fin) ;
  byte[] buf = new byte[1024] ;

  int len = 0 ;
  //
  ZipEntry entry ;

  String ouputDir = "d:\\java\\zip\\out" ;

  while((entry = zis.getNextEntry()) != null){
    String fname = entry.getName() ;
    File f = new File(ouputDir , fname) ;
    FileOutputStream fos = new FileOutputStream(f) ;
    while((len = zis.read(buf)) != -1){
      fos.write(buf , 0 , len);
    }
    fos.close();
  }
  zis.close();
}

10.4 对象流

/**
  * 串行化
  */
@Test
public void testSerial() throws Exception {
  Person p = new Person("");
  p.setId(15);
  p.setName("tomas");
  p.setAge(22);

  FileOutputStream fos = new FileOutputStream("d:\\java\\p.dat") ;
  ObjectOutputStream oos = new ObjectOutputStream(fos) ;
  oos.writeObject(p);
  oos.close();
  fos.close();
}

/**
  * 串行化
  */
@Test
public void testDeserial() throws Exception {
  FileInputStream fis = new FileInputStream("d:\\java\\ppp.dat") ;
  ObjectInputStream ois = new ObjectInputStream(fis) ;
  Person p = (Person) ois.readObject();
  ois.close();
  fis.close();
  System.out.println(p.getName());
  System.out.println(p.isMarried());
}

11、设计模式

12、作业

  1. 把long数据转换成字节数组
  2. 把字节数组数据转换成long
  3. 有5亿整数(非负),去重计算不同整数的个数,300M内存
  4. 通过程序创建文本文件,内容是abc,采用uncode码,文件大小是10字节
  5. 将byte变换成无符号的整数(0 ~ 255 , 正数不变)

基本数据类型-位运算-字符集-流

标签:tin   tom   oop   指定   center   fileinput   -128   iso   字符流   

原文地址:https://www.cnblogs.com/xupccc/p/9594153.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!