4.整型或其他类型与字符串类型的相互转换。
int a = Integer.parseInt(String s)
5.写到文件中:
static private int freqShink(String freqFileName, int freq){
int return_value = -1;
FileInputStream fin = null;
FileWriter writer = null;
File cpu_scaling_file = new File(freqFileName);
try{
fin = new FileInputStream(cpu_scaling_file);
int length = fin.available();
byte [] buffer = new byte[length];
fin.read(buffer);
String res = new String(buffer);
int old_freq = Integer.parseInt(res.trim()); //文件中内容读到byte数组,转为字符串,再从中转为整型
writer = new FileWriter(freqFileName);
writer.write(Integer.toString(freq).toCharArray()); //将整型转为字符串,再转为字符数组写到文件中
if(DEBUG){Log.i(TAG, freqFileName + ", change from " + old_freq + " to " + freq);}
}catch(Exception e){
Log.e(TAG, e.toString());
Log.e(TAG, "Can't change the cpu freq: " +
freqFileName +
", ignore it.");
}finally{
if (fin != null) {
try {
fin.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return return_value;
}
8.常用的正则表达式:如
public static boolean isNumeric(String str){ //是数字返回true
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
private boolean ifLegal(String motorID){
char c = motorID.charAt(0);
String sub= motorID.substring(1);
if (isNumeric(sub) && !Character.isLowerCase(c)){ //判断大写字母
return true;
}
return false;
}
9.创建自己需要的简单的类:
如:
public class Point{
private int pointX,pointY;
public Point(int x,int y){
pointX=x;
pointY=y;
}
public void putPointX(int point){
pointX = point;
}
public int getPointX(){
return pointX;
}
public void putPointY(int point){
pointY = point;
}
public int getPointY(){
return pointY;
}
}