首页 文章 精选 留言 我的

精选列表

搜索[Java],共10000篇文章
优秀的个人博客,低调大师

java学习笔记--io流的异常处理

public static void main(String[] args) { // readTest(); copyImage(); } // 拷贝图片 public static void copyImage() { FileInputStream fileInputStream = null; FileOutputStream fileOutputStream = null; try { // 找到目标文件 File inFile = new File("F:\\美女\\1.jpg"); File outFile = new File("E:\\1.jpg"); // 建立输入输出通道 fileInputStream = new FileInputStream(inFile); fileOutputStream = new FileOutputStream(outFile); // 建立缓冲数组,边读边写 byte[] buf = new byte[1024]; int length = 0; while ((length = fileInputStream.read(buf)) != -1) { fileOutputStream.write(buf, 0, length); } } catch (IOException e) { System.out.println("拷贝图片出错..."); throw new RuntimeException(e);//读取的错误 } finally { // 关闭资源 try { if (fileOutputStream != null) { fileOutputStream.close(); System.out.println("关闭输出流对象成功..."); } } catch (IOException e) {//关闭的错误 System.out.println("关闭输出流资源失败..."); throw new RuntimeException(e); } finally { if (fileInputStream != null) { try { fileInputStream.close(); System.out.println("关闭输入流对象成功..."); } catch (IOException e) { System.out.println("关闭输入流对象失败..."); throw new RuntimeException(e); } } } } } 总结:io流抛出异常的位置有两个 1. 打开的文件的时候 文件损坏或者硬盘损坏等 主要是 抛出包装后的错误信息RuntimeException(e) 2. 流关闭放在finally中 但是关闭流的时候也会发生错误 3. 如果io流抛出异常 就无法执行接下里的任务 所以我们要try{} catch(){}finally{}

优秀的个人博客,低调大师

JAVA实现编写平台代码生成器

[项目中经常写CRUD,但实际这些工作,我觉得如果有一个完整的代码规范,完全可以自动生成,加快开发效率. 代码生成器技术原理不复杂,一般就是写好一个模板生成一系列的代码而已。我看到mybatis_plus的代码生成器就相当不错,就自己拿过来改造了一下 1.项目中,需先引入vm库,用来生成代码 <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity</artifactId> <version>${velocity.version}</version> </dependency> 2.model的代码如下 /** * <p> * 考试题目表 * </p> * * @author starmark * @since 2018-04-12 */ @Data @EqualsAndHashCode(callSuper=false) public class Exam implements Serializable { /** * 主键 */ @TableId("id") private Long id; /** * 题目 */ @TableField("subject") private String subject; /** * 答案 */ @TableField("answer") private String answer; /** * 类别 */ @TableField("category") private String category; @TableField("key_point") private String keyPoint; @TableField("created_by") private String createdBy; @TableField("created_date") private Date createdDate; @TableField("last_updated_by") private String lastUpdatedBy; @TableField("last_updated_date") private Date lastUpdatedDate; } vm就改造成如下: package ${package.Entity}; #if(${activeRecord}) import lombok.Data; import lombok.EqualsAndHashCode; #end #foreach($pkg in ${table.importPackages}) import ${pkg}; #end /** * <p> * ${table.comment} * </p> * * @author ${author} * @since ${date} */ ${data} ${EqualsAndHashCode} #if(${table.convert}) @TableName("${table.name}") #end #if(${superEntityClass}) public class ${entity} extends ${superEntityClass}#if(${activeRecord})<${entity}>#end { #elseif(${activeRecord}) public class ${entity} implements Serializable { #else public class ${entity} implements Serializable { #end #foreach($field in ${table.fields}) #if(${field.keyFlag}) #set($keyPropertyName=${field.propertyName}) #end #if("$!field.comment" != "") /** * ${field.comment} */ #end #if(${field.convert}) #if(${field.keyFlag}) @TableId("${field.name}") #else @TableField("${field.name}") #end #end private ${field.propertyType} ${field.propertyName}; #end } 2.ServiceImpl实现类如下: import com.starmark.exam.entity.Exam; import com.starmark.exam.mapper.ExamMapper; import com.starmark.exam.service.IExamService; import com.starmark.core.base.AbstractBaseService; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; /** * <p> * 考试题目表 服务实现类 * </p> * * @author starmark * @since 2018-04-12 */ @Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public class ExamServiceImpl extends AbstractBaseService<ExamMapper, Exam> implements IExamService { } VM就写成如下: package ${package.ServiceImpl}; import ${package.Entity}.${entity}; import ${package.Mapper}.${table.mapperName}; import ${package.Service}.${table.serviceName}; import ${superServiceImplClassPackage}; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Propagation; /** * <p> * ${table.comment} 服务实现类 * </p> * * @author ${author} * @since ${date} */ @Service @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class) public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} { } 3.Controller实现类如下: import io.swagger.annotations.ApiOperation; import com.starmark.common.base.PageVo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.starmark.core.web.base.AbstractBaseController; /** * <p> * 考试题目表 前端控制器 * </p> * * @author starmark * @since 2018-04-12 */ @RestController @RequestMapping("/exam/exam") public class ExamController extends AbstractBaseController<IExamService> { @ApiOperation(value = "查询考试题目表列表") @PutMapping(value = "/page") public Object query(@RequestBody PageVo pageVo) { return super.queryPage(pageVo); } @ApiOperation(value = "新增考试题目表") @PostMapping public Object add(@RequestBody SysOrgDept param) { return super.add(param); } @ApiOperation(value = "考试题目表详情") @GetMapping(value = "/{id}") public Object get(@PathVariable("id") Long id) { return super.get(id); } @PutMapping @ApiOperation(value = "修改考试题目表") public Object update(@RequestBody Exam param) { return super.update(param); } @DeleteMapping(value = "/{id}") @ApiOperation(value = "删除考试题目表") public Object delete(@PathVariable("id") Long id) { return super.delete(id); } } VM就编写如下: package ${package.Controller}; import io.swagger.annotations.ApiOperation; import com.starmark.common.base.PageVo; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; #if(${superControllerClassPackage}) import ${superControllerClassPackage}; #end /** * <p> * ${table.comment} 前端控制器 * </p> * * @author ${author} * @since ${date} */ @RestController @RequestMapping("#if(${package.ModuleName})/${package.ModuleName}#end/${table.entityPath}") #if(${superControllerClass}) public class ${table.controllerName} extends ${superControllerClass}<${table.serviceName}> { #else public class ${table.controllerName} { #end @ApiOperation(value = "查询${table.comment}列表") @PutMapping(value = "/page") public Object query(@RequestBody PageVo pageVo) { return super.queryPage(pageVo); } @ApiOperation(value = "新增${table.comment}") @PostMapping public Object add(@RequestBody SysOrgDept param) { return super.add(param); } @ApiOperation(value = "${table.comment}详情") @GetMapping(value = "/{id}") public Object get(@PathVariable("id") Long id) { return super.get(id); } @PutMapping @ApiOperation(value = "修改${table.comment}") public Object update(@RequestBody ${entity} param) { return super.update(param); } @DeleteMapping(value = "/{id}") @ApiOperation(value = "删除${table.comment}") public Object delete(@PathVariable("id") Long id) { return super.delete(id); } } 以上就是各个类之间对应的模板。 代码就是通过读数据库的表信息,含字段名称,注解难来生成相关的文件。 现在我项目开发是通过代码生成器生成代码,然后中途加字段是通过我另一文章给mybatis添加自动建表,自动加字段的功能来加字段。 如果想要完整的代码生成器,请打赏一注彩票钱再联系我。

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册