throws

学习Java第八周

风格不统一 提交于 2019-11-28 12:34:54
1.流的分类 1、字节流:Stream 2、字符流: Writer,Reader 输入流 :InputStream ,Reader 输出流 :OuputStream,Writer 字节流重要还是字符流重要:字节流:字节流类为处理字节式输入/输出提供了丰富的环境。 不仅仅可以读二进制,也可以读文本。 字符流:只能读文本。 流的分类: 字节输入流:InputStream 字节输出流:OuputStream 字符输入流:Reader 字符输出流:Writer 2.InputStream流 1、public abstract class InputStream 见到抽象,不能用它创建对象。 2、方法: public abstract int read() throws IOException public int read(byte b[]) throws IOException public int read(byte b[], int off, int len) throws IOException public int available() throws IOException //返回有效字节数。 面试的时候说:请说出available方法的作用:取得有效字节数。不调用read()方法的时候,文件指针指向第一个字符前,后面有文件大小个字符没有读。 public void

How can I throw an exception for the whole class instead of doing it method by method

北慕城南 提交于 2019-11-28 10:49:15
I'm writing a program in Java, and nearly every method in one of my classes is written like: public void doStuff() throws AWTException{} Is there a way for me to get rid of the extra step of typing throws AWTException for each method, and somehow do it for the entire class? Sorry, No. There is no way to do this in Java. if you throw exception with class level then how you identify that on which type of exception is thrown by method... or on which method it throw exception. so it is not allow in java...and also its not a good coding with java You can by throw exception at constructor level. You

缓冲流

