//如何读大文件:
package com.zhoubo;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class ReadBigFile {
/*
* @param args
*/
public static void main(String[] args)throws Exception {
//定义缓冲区的大小为1KB
int bufSize = 1024;
byte [] bs = new byte[bufSize];
ByteBuffer byteBuffer = ByteBuffer.allocate(bufSize);
FileChannel channel = new RandomAccessFile("c://dmmsi.log","r").getChannel();
int size;
//读取到输入流的最后
while((size = channel.read(byteBuffer))!=-1){
byteBuffer.rewind();
byteBuffer.get(bs);
System.out.println(new String(bs, 0, size));
byteBuffer.clear();
}
channel.close();
}
提供一个字符串,转化成10进制输出,例如“AA”,输出170 :
package com.zhoubo;
/**
*
* @author BZ70000910
*
*/
public class ZTETest {
/**
* @param args
*/
public static void main(String[] args) {
String str = "ABCDE" ;
ZTETest test = new ZTETest();
System.out.println(test.decimal(str));
}
public int decimal(String str){
char [] chars;
if(str != ){
chars = str.toCharArray();
}else
chars = ;
int sum = 0;
int j = 0;
for(int i = str.length()-1; i>=0 ; i--){
sum = sum + (getValue(chars[i])<<(4*j));
j++;
}
return sum;
}
public int getValue(char x){
switch(x){
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'A': return 10;
case 'B': return 11;
case 'C': return 12;
case 'D': return 13;
case 'E': return 14;
case 'F': return 15;
default: return 0 ;
}
}
}
(三) 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,
输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
package com.zhoubo;
public class ZTETest2 {
/**
* @param args
*/
public static void main(String[] args) {
String str = "我abc汉def";
ZTETest2 test = new ZTETest2();
System.out.println(str.substring(0,4));
System.out.println(test.StringCutByByte(str, 2));
}
public String StringCutByByte(String str, int index){
String temp = ;
byte [] bytes = str.getBytes();
byte [] bytetemp = new byte[index];
for(int i=0; i<index ; i++ ){
bytetemp[i] = bytes[i];
System.out.println(bytes[i]);
}
if(index>1){
if(bytes[index-1]<0&&bytes[index-2]>0){
//第三个参数表示要解码的字节数
temp = new String(bytetemp,0,index-1);
}else{
temp = new String(bytetemp);
}
}else{
if(bytes[index-1]<0){
temp = ;
}else
temp = new String(bytetemp,0,1);
}
return temp;
}
}