mikebai.com

  • Home
  • dev
  • DotNET
  • M365
  • 搞笑
  • 杂七杂八
  • FocusDict
dev
dev

String.getBytes("Unicode")的疑问

String.getBytes(charsetName),这个方法很多人都用过,可是有没有试过temp.getBytes("Unicode");这样的用法,它的返回值很奇怪,第1和第2个字节是-1或-2,比如下面的代码,你能想象出它的结果吗?             String temp = "a";         try {             byte[] unicodes = temp.getBytes("Unicode");             System.out.println("unicodes=" + unicodes.length);            for (int i = 0; i < unicodes.length; i++) {                System.out.println(unicodes[i]);            }             unicodes = temp.getBytes("UnicodeLittleUnmarked");            System.out.println("unicodes=" + unicodes.length);            for (int i = 0; i < unicodes.length; i++) {                System.out.println(unicodes[i]);            }                       unicodes = temp.getBytes("UnicodeBigUnmarked");            System.out.println("unicodes=" + unicodes.length);            for (int i = 0; i < unicodes.length; i++) {                System.out.println(unicodes[i]);            }         } catch (UnsupportedEncodingException e) {                       e.printStackTrace();        } 输出结果: unicodes=4-1-2970unicodes=2970unicodes=2097 为什么会有这种结果呢?蓝色的返回了四个字节,-1,-2,是字节顺的一种表示,这是由sun的类库实现,指示如果没有指定字节就使用默认的UnicodeLittle(在Window平台,别的平台我没测试),但为了标识这种字节顺,就使用了-1,-2在前面表示。 黑色的字,是UnicodeLittleUnmarked的结果,其返回字节只是两个字节,这与Unicode的编码相符合,注意到97,0与使用Unicode时后两个字节顺序一样。 红色的字,是使用UnicodeBigUnmarked的结果,字节顺与Little相反,也没有-1,-2. 由以上应该知道,temp.getBytes("Unicode");应该小心使用,应该注意它返回的-1,-2,这两个字节,因为在一些网络程序中,特别是当对方是由java以外的语言编写,有可能不会使用这种方式来标识字节顺,因此要了解对方的细节,这样才能保证数据的准确传递。

2011-06-19 0comments 101hotness 0likes mikebai Read all
dev

.NET和Java中BYTE的区别

1.有符号和无符号 c#中字节byte的范围是0~255; java中字节byte的范围是-128~127: 2.高低位顺序不同 比如16位整数10用16进制000A,在.net转换成byte数组是00 0A,而java/flash/flex等转换是0a 00 NET 转JAVA BitConverter.ToInt32(bytes, 0);(C#) JAVA:public static int ToInt32(byte[] bytes, int startIndex) {   int l = (int) bytes[startIndex] & 0xFF;   l += ((int) bytes[startIndex + 1] & 0xFF) << 8;   l += ((int) bytes[startIndex + 2] & 0xFF) << 16;   l += ((int) bytes[startIndex + 3] & 0xFF) << 24;   return l; }

2011-06-19 0comments 103hotness 0likes mikebai Read all
dev

Trim any of the characters

