Java日常

偶尔会使用下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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值