醉酒当歌 提交于 2019-11-28 08:23:03
缓冲流 缓冲流:    读取数据大量的文件时 , 读取的速度慢, java 提供了一套缓冲流,提高 IO 流的效率。分为字节缓冲流和字符缓冲流。 ★字节缓冲流:      缓冲输出流:BufferedOutputStream  缓冲输入流:BufferesInputStream BufferedOutputStream: /* * 字节输出流的缓冲流 作用 提高效率 * 继承OutputStream * 构造方法 new BufferedOutputStream(OutputStream out); * * */ public class BufferedOutputStreamDemo { public static void main(String[] args) throws Exception { // 1创建字节输出流 绑定文件 //FileOutputStream fos =new FileOutputStream("c:\\buffer"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("c:\\buffer")); bos.write(65);//自动查码表 utf-8 0 1 //输出字符串 byte[] b= "hello world".getBytes();

WebServer Project-02-XML解析

北慕城南 提交于 2019-11-28 07:03:02
XML:Extensible Markup Language,可扩展标记语言,左卫门数据的一种存储格式或用于存储软件的参数,程序解析此配置文件,就可以达到不修改代码就能更改程序的目的。 <?xml version="1.0" encoding="UTF-8"?> <persons> <person> <name>至尊宝</name>> <age>9000</age> </person> <person> <name>白晶晶</name>> <age>2000</age> </person> </persons> package server.study; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; /** * 熟悉SAX解析流程 * Created by 16114 on 2019/8/22. *

SpringMVC-拦截器

大兔子大兔子 提交于 2019-11-28 05:36:26
SpringMVC拦截器 1.拦截器的作用   SpringMVC的处理器拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。   用户可以自定义一些拦截器来实现特定的功能。   拦截器链:拦截器链就是将拦截器按一定的顺序连接成一条链。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。    拦截器和过滤器的区别 :      过滤器 是Servlet规范中的一部分,任何java web工程都可以使用。      拦截器 是SpringMVC框架自己的,只有使用了SpringMVC框架的工程才可以使用。      过滤器 在url-pattern中配置了/*之后,可以对所有要访问的资源拦截。      拦截器 它只会拦截访问的控制器方法,如果访问的是jsp,html,css,image或者是js是不会进行拦截的。   它也是AOP思想的具体应用。   我们要想自定义拦截器,要求必须实现: HandlerInterceptor 接口。 2. 自定义拦截器的步骤   2.1 第一步:编写一个普通类实现HandlerInterception接口 package com.llb.interception; import org.springframework.web.servlet.HandlerInterceptor;

初生牛犊之spring(五)

夙愿已清 提交于 2019-11-28 04:06:21
spring拦截器 自定义的拦截器必须实现 org.springframework.web.servlet.HandlerInterceptor 接口 1.编写拦截器 实现HandlerInterceptor接口的各个实现类 package cn.HandlerInterceptor; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class AddHIA extends HandlerInterceptorAdapter{ /** * 整个请求处理后调用 * @param request * @param response * @throws Exception */ public void afterCompletion(HttpServletRequest request,HttpServletResponse response,Object object,Exception

When to use throws in a Java method declaration?

女生的网名这么多〃 提交于 2019-11-28 03:22:13
So I thought I had a good basic understanding of exception-handling in Java, but I was recently reading some code that gave me some confusion and doubts. My main doubt that I want to address here is when should a person use throws in a Java method declaration like the following: public void method() throws SomeException { // method body here } From reading some similar posts I gather that throws is used as a sort of declaration that SomeException could be thrown during the execution of the method. My confusion comes from some code that looked like this: public void method() throws IOException

Java实现Linux上传&下载文件

守給你的承諾、 提交于 2019-11-27 21:38:44
/** *实现服务器连接 * @param host * @param port * @param username * @param password * @param privateKeyFile * @return * @throws IOException */public static Connection getSSHConnection(String host,int port,String username,String password,String privateKeyFile) throws IOException { Connection connection=new Connection(host,port); connection.connect(); File file=new File(privateKeyFile); System.out.println(file); boolean b=connection.authenticateWithPublicKey(username,file,password); System.out.println(b); if (b){ return connection; }else { System.out.println("登录连接失败,请检查用户名、密码、私钥文件"); return null; }} /*

异常

孤街醉人 提交于 2019-11-27 19:17:40
一、异常概述 异常是指在程序的运行过程中所发生的不正常的事件,它会中断正在运行的程序 1、传统处理异常的方式(if else): [1]业务逻辑和处理异常的判断逻辑耦合在一起。[2]代码冗余 2、Java使用异常处理机制为程序提供异常处理的能力。异常处理机制完成后,程序可以继续执行 3、异常的常见方法:printStackTrace() 输出异常的堆栈信息,第一行:表示异常类型和异常描述(可为空),最后一行:包含异常的类、方法、具体代码行号           getMessage() 获取异常的描述           toString() 返回异常的类型和异常描述 4、try-catch:try{} 代码块用于执行可能存在异常的代码,catch{}代码块用于捕获并处理异常。   [1]正常执行,try→try-catch块后的代码段   [2]异常发生,try{} 代码块内某行代码产生异常,jvm创建一个具体异常对象ex,try{} 内不再向下执行,找到catch(Exception e){}捕获并处理异常     在异常捕获过程中需要进行异常类型匹配,若e=ex(多态)   [3]匹配异常类型不匹配,程序直接中断 5、try-catch-finally:try{} 执行可能有异常的代码,catch{}捕获并处理异常,finally{}用于回收资源(关闭文件、关闭数据库

OutputStream

谁说胖子不能爱 提交于 2019-11-27 16:11:06
OutputStream的基本方法 //向输出流中写入一个字节数据该字节数据为参数b的低8位 void write(int b) throws IOException //将一个字节类型的数组中的数据写入输出流 void write (byte 【】 b) throws IOException //将一个字节类型的数组中的数据从指定位置(off)开始的len个字节写入到输出流 void write(byte【】 b,int off,int len) throws IOException //关闭流释放内存资源 void close() throws IOException 将输出流中缓冲的数据全部写到目的地 void flush() throws IOException 注意:先写flush再写close 来源: https://www.cnblogs.com/lsswudi/p/11369722.html