/* * Static String formatting and query routines. * Copyright (C) 2001-2005 Stephen Ostermiller * http://ostermiller.org/contact.pl?regarding=Java+Utilities * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * See COPYING.TXT for details. */ import java.util.HashMap;import java.util.regex.Pattern; /** * Utilities for String formatting, manipulation, and queries. * More information about this class is available from <a target="_top" href= * "http://ostermiller.org/utils/StringHelper.html">ostermiller.org</a>. * * @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities * @since ostermillerutils 1.00.00 */public class StringHelper {   /**   * Trim any of the characters contained in the second   * string from the beginning and end of the first.   *   * @param s String to be trimmed.   * @param c list of characters to trim from s.   * @return trimmed String.   * @throws NullPointerException if s is null.   *   * @since ostermillerutils 1.00.00   */  public static String trim(String s, String c){    int length = s.length();    if (c == null){      return s;    }    int cLength = c.length();    if (c.length() == 0){      return s;    }    int start = 0;    int end = length;    boolean found; // trim-able character found.    int i;    // Start from the beginning and find the    // first non-trim-able character.    found = false;    for (i=0; !found && i<length; i++){      char ch = s.charAt(i);      found = true;      for…

2011-06-17 0comments 107hotness 0likes mikebai Read all
dev

android java 的trim很考抹鲁啊

发现竟然没有重载函数.只能trim空格etc(ascii码一裤裆). 很考抹鲁啊.. 还是.net整的明白啊..... http://stackoverflow.com/questions/2088037/trim-characters-in-java 这个问题在网上一搜,还真是一堆实现方法..还有人特地计算了一下耗时,,,,, PS:eclipse里面没有Ctrl+C直接复制焦点行(非选择状态)+回车符号的功能啊 考抹鲁啊...................................... 有没有有没有啊.............................   public String trim( String stringToTrim, String stringToRemove ){    String answer = stringToTrim;     while( answer.startsWith( stringToRemove ) )    {        answer = answer.substring( stringToRemove.length() );    }     while( answer.endsWith( stringToRemove ) )    {        answer = answer.substring( 0, answer.length() - stringToRemove.length() );    }     return answer;} This answer assumes that the characters to be trimmed are a string. For example, passing in "abc" will trim out "abc" but not "bbc" or "cba", etc. Some performance times for running each of the following 10 million times. " mile ".trim(); runs in 248 ms included as a reference implementation for performance comparisons. trim( "smiles", "s" ); runs in 547 ms - approximately 2 times as long as java's String.trim() method. "smiles".replaceAll("s$|^s",""); runs in 12,306 ms - approximately 48 times as long as java's String.trim() method. And using a compiled regex pattern Pattern pattern = Pattern.compile("s$|^s"); pattern.matcher("smiles").replaceAll(""); runs in 7,804 ms - approximately 31 times as long as java's String.trim() method.

2011-06-16 0comments 128hotness 0likes mikebai Read all
dev

android创建文件夹和文件的一些经验教训

这几天做一个功能需要在手机上创建一个文件夹,然后往里面存储一些文件,首先得考虑用户有没有sdcard,如果有就在sdcard上创建一个指定的文件夹,如果没有则在你的工程所在的目录“/data/data/你的包名”下创建文件夹。用到的方法是:首先判断sdcard是否插入String status = Environment.getExternalStorageState();  if (status.equals(Environment.MEDIA_MOUNTED)) {   return true;  } else {   return false;  }然后根据是否插入状态指定目录if (SdcardHelper.isHasSdcard()) {   sDir = SDCARD_DIR;  } else {   sDir = NOSDCARD_DIR;  }然后是创建文件夹  File destDir = new File(sDir);  if (!destDir.exists()) {   destDir.mkdirs();  }问题是:刚开始我的文件夹的目录是按照windows方式的例如"\sdcard\tempdir"结果运行后也不报错但是怎么也创建不了文件夹,后面想到应该是按linux格式的目录,改为"/sdcard/tempdir"后即可成功创建。因为之前创建文件都是按照windows方式例如"\sdcard\test.txt"调用new File("\\sdcard\\test.txt").createNewFile();创建而且可以成功,所以目录就没考虑。经验证创建文件夹使用windows或者linux的目录结构都可以,而目录的话必须用linux的格式。最后我看网上说要加入以下权限: <!--往sdcard中写入数据的权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <!--在sdcard中创建/删除文件的权限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>但是我没加也可以成功创建。具体为什么没有细入研究。知道的兄弟望告知一声谢了。   //在sdcard上根目录创建一个notes文件夹File sdCard = Environment.getExternalStorageDirectory();rootPath = sdCard.getPath()+"/notes";File file=new File(rootPath);if(!file.exists()){ file.mkdir();}

2011-06-14 0comments 108hotness 0likes mikebai Read all
dev

Android模拟器SD卡的使用

 Andorid开发中经常遇到与SD卡有关的调试,Android SDK+Eclipse提供了SD卡的模拟方法。使用方法如下:   1、创建一个SD卡镜像文件。    打开cmd,输入如下命令:    mksdcard 1024M sdcard.img    该命令会在当前目录下生成一个sdcard.img文件,该文件就是Android模拟器的SD卡镜像文件。    1024M表示1024兆,即该SD卡有1个G的容量,也可用K做单位(1M=1024K),K,M必须大写。    目前Android支持8M~128G的SD卡。   2、运行带有SD卡的模拟器   创建了SD卡镜像文件,只是创建了一个文件,还不能在模拟器中直接用,要在模拟器中可看到该SD卡,方法有两种,如下:       ◆在cmd中,命令如下:    emulator -sdcard e:sdcard.img    ◆在Eclipse中,在Run->Run Configurations...菜单里面的Target标签页里面,输入启动参数,如下图:   3、向SD卡中导文件    ◆在cmd中,命令如下:   这会将本地当前目录下的test.txt文件考到sdcard中,文件名不变。前一个test.txt是本地文件的路径,sdcard是目的sdcard镜像的文件名(去掉后缀)。    ◆在Eclipse中,操作如下:    在设置了RUN的命令参数之后,RUN一个应用,然后使用DDMS的File Explorer工具导入导出文件。    打开DDMS工具:在Eclipse的Window->Open Perspective->Other...里面打开DDMS工具。    在DDMS的File Explorer标签页里面选择sdcard目录导入导出文件,如下图:      4、在模拟器中使用SD卡中的文件    导入文件后,如果要在模拟器中访问,还需要在模拟器中的Dev tools里面scan一下媒体文件,如下图:                 来源:blogjava.net    作者:江天部落格    责编:豆豆技术应用

2011-06-11 0comments 125hotness 0likes mikebai Read all
dev

笔记2011-06-09

Android 配置文件里面"@+id/"和"@id/"的区别@+id是定义组件的id的@id是引用id的

2011-06-09 0comments 123hotness 0likes mikebai Read all
dev

java的super和C#中的base

using System;using System.Collections.Generic;using System.Text; namespace ConsoleApplication4{    public class Person    {        public string name;        public uint age;        public Person(string name, uint age)        {            this.name = name;            this.age = age;            Console.WriteLine(name);            Console.WriteLine(age);        }    }    public class Student : Person    {        private uint id;        public Student(string name, uint age, uint id)            : base(name, age)        {            this.id = id;            Console.WriteLine(id);        }    }    class Program    {        static void Main(string[] args)        {            Student stu = new Student("lihaifeng", 15, 2);        }    }}//这个是C#中的base,base关键字用于从派生类中访问基类成员。即使基类的方法已经在派生类中重写,仍然可以使用base关键字调用。在创建派生类的实例时,可以使用base关键字调用基类的构造方法。base关键字只能访问基类的构造方法,实例方法,和实例属性,不能访问基类的静态方法 package mypackage;class Base{ Base(){ } Sub s = new Sub(); public void m(){ System.out.println("父类的方法");} } class Sub extends Base{ Sub(){ super(); //调用父类的构造方法 super.m();//调用父类的方法 } public void n(){ System.out.println("子类的方法"); } public static void main(String[] args){ Sub s = new Sub(); s.m();//这里应该理解为: //子类继承了父类,那么这个m()应该属于子类的了!,所以我们在重写的时候要覆盖父类的方法! 那么这里算调用子类自己的方法了 Base b = new Base(); b.m();//父类对象调用自己方法 b.s.n(); //父类调用子类的方法//b.n();//编译错误,因为子类的方法对父类不可见! } } abstract class Shape{protected double length;protected double width;Shape(double num,double num1){   this.length=num;   this.width=num1;  }abstract double area();}class Square extends Shape{Square(double a,double b){super(a,b);}double area(){   System.out.println("正方形的面积为:"+length*width);   return length*width;  }}class Triangle extends Shape{Triangle(double a,double b){   super (a,b);}double area(){   System.out.println("三角形的面积为");   return (0.5*length*width);}} class CalculateArea{protected CalculateArea(){}public static void main(String args[]){   Shape fobj;   Square sqobj=new Square(10,20);   Triangle trobj=new Triangle(12,8);   fobj=sqobj;   System.out.println(fobj.area());   fobj=trobj;   System.out.println(fobj.area());  }} 示例声明了一个Shape的抽象类。这个类包含一个名为area()的抽象方法,两个类square和traingle派生自shape类并重写方法area().           shape类的对象不能被实例化,因为他是抽象的,对象fobj声明为sha

2011-03-01 0comments 126hotness 0likes mikebai Read all
dev

eclipse @author Administrator问题

创建java文件时,注释中作者会自动录入为当前pc的用户名.用以下方法解决 eclipse ide中:preferences > Java > Code Style > Code Templates> Code > 选择“New Java files”项,点编辑按钮--默认为------------${filecomment}${package_declaration}${typecomment}${type_declaration}--修改后为------------${package_declaration}     /**   * ${file_name}* @version 1.0* @author abcdefg* @createTime ${date} ${time} */     ${typecomment}   ${type_declaration}

2011-02-28 0comments 122hotness 0likes mikebai Read all
dev

final、static(Java)和const、static(C#)

C#中的static 和Java中的static 简单,两者用法完全是一致的。从两方面讨论: 1. 变量是属于类的,不是实例级别的。只能通过类名调用,不能通过实例调用。 2. 如果在定义时就赋值了,那么在类初始化的时候,最先完成所有静态变量的赋值。但是要注意,所有静态变量的初始化顺序是无法确定的。 C# 中的const 和Java中的finnal 很长一段时间我一直认为两者是相同的作用,无非是变量初始化后不能更改,即只能在定义时或者构造函数中赋值。然而这仅仅只是片面的,下面将为大家详细分析: 1.修饰变量 准确的说C#中的const 等价于 Java中的static final,也就是说,Java中final不具有static的功能。而C#中的const具有static的功能。因此在C#中 public static const string 等将于 public const string。 2.修饰类和方法 此时Java中的final类似C#中的sealed,就是说,final修饰的类不能被继承,final修饰的方法不能被覆盖。 而C#中的const不能修饰类和方法。 问题: 1. 私有静态成员的作用(private static 变量) 字面表示私有的,类外不能使用;静态的,全局变量。看上去很矛盾,又不能被类外使用,要全局的有什么用。问得好,类中全局也是很有意义的,例如 private static int a = 5,那么就可以保证变量a在类的初始化过程中将被优先初始化(在构造函数执行之前)。这样如果对象A的初始化需要对象B的实例,那么就可以用这种申明,以保证在类A在构造函数中能够使用类B的实例。同时private又能够保证类B的实例只能在类A中使用,起到很好的密封作用。 2. 私有最终成员作用(private final 变量) 在类构造函数完成前必须对该成员完成初始化,一旦定义不许更改;该成员只能在本类中使用。实例,子类中都不能使用。 private static final修饰的成员在申明的时就被赋值,保证在构造函数中可以被使用,一个被private static final修饰的成员通常表示其他组件的一个实例,且变量是类中的全局变量。 private final         修饰的成员在构造中被赋值,表示它是该类全局的私有成员变量,且该类的构造需要传入他们的初始值,才能完成类的初始化。

2011-02-24 0comments 128hotness 0likes mikebai Read all
1…1920212223…25

Recent Posts

  • c# winform适配高dpi
  • com.microsoft.sqlserver.jdbc.SQLServerException “trustServerCertificate”属性设置为“false”,但驱动程序无法使用安全套接字层 (SSL) 加密与 SQL Server建立安全连接
  • java -cp 用法介绍
  • HTML 容器元素
  • MVC的cshtml的介绍

Recent Comments

No comments to show.

COPYRIGHT © 2025 mikebai.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang