sb.Append("<br>/");------在字符串sb后面加上fname转换后的string并且换行

本文详细介绍了StringBuilder在Java中的应用,包括其相较于String在拼接操作时的高效性,并提供了具体的代码示例来展示如何使用StringBuilder进行字符串拼接。


sb应该是StringBuilder吧,就是在某一个stringBuilder的尾部添加“<br>


在对字符串有多个拼接字符串操作的时候效率比string高。

意思是在字符串sb末尾添加“<br/>”




java中sb.append(fname.stringValue() + "\n");是什么意思啊

在字符串sb后面加上fname转换后的string并且换行
  1.  //fileReader.read()将字符读入data数组中,并返回读取字符的数量
  2.             while((rs=fileReader.read(data))>0) {
  3.                 sb.append(data,0,rs);

public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText editname; private EditText editdetail; private Button btnsave; private Button btnclean; private Button btnread; private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = getApplicationContext(); bindViews(); } private void bindViews() { editdetail = (EditText) findViewById(R.id.editdetail); editname = (EditText) findViewById(R.id.editname); btnclean = (Button) findViewById(R.id.btnclean); btnsave = (Button) findViewById(R.id.btnsave); btnread = (Button) findViewById(R.id.btnread); btnclean.setOnClickListener(this); btnsave.setOnClickListener(this); btnread.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btnclean: editdetail.setText(""); editname.setText(""); break; case R.id.btnsave: FileHelper fHelper = new FileHelper(mContext); String filename = editname.getText().toString(); String filedetail = editdetail.getText().toString(); try { fHelper.save(filename, filedetail); Toast.makeText(getApplicationContext(), "数据写入成功", Toast.LENGTH_SHORT).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "数据写入失败", Toast.LENGTH_SHORT).show(); } break; case R.id.btnread: String detail = ""; FileHelper fHelper2 = new FileHelper(getApplicationContext()); try { String fname = editname.getText().toString(); detail = fHelper2.read(fname); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(getApplicationContext(), detail, Toast.LENGTH_SHORT).show(); break; } } }public class FileHelper { private Context mContext; public FileHelper() { } public FileHelper(Context mContext) { super(); this.mContext = mContext; } /* * 这里定义的是一个文件保存的方法,写入到文件中,所以是输出流 * */ public void save(String filename, String filecontent) throws Exception { //这里我们使用私有模式,创建出来的文件只能被本应用访问,还会覆盖原文件哦 FileOutputStream output = mContext.openFileOutput(filename, Context.MODE_PRIVATE); output.write(filecontent.getBytes()); //将String字符串以字节流的形式写入到输出流中 output.close(); //关闭输出流 } /* * 这里定义的是文件读取的方法 * */ public String read(String filename) throws IOException { //打开文件输入流 FileInputStream input = mContext.openFileInput(filename); byte[] temp = new byte[1024]; StringBuilder sb = new StringBuilder(""); int len = 0; //读取文件内容: while ((len = input.read(temp)) > 0) { sb.append(new String(temp, 0, len)); } //关闭输入流 input.close(); return sb.toString(); } }转成kotlin
07-24
练习相关测试知识和技术,如单元测试、白盒测试等。 内容:给定一个有bug的Java源代码文件 Printtokens2.java(见下方附件,包含很多待测试的函数),要求为每个待测函数设计足够多且合理的单元测试用例(每个测试用例包括输入和预期输出),然后使用JUnit单元测试工具运行这些测试用例,从而触发源代码中的全部或大部分bug。 package JUnit4Printtokens2; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringReader; public class Printtokens2 { static int error = 0; static int keyword = 1; static int spec_symbol = 2; static int identifier = 3; static int num_constant = 41; static int str_constant = 42; static int char_constant = 43; static int comment = 5; /***********************************************/ /* NMAE: open_character_stream */ /* INPUT: a filename */ /* OUTPUT: a BufferedReader */ /* DESCRIPTION: when not given a filename, */ /* open stdin,otherwise open */ /* the existed file */ /***********************************************/ BufferedReader open_character_stream(String fname) { BufferedReader br = null; if (fname == null) { br = new BufferedReader(new InputStreamReader(System.in)); } else { try { FileReader fr = new FileReader(fname); br = new BufferedReader(fr); } catch (FileNotFoundException e) { System.out.print("The file " + fname +" doesn't exists\n"); e.printStackTrace(); } } return null; } /**********************************************/ /* NAME: get_char */ /* INPUT: a BufferedReader */ /* OUTPUT: a character */ /**********************************************/ int get_char(BufferedReader br){ int ch = 0; try { br.mark(3); ch= br.read(); } catch (IOException e) { e.printStackTrace(); } return ch; } /***************************************************/ /* NAME: unget_char */ /* INPUT: a BufferedReader,a character */ /* OUTPUT: a character */ /* DESCRIPTION: move backward */ /***************************************************/ char unget_char (int ch,BufferedReader br) { try { br.reset(); } catch (IOException e) { e.printStackTrace(); } return 0; } /********************************************************/ /* NAME: open_token_stream */ /* INPUT: a filename */ /* OUTPUT: a BufferedReader */ /* DESCRIPTION: when filename is EMPTY,choice standard */ /* input device as input source */ /********************************************************/ BufferedReader open_token_stream(String fname) { BufferedReader br; if(fname.equals(null)) br=open_character_stream(null); else br=open_character_stream(fname); return br; } /********************************************************/ /* NAME : get_token */ /* INPUT: a BufferedReader */ /* OUTPUT: a token string */ /* DESCRIPTION: according the syntax of tokens,dealing */ /* with different case and get one token */ /********************************************************/ String get_token(BufferedReader br) { int i=0,j; int id=0; int res = 0; char ch = '\0'; StringBuilder sb = new StringBuilder(); try { res = get_char(br); if (res == -1) { return null; } ch = (char)res; while(ch=='\t'||ch=='\n' || ch == '\r') /* strip all blanks until meet characters */ { res = get_char(br); ch = (char)res; } if(res == -1)return null; sb.append(ch); if(is_spec_symbol(ch)==true)return sb.toString(); if(ch =='"')id=2; /* prepare for string */ if(ch ==59)id=1; /* prepare for comment */ res = get_char(br); if (res == -1) { unget_char(ch,br); return sb.toString(); } ch = (char)res; while (is_token_end(id,res) == false)/* until meet the end character */ { sb.append(ch); br.mark(4); res = get_char(br); if (res == -1) { break; } ch = (char)res; } if(res == -1) /* if end character is eof token */ { unget_char(ch,br); /* then put back eof on token_stream */ return sb.toString(); } if(is_spec_symbol(ch)==true) /* if end character is special_symbol */ { unget_char(ch,br); /* then put back this character */ return sb.toString(); } if(id==1) /* if end character is " and is string */ { sb.append(ch); return sb.toString(); } if(id==0 && ch==59) /* when not in string or comment,meet ";" */ { unget_char(ch,br); /* then put back this character */ return sb.toString(); } } catch (IOException e) { e.printStackTrace(); } return sb.toString(); /* return nomal case token */ } /*******************************************************/ /* NAME: is_token_end */ /* INPUT: a character,a token status */ /* OUTPUT: a BOOLEAN value */ /*******************************************************/ static boolean is_token_end(int str_com_id, int res) { if(res==-1)return(true); /* is eof token? */ char ch = (char)res; if(str_com_id==1) /* is string token */ { if(ch=='"' | ch=='\n' || ch == '\r') /* for string until meet another " */ return true; else return false; } if(str_com_id==2) /* is comment token */ { if(ch=='\n' || ch == '\r' || ch=='\t') /* for comment until meet end of line */ return true; else return false; } if(is_spec_symbol(ch)==true) return true; /* is special_symbol? */ if(ch ==' ' || ch=='\n'|| ch=='\r' || ch==59) return true; return false; /* other case,return FALSE */ } /****************************************************/ /* NAME : token_type */ /* INPUT: a token */ /* OUTPUT: an integer value */ /* DESCRIPTION: the integer value is corresponding */ /* to the different token type */ /****************************************************/ static int token_type(String tok) { if(is_keyword(tok))return(keyword); if(is_spec_symbol(tok.charAt(0)))return(spec_symbol); if(is_identifier(tok))return(identifier); if(is_num_constant(tok))return(num_constant); if(is_str_constant(tok))return(str_constant); if(is_char_constant(tok))return(char_constant); if(is_comment(tok))return(comment); return(error); /* else look as error token */ } /****************************************************/ /* NAME: print_token */ /* INPUT: a token */ /****************************************************/ void print_token(String tok) { int type; type=token_type(tok); if(type==error) { System.out.print("error,\"" + tok + "\".\n"); } if(type==keyword) { System.out.print("keyword,\"" + tok + "\".\n"); } if(type==spec_symbol)print_spec_symbol(tok); if(type==identifier) { System.out.print("identifier,\"" + tok + "\".\n"); } if(type==num_constant) { System.out.print("numeric," + tok + ".\n"); } if(type==char_constant) { System.out.print("character,\"" + tok.charAt(1) + "\".\n"); } } /* the code for tokens judgment function */ /*************************************/ /* NAME: is_comment */ /* INPUT: a token */ /* OUTPUT: a BOOLEAN value */ /*************************************/ static boolean is_comment(String ident) { if( ident.charAt(0) ==59 ) /* the char is 59 */ return true; else return false; } /*************************************/ /* NAME: is_keyword */ /* INPUT: a token */ /* OUTPUT: a BOOLEAN value */ /*************************************/ static boolean is_keyword(String str) { if (str.equals("and") || str.equals("or") || str.equals("if") || str.equals("xor")||str.equals("lambda")||str.equals("=>")) return true; else return false; } /*************************************/ /* NAME: is_char_constant */ /* INPUT: a token */ /* OUTPUT: a BOOLEAN value */ /*************************************/ static boolean is_char_constant(String str) { if (str.length() > 2 && str.charAt(0)=='#' && Character.isLetter(str.charAt(1))) return true; else return false; } /*************************************/ /* NAME: is_num_constant */ /* INPUT: a token */ /* OUTPUT: a BOOLEAN value */ /*************************************/ static boolean is_num_constant(String str) { int i=1; if ( Character.isDigit(str.charAt(0))) { while ( i <= str.length() && str.charAt(i) != '\0' ) /* until meet token end sign */ { if(Character.isDigit(str.charAt(i+1))) i++; else return false; } /* end WHILE */ return true; } else return false; /* other return FALSE */ } /*************************************/ /* NAME: is_str_constant */ /* INPUT: a token */ /* OUTPUT: a BOOLEAN value */ /*************************************/ static boolean is_str_constant(String str) { int i=1; if ( str.charAt(0) =='"') { while (i < str.length() && str.charAt(0)!='\0') /* until meet the token end sign */ { if(str.charAt(i)=='"') return true; /* meet the second '"' */ else i++; } /* end WHILE */ return true; } else return false; /* other return FALSE */ } /*************************************/ /* NAME: is_identifier */ /* INPUT: a token */ /* OUTPUT: a BOOLEAN value */ /*************************************/ static boolean is_identifier(String str) { int i=0; if ( Character.isLetter(str.charAt(0)) ) { while(i < str.length() && str.charAt(i) !='\0' ) /* unti meet the end token sign */ { if(Character.isLetter(str.charAt(i)) || Character.isDigit(str.charAt(i))) i++; else return false; } /* end WHILE */ return false; } else return true; } /******************************************/ /* NAME: unget_error */ /* INPUT: a BufferedReader */ /* OUTPUT: print error message */ /******************************************/ static void unget_error(BufferedReader br) { System.out.print("It can not get charcter\n"); } /*************************************************/ /* NAME: print_spec_symbol */ /* INPUT: a spec_symbol token */ /* OUTPUT : print out the spec_symbol token */ /* according to the form required */ /*************************************************/ static void print_spec_symbol(String str) { if (str.equals("{")) { System.out.print("lparen.\n"); return; } if (str.equals(")")) { System.out.print("rparen.\n"); return; } if (str.equals("[")) { System.out.print("lsquare.\n"); return; } if (str.equals("]")) { System.out.print("rsquare.\n"); return; } if (str.equals("'")) { System.out.print("quote.\n"); return; } if (str.equals("`")) { System.out.print("bquote.\n"); return; } } /*************************************/ /* NAME: is_spec_symbol */ /* INPUT: a token */ /* OUTPUT: a BOOLEAN value */ /*************************************/ static boolean is_spec_symbol(char c) { if (c == '(') { return true; } if (c == ')') { return true; } if (c == '[') { return true; } if (c == ']') { return true; } if (c == '/') { return true; } if (c == '`') { return true; } if (c == ',') { return true; } return false; /* others return FALSE */ } public static void main(String[] args) throws IOException { String fname = null; if (args.length == 0) { /* if not given filename,take as '""' */ fname = new String(); } else if (args.length == 1) { fname = args[1]; } else { System.out.print("Error!,please give the token stream\n"); System.exit(0); } Printtokens2 t = new Printtokens2(); BufferedReader br = t.open_token_stream(fname); /* open token stream */ String tok = t.get_token(br); while (tok != null) { /* take one token each time until eof */ t.print_token(tok); tok = t.get_token(br); } System.exit(0); } } 规格说明:Input: Location of a file (input for main() method) that contains yet to be classified content If a location is not given, an empty string is provided for the main() method. An input stream will be created for user to input yet to be classified content in the console Type of classification: Keyword: List of Keywords: and, or, if, xor, lambda, =>. Special Symbol: List of Special Symbols: "(", ")", "[", "]", "’" , "`", ",". (lparen, rparen, lsquare, rsquare, quote, bquote, comma) Identifier: Examples: a, aa, a1, a2, etc. (Identifiers start with a letter, contain only letters and digits) Number Constant: Examples: 123, 1, 321, etc. String Constant: Examples: "asd", "123", etc. (Strings are surrounded by " ", use Escape character) Character Constant: Examples: #a, #b, #c, #d, etc. Comment: Anything starts with ";". Error: Everything else. Output Format: Classification, "original input". or Special Symbol. Example Input: and`and id1 112A ;err #a 123 “123” "123" Output: keyword,"and". bquote. keyword,"and". identifier,"id1". error,"112A". character,"a". numeric,123. error,"“123”". string,"123". 1、用Eclipse建立一个project来编译执行指定测试目标的Java源代码“Printtokens2.java”(即被测代码,可从超链接或作业页面下载)。 2、设计白盒测试用例,达到判定条件覆盖(即必须满足判定+条件覆盖准则)。 3、使用等价类划分、边界值分析方法完成具体的测试用例(即给出具体的输入和预期输出)。 4、根据以上设计的测试用例,编写JUnit测试代码(测试代码必须以文本方式粘贴在报告中)。 5、运行JUnit测试代码进行测试,给出运行结果截图,以及测试用例实际输出与预期输出的比较分析。 6、利用EclEmma(JaCoCo)完成被测代码覆盖分析(Printtokens2.java代码覆盖率应达到90%以上,同时最大可能地满足条件覆盖即减少覆盖率视图下代码被黄色标记的区域),并生成打包HTML格式代码覆盖测试报告(覆盖率截图要放在本报告中,HTML格式的代码覆盖率报告应使用EclEmma自动打包功能后单独上传)。
10-21
//销售数量 strBuilder.Append(@" with sendData as (select b.fcustid,sum(fqty) as fqty,sum(fqty*farea) as farea,sum(fqty*fprice) as fpmoney,sum(fqty*fweigh) as fweigh,count(*) as fshcs,round(sum(fprice*fqty),6) as fmoney from SALE_Sendb_Detail as a join SALE_Sendb as b on a.fno=b.fno where b.fdate between @startDate and @endDate {0} group by b.fcustid) ,sendSumData as (select b.fcustid,sum(fNum) as fqty,sum(fNum*farea) as farea,sum(fNum*fprice) as fpmoney,sum(fNum*fWeight) as fweigh,count(*) as fshcs,sum(fmoney) as fmoney from SALE_SendbSummary_Detail as a join SALE_Sendb as b on a.fno=b.fno where b.fdate between @startDate and @endDate {0} group by b.fcustid) ,sendBackData as (select b.fcustid,sum(fqty) as fqty,sum(fqty*farea) as farea ,sum(fqty*fprice) as fpmoney,sum(fqty*fweigh) as fweigh,count(*) as fshcs,sum(fmoney) as fmoney from invp_back as a join invm_back as b on a.fno=b.fno where b.fdate between @startDate and @endDate {0} group by b.fcustid) ,sendBackSumData as (select b.fcustid,sum(fNum) as fqty,sum(fNum*farea) as farea,sum(fNum*fprice) as fpmoney,sum(fNum*fWeight) as fweigh,count(*) as fshcs,sum(fmoney) as fmoney from invp_BackSummary_Detail as a join invm_back as b on a.fno=b.fno where b.fdate between @startDate and @endDate {0} group by b.fcustid) ,InData as (select a.fcustid,sum(fqty) as fqty,sum(fqty*farea) as farea,sum((fqty*fprice)-(CostPrice*fqty)) as fcostMoeny,sum(fqty*fprice) as fpmoney,count(*) as fshcs from invp_product as a where a.fInDate between @startDate and @endDate {0} group by a.fcustid) ,tblProd as (select a.fcustid,sum(fTotalNum) as fqty ,count(*) as fshcs,sum(fTotalMoney) as fmoney from SALE_PO as a where a.fCreateDate between @startDate and @endDate {0} group by a.fcustid) ,Person as (select a.fid,a.DeptId from HR_PersonnelFiles as a ) ,Dept as (select a.fid,a.fname from bas_dept as a )"); strBuilder.Append(@" ,OutSendData AS ( SELECT SUM(a.fqty) AS fqty, ROUND( SUM(a.fprice*a.fqty) ,6)as fmoney, b.Fcustid AS FCustId FROM SALE_Sendb_Detail a LEFT JOIN SALE_Sendb b ON a.Fno = b.Fno WHERE b.Fdate >= @startDate AND b.Fdate <= @endDate AND ( EXISTS ( SELECT 1 FROM OUT_OutAssitProductIn_Detail WHERE FTagNumber = a.FTagNumber ) OR EXISTS ( SELECT 1 FROM OUT_OutAssitProductIn_Detail ao INNER JOIN Invp_product ain ON ain.UseProdTag = ao.FTagNumber WHERE ain.FTagNumber = a.FTagNumber ) ) GROUP BY b.Fcustid ), OutBackData AS ( SELECT SUM(a.fqty) AS fqty, ROUND(SUM(a.fprice*a.fqty),6) as fmoney, b.Fcustid AS FCustId FROM Invp_back a LEFT JOIN Invm_back b ON a.Fno = b.Fno WHERE b.Fdate >= @startDate AND b.Fdate <= @endDate AND ( EXISTS ( SELECT 1 FROM OUT_OutAssitProductIn_Detail WHERE FTagNumber = a.FTagNumber ) OR EXISTS ( SELECT 1 FROM OUT_OutAssitProductIn_Detail ao INNER JOIN Invp_product ain ON ain.UseProdTag = ao.FTagNumber WHERE ain.FTagNumber = a.FTagNumber ) ) GROUP BY b.Fcustid )"); strBuilder.Append(@" SELECT cust.fid as fcustid, cust.fname as fcustname, cust.fcname as fcustCname, fsales,fsl,TaxType , (isnull(ab.fqty,0)-isnull(ac.fqty,0)) as fqty,isnull(ab.fqty,0) as SendNum,isnull(ac.fqty,0) as BackNum ,(isnull(ROUND(ab.farea, cust.SendAreaRoundNum),0)-isnull(ROUND(ac.farea, cust.SendAreaRoundNum),0)) as farea ,(isnull(ab.fweigh,0)-isnull(ac.fweigh,0)) as fweight, isnull(ab.fshcs,0) as fshcs ,(isnull(ab.fmoney,0)-isnull(ac.fmoney,0)) as fmoney ,(isnull(abb.fmoney,0)-isnull(acc.fmoney,0)) as fmoneySummary ,isnull(ab.fmoney,0) as SendMoney ,isnull(abb.fmoney,0) as SendSummaryMoney ,isnull(ad.fqty,0) as InNum ,isnull(ad.fcostMoeny,0) as InFcostMoeny ,isnull(ad.farea,0) as InFarea ,isnull(ad.fpmoney,0) as InMoney ,isnull(ac.fmoney,0) as BackMoney ,isnull(acc.fmoney,0) as BackSummaryMoney ,hd.fname as DeptName ,isnull(prod.fshcs,0) as fOrderCount ,isnull(prod.fqty,0) as fOrderQty ,isnull(prod.fmoney,0) as fOrderMoney "); strBuilder.Append(@" ,isnull(outd.fmoney,0) as OutSendMoney ,isnull(outd.fqty,0) as OutSendFqty ,isnull(outb.fmoney,0) as OutBackMoney ,isnull(outb.fqty,0) as OutBackNums from bas_customer as cust left join sendData as ab on ab.fcustid=cust.fid left join sendSumData as abb on abb.fcustid=cust.fid left join sendBackData as ac on ac.fcustid=cust.fid left join sendBackSumData as acc on acc.fcustid=cust.fid
09-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值