偶尔会使用下Java,学习片段记录于此,持续更新……
字符串生成器
对字符串进行反复累加时使用字符串生成器性能更高。
package selfpackge;
import java.util.Date;
public class lauClass{
public static void main(String args[]){
String str = "";
long startTime = System.currentTimeMillis();
for(int i = 0; i!= 100000; ++i){
str += i;
}
long endTime = System.currentTimeMillis();
long lastTime = endTime - startTime;
System.out.println("Waste time: " + lastTime);
StringBuilder builderstr = new StringBuilder("");
long startTime1 = System.currentTimeMillis();
for (int i = 0; i!=100000;++i){
builderstr.append(i);
}
long endTime1 = System.currentTimeMillis();
long lastTime1 = endTime1 - startTime1;
System.out.println("Waste time: " + lastTime1);
}
}
文件读写
方法一缺点是如果读取对文件里是汉字可能会出现乱码,因为汉字占两个字节。方法二不存在这个问题。
package selfpackge;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
// 方法一
public class lauClass{
public static void main(String[]args){
File afile = new File("C:/Users/liugan5/Desktop/java.txt");
if (afile.exists()){
afile.delete();
System.out.println("java.txt has been deleted.");
}
try{
afile.createNewFile();
System.out.println("java.txt has been created.");
} catch (IOException e) {
e.printStackTrace();
}
try {
FileOutputStream out = new FileOutputStream(afile);
byte[]bt = "time cost:100ms\nprecision:0.969".getBytes();
out.write(bt);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream in = new FileInputStream(afile);
byte[] inbt = new byte[1024];
int len = in.read(inbt);
System.out.println(new String(inbt,0,len));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 方法二
public class lauClass{
public static void main(String[]args){
File afile = new File("C:/Users/liugan5/Desktop/java.txt");
if (afile.exists()){
afile.delete();
System.out.println("java.txt has been deleted.");
}
try{
afile.createNewFile();
System.out.println("java.txt has been created.");
} catch (IOException e) {
e.printStackTrace();
}
try {
FileWriter out = new FileWriter(afile);
String outstr = "time cost:100ms\nprecision:0.969";
out.write(outstr);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileReader in = new FileReader(afile);
char[] instr = new char[1024];
int len = in.read(instr);
System.out.println(new String(instr,0,len));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
时间计算
Java中计算耗时方法。
package selfpackge;
import java.util.Date;
public class lauClass{
public static void main(String args[]){
String str = "";
long startTime = System.currentTimeMillis();
for(int i = 0; i!= 100000; ++i){
str += i;
}
long endTime = System.currentTimeMillis();
long lastTime = endTime - startTime;
System.out.println("Waste time: " + lastTime);
StringBuilder builderstr = new StringBuilder("");
long startTime1 = System.currentTimeMillis();
for (int i = 0; i!=100000;++i){
builderstr.append(i);
}
long endTime1 = System.currentTimeMillis();
long lastTime1 = endTime1 - startTime1;
System.out.println("Waste time: " + lastTime1);
}
}