zoukankan      html  css  js  c++  java
  • 开发测试类2

    --从文件里面按行读取数据

    public
    class Testbyte { /** * @param args * @throws FileNotFoundException */ public static void main(String[] args) throws FileNotFoundException { File f = new File("C:\11.txt"); BufferedReader bf = new BufferedReader(new FileReader(f)); Scanner sc=new Scanner(bf); //Scanner sc=new Scanner(new FileReader("c:\22.txt")); System.out.println(sc.toString()); String line=null; while((sc.hasNextLine()&&(line=sc.nextLine())!=null)){ if(!sc.hasNextLine()) System.out.println(line); } } }
    
    
    --Continue用法

    public
    class TestContinue { /** * @param args */ public static void main(String[] args) { for(int i=0;i<10;i++){ if(i==5){ continue; } System.out.println(i); } int a="2222".compareTo("8111"); System.out.println(a); } }

    --在catch里面return也是可以的
    public
    class TestExcp { /** * @param args */ public static void main(String[] args) { String q=testExcp(); System.out.println(q); } private static String testExcp(){ String a=null; try{ a.equals("1"); }catch (Exception e) { return "1"; } return "12"; } }
    --finally在return之后执行
    public
    class TestFinally { /** * @param args */ public static void main(String[] args) { test(); } private static String test(){ try{ System.out.println("1111"); return test1(); }catch (Exception e) { // TODO: handle exception } finally{ System.out.println("22222"); } return null; }
       private static String test1(){ System.out.println("return out"); return null; } }
    public class TestJson {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            File file=new File("c://json.txt");
            FileInputStream fis=new FileInputStream(file);
            byte[] b=new byte[2048];
            fis.read(b);
            String jsonString =new String(b);
            jsonString =jsonString.trim();
            System.out.println("json字符串:
    "+jsonString);
            jsonString=jsonString.replaceAll("
    |
    ", "");
            System.out.println("json字符串:
    "+jsonString);
            JSONObject jb = JSONObject.fromObject(jsonString);
            JSONArray ja = jb.getJSONArray("term_params");
            String datetime=jb.getString("datetime");
            System.out.println("datetime_____:"+datetime);
            for (int i = 0; i < ja.size(); i++) {
                String sn=ja.getJSONObject(i).getString("sn");
                System.out.println("sn_____:"+sn);
                String keyParamJson=ja.getJSONObject(i).getString("key_params");
                System.out.println("keyParamJson_______"+keyParamJson);
                //替换[ 和 ]
                keyParamJson=keyParamJson.replaceAll("(\[)", "");
                keyParamJson=keyParamJson.replaceAll("(\])", "");
                System.out.println("keyParamJson_______"+keyParamJson);
                JSONObject jbParams = JSONObject.fromObject(keyParamJson);
                String a=jbParams.getString("auk_data");
                System.out.println("auk_data____:"+a);
            }
            
    
        }
    
    }
    public class TestLen {
    
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
            File file=new File("c://11.txt");
            try {
                FileInputStream fis=new FileInputStream(file);
                byte[] b=new byte[4];
                fis.read(b);
                byte[] c=new byte[4];
                fis.read(c);
                //String len = new String(b,"ISO-8859-1");
                String len = new String(b);
                System.out.println(len);
                
                String cstr = new String(c);
                System.out.println(cstr);
                int lenInt=Integer.valueOf(len);
                System.out.println(lenInt);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    
        }
    
    }
    public class TestNewLine {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String a="111";
            String b="
    ";
            String c=a+b;
            System.out.println(c.length());
            System.out.println(c);
    
        }
    
    }
    public class TestRandom {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            Random r=new Random();
            int a=r.nextInt(6000);
            System.out.println(a);
    
        }
    
    }
    public class TestReturn {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            String a = a();
            System.out.println(a);
        }
    
        private static String a() {
            if (true) {
                if (true) {
                    return "2";
                }
                return "1";
            }
            return null;
        }
    
    }
    public class TestSplit {
    
        /**
         * @param args
         * @throws IOException
         */
        public static void main(String[] args) throws IOException {
            File f = new File("C:\11.txt");
            BufferedReader bf = new BufferedReader(new FileReader(f));
            String str;
            String sourceLen = bf.readLine();
            System.out.println(sourceLen);
            int lengthAll = sourceLen.length();
            int lengthLast = 0;
            String mac = null;
            while ((str = bf.readLine()) != null) {
                System.out.println("总长度" + lengthAll);
                System.out.println("本次长度" + str.trim().length());
                System.out.println("上一个数据长度" + lengthLast);
                if (lengthLast == 0 || lengthLast == str.trim().length()) {
    
                    lengthAll = lengthAll + str.length();
                    String[] s = str.split(",");
                    System.out.println(s[1]);
                    lengthLast = str.trim().length();
                } else {
                    mac = str;
                    System.out.println("MAC:" + mac);
                }
    
            }
    
        }
    
    }
    public class TestTime {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
             Date date = new Date();
             System.out.println(date.getTime()-5555);
             System.out.println(new Date());
        }
    
    }
    public class TestVarArgus {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            String a=null;
            String b="1";
            String c="dsada";
            verifyString(c,a,b);
    
        }
        private static void verifyString(String...msgs){
            for(String s:msgs){
                if(StringUtil.isEmpty(s)){
                    System.out.println("ok");
                    break;
                }
                System.out.println(s);
            }
        }
    
    }

    public static void main(String[] args) {        test();    }    private static String test(){        try{            System.out.println("1111");            return test1();                    }catch (Exception e) {            // TODO: handle exception        }        finally{                        System.out.println("22222");        }        return null;    }    private static String test1(){    System.out.println("return out");    return null;    }

  • 相关阅读:
    7.6实战练习
    构造方法的主要应用
    6.0字符串String
    数组冒泡排序
    数组(二维数组)
    5.1数组(一维部分)
    4个方位的三角形和菱形
    4.3循环语句
    控制台输入输出
    4 java语句 4.2条件语句
  • 原文地址:https://www.cnblogs.com/landauni/p/6763571.html
Copyright © 2011-2022 走看看