throws

javaIO——概述

心已入冬 提交于 2019-11-27 03:33:07
  以前看java书,都将IO作为一个大的章节甚至模块来编写,可见IO在java语言中的重要性。   java的流按功能和处理的目标数据不同可以分为字节流和字符流。字符流处理的基本数据单元是字符;字节流处理的基本数据单元是字节。类关系结构图如下:            图片转自: https://blog.csdn.net/weixin_44411569/article/details/88788085      IO本身就是输入输出的意思,从上图可以看出,无论字符还是字节流,都是分为输入和输出两大块。   并且不管是输入还是输出流、字符流还是字节流,都实现了接口 java.io.Closeable ,该接口只有一个 close 方法。官方文档是这样说的:A {@code Closeable} is a source or destination of data that can be closed.The close method is invoked to release resources that the object is holding (such as open files).意思是说: 一个Closeable对象是一个可以关闭的数据源或者数据目标地,可以通过调用close方法来释放该对象所占用的资源(比如打开的文件) 。显然,所谓数据源就是对应输入流

转换流

自古美人都是妖i 提交于 2019-11-27 01:33:46
转换流 我们要读取的文件aa.txt采用的是GBK的编码方式。 ​ FileReader这个流是采用Idea默认的编码方式进行读取(UTF-8)。 ​ 两种编码不一致,那么就引发了乱码问题。 ​ 如果想要读取GBK编码的文件,我们可以指定一个编码方式去读取。 ​ 使用转换流,可以指定编码进行读取。 public class Demo01FileReader { public static void main ( String [ ] args ) throws IOException { //创建FileReader对象 Reader r = new FileReader ( "d:\\aa.txt" ) ; //开始读取,一次读取一个字符 int i ; while ( ( i = r . read ( ) ) != - 1 ) { //输出 System . out . print ( ( char ) i ) ; } //关流 r . close ( ) ; } } InputStreamReader InputStreamReader是转换流, 可以指定编码读取数据, 可以将文件中的数据读取到Java程序中。 InputStreamReader的构造方法: InputStreamReader(InputStream in):参数要传递字节输入流。

Is there a way to make Runnable's run() throw an exception?

拟墨画扇 提交于 2019-11-27 00:16:06
问题 A method I am calling in run() in a class that implements Runnable) is designed to be throwing an exception. But the Java compiler won't let me do that and suggests that I surround it with try/catch. The problem is that by surrounding it with a try/catch I make that particular run() useless. I do want to throw that exception. If I specify throws for run() itself, the compiler complains that Exception is not compatible with throws clause in Runnable.run() . Ordinarily I'm totally fine with not

TCP粘包和拆包

一世执手 提交于 2019-11-26 23:38:46
TCP(transport control protocol,传输控制协议) 是面向连接的,面向流的,提供高可靠性服务。收发两端(客户端和服务器端)都要有一一成对的socket,因此,发送端为了将多个发往接收端的包,更有效的发到对方,使用了优化方法(Nagle算法),将多次间隔较小且数据量小的数据,合并成一个大的数据块,然后进行封包。这样,接收端,就难于分辨出来了,必须提供科学的拆包机制。即面向流的通信是无消息保护边界的。 图解TCP的粘包和拆包 假设客户端分别发送了两个数据包D1和D2给服务端,由于服务端一次读取到字节数是不确定的,故可能存在以下四种情况: 1.服务端分两次读取到了两个独立的数据包,分别是D1和D2,没有粘包和拆包 2.服务端一次接受到了两个数据包,D1和D2粘合在一起,称之为TCP粘包 3.服务端分两次读取到了数据包,第一次读取到了完整的D1包和D2包的部分内容,第二次读取到了D2包的剩余内容,这称之为TCP拆包 4.服务端分两次读取到了数据包,第一次读取到了D1包的部分内容D1_1,第二次读取到了D1包的剩余部分内容D1_2和完整的D2包。 特别要注意的是,如果TCP的接受滑窗非常小,而数据包D1和D2比较大,很有可能会发生第五种情况,即服务端分多次才能将D1和D2包完全接受,期间发生多次拆包。 粘包、拆包问题的解决方案:定义通信协议 目前业界主流的协议

RedisTemplate实例

淺唱寂寞╮ 提交于 2019-11-26 22:37:16
@Component public class RedisUtils { /** * 日志 */ private static final Logger LOGGER = LoggerFactory.getLogger(RedisUtils.class); @Autowired private RedisTemplate redisTemplate; /** * <写入缓存> * * @param key key * @param value value * @return 写入是否成功 * @throws */ public boolean set(String key, Object value) { boolean result = false; try { ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue(); operations.set(key, value); result = true; } catch (Exception e) { LOGGER.error("", e); } return result; } /** * <写入缓存设置时效时间> * * @param key key * @param value value * @param

Throws or try-catch

送分小仙女□ 提交于 2019-11-26 21:29:50
What is the general rule of thumb when deciding whether to add a throws clause to a method or using a try-catch ? From what I've read myself, the throws should be used when the caller has broken their end of the contract (passed object) and the try-catch should be used when an exception takes place during an operation that is being carried out inside the method. Is this correct? If so, what should be done on the callers side? P.S: Searched through Google and SO but would like a clear answer on this one. Bozho catch an exception only if you can handle it in a meaningful way declare throwing the

Error message “unreported exception java.io.IOException; must be caught or declared to be thrown”

我与影子孤独终老i 提交于 2019-11-26 17:49:42
Error: filecontent.java:15: unreported exception java.io.IOException; must be caught or declared to be thrown showfile(); ^ filecontent.java:78: unreported exception java.io.IOException; must be caught or declared to be thrown showfile(); ^ I have already thrown java.io.IOException, but still it shows these errors. My code: import java.awt.*; import java.awt.event.*; import java.io.*; class filecontent extends Frame implements ActionListener { TextField t[] = new TextField[4]; TextArea ta[] = new TextArea[4]; Button submit; Panel p1; filecontent() { setGUI(); setRegister(); showfile();

小白之旅13-1

社会主义新天地 提交于 2019-11-26 17:49:08
一. 异常 1.1 概念 Java程序在运行时期发生的问题就是异常。 在Java中,把异常封装成了一个类。 当发生了某些问题时,系统会自动创建对应的异常对象并抛出该异常相关的信息。 1.2 异常的体系 Throwable Error:用于指示合理的应用程序不应该试图捕获的严重问题 Exception:它指出了合理的应用程序想要捕获的条件 RuntimeException IndexOutOfBoundsException ... IOException FileNotFoundException ... ... 1.3 异常的处理 1.3.1 捕获 1.3.1.1 格式 try{ ​ // 需要被检测的代码 } catch(异常 对象){ ​ //发生异常后才会执行,且是立即执行 ​ // 处理异常的代码 } finally{ ​ // 一定会被执行的代码 ​ // 一般用于释放资源 } 1.3.1.2 注意 1、如果有多个异常可能发生,需要将这些异常罗列在try代码块之后,用于进行针对性的异常处理 2、如果多个异常之间有包含关系,那么范围大的异常必须写在范围小的异常之后 3、只有使用对应的异常对象才能进行相应异常的捕获,否则是无效 1.3.2 抛出 关键字:throw、throws throws:用于方法声明,声明该方法的方法体中可能存在异常,需要调用者进行处理 格式: ​

Difference between throw and throws in Java? [duplicate]

ⅰ亾dé卋堺 提交于 2019-11-26 17:45:18
问题 This question already has answers here : Exception handling : throw, throws and Throwable (8 answers) Closed 5 years ago . Can any one clearly state the difference between throw and throws in Java exception handling with an example? I have tried googling but couldn't arrive at a conclusion. Pls help 回答1: throws clause is used to declare an exception and throw keyword is used to throw an exception explicitly. If we see syntax wise then throw is followed by an instance variable and throws is

Java 面向对象(十三)

旧巷老猫 提交于 2019-11-26 16:58:01
异常 什么是异常 异常 :指的是程序在执行过程中,出现的非正常的情况,最终会导致JVM的非正常停止。 在Java等面向对象的编程语言中,异常本身是一个类,产生异常就是创建异常对象并抛出了一个异常对象。Java处理异常的方式是中断处理。 异常指的并不是语法错误,语法错了,编译不通过,不会产生字节码文件,根本不能运行。 异常体系 异常机制其实是帮助我们 找到 程序中的问题,异常的根类是 java.lang.Throwable ,其下有两个子类: java.lang.Error 与 java.lang.Exception ,平常所说的异常指 java.lang.Exception 。 Throwable体系: Error :严重错误Error,无法通过处理的错误,只能事先避免,好比绝症。 Exception :表示异常,异常产生后程序员可以通过代码的方式纠正,使程序继续运行,是必须要处理的。好比感冒。 Throwable中的常用方法: public void printStackTrace() :打印异常的详细信息。 包含了异常的类型,异常的原因,还包括异常出现的位置,在开发和调试阶段,都得使用printStackTrace。 public String getMessage() :获取发生异常的原因。 提示给用户的时候,就提示错误原因。 public String toString()