- 博客(21)
- 收藏
- 关注
翻译 Demo: zip file system
public class Main { public static void main(String[] args) { String[] data = { "Line 1", "Line 2 2", "Line 3 3 3", "Line 4 4 4 4", "Line 5 5 5 5 5" }; try(FileSystem zipFs = openZip(Paths.get("myData.zip"))) {
2021-07-26 00:12:35 138
翻译 Using Default File System and Zip File Systems
Files are contained within a file systemHas a default file systemSpecialized file systems are supportedExample: Zip file systemPath instances are tied to a file systemPaths class works only for default
2021-07-11 20:46:36 116
翻译 Accessing Files with the java.nio.file package
java.nio.file preferred packages for filesjava.nio.FileXXX streams are deprecatedProvide a number of benefits over java.ioBetter exception reportingGreater scalabity - large file systemMore file system feature supportSimplifies common task常用clas
2021-07-11 15:11:40 287
翻译 File and Buffered Streams
Access FilesOften use streams for file-based I/OClass for each stream type in java.io packageFileReaderFileWriterFileInputStreamFileOutputStreamThe java.io classes are now deprecatedStill widely used in codeBuffered StreamsDirect file ac
2021-07-03 19:51:04 82
翻译 Chaining Streams
Streams are often chained togetherOne stream instance leverages anotherCreates higher-level functionalitySimplifies reusablitilyChain using constructorA good example is InputStreamReader:data comes in as binary format and process it as character dat
2021-06-27 17:14:09 92
翻译 Stream Errors and Cleanups
Error handling: Stream methods throw exceptions to indicate errorsClean up:Streams are backed by physical storage, eg. file system, network. cannot rely on standard Java resource recoveryStreams implement Closeable interface, you can call close method
2021-06-27 15:16:45 66
翻译 Common Stream Classes
以下展示了常用的一些流,每个流都需要做更深入的分析和解剖Input/OutputStream Derived ClassesReader/Writer Derived Classes
2021-06-14 23:43:58 122
翻译 Streams
Base stream to read from binary data:InputStream abstract class:int read() : read 一个byteint read(byte[ ] buff): read # of bytes that fit into the array, 这里的int返回值是表示读取到了多少个byteBase stream to read from text data:Reader abstract class:int read(): re
2021-06-14 22:50:22 68
原创 Checked and Unchecked Exceptions in Java
Checked exceptions: exceptions that are checked at compile time. 所以需要在方法上throws异常,或者用try-catch来处理异常,不然编译器会报错。Checked exceptions表示在正常work flow之外的错误。// throws keywordprivate static void checkedExceptionWithThrows() throws FileNotFoundException { File f
2021-03-28 22:18:14 91
翻译 [Effective Java] Item 61: Throws exceptions appropriate to the abstraction
当你写一个方法的时候,propagate一个由于invoke某个方法抛出的异常,然而这个异常对于使用你这个方法的client是不合理的。很简单的例子,一个网页,你按了某个键,读取用户信息,然而这个读取的方法propagate底下数据库的异常,这样对于用户你而言是没有意义的,你也会很困惑什么是数据库错误。而且,万一你需要改变这个方法的应用,你不从数据库读取数据,而从文件系统中读取,那么你还要在下次release中修改你方法中声明的异常。为了avoid这个问题,...
2021-03-27 23:57:55 86
翻译 [Effective Java] Item 60: Favor the use of standard exception
Commonly reused unchecked exceptions provided by Java platform libraries - 复用这些大家都知道的exceptions,好处就是大家都熟悉,容易理解,也知道这些异常所代表的用途。The most commonly reused exceptionsExceptionsOccasion for UseExampleIllegalArgumentExceptionNon-null parameter value
2021-03-21 00:46:09 74
翻译 [Effective Java] Item 59 Avoid unnecessary use of checked exceptions
Checked exceptions是要在合理使用API时无法避免异常,并且使用者也能采取有用的措施的情况下使用的。如果不满足这两个条件,而且使用者其实也没有什么更好的办法去处理的话,unchecked exception可能会更适合。我们来看CloneNotSupportedException,这个异常其实是因为,clone方法invoke了一个没有implement Cloneable这个Interface的object上而抛出的异常,这里其实有没有什么特别能处理的方法,然而API当时设计成了Che
2021-03-20 21:44:24 75
翻译 [Effective Java] Item 58 Use checked exceptions for recoverable conditions and runtime exceptions ..
Use checked exceptions for recoverable conditions and runtime exceptions for programming errors从种类上分:checked exceptions,runtime exceptions, errorsChecked exceptionsuse checked exceptions for recoverable conditions通常你可以使用try-catch来处理异常,或者在外层的方法上declare
2021-03-20 16:46:39 79
翻译 [Effective Java] Item 57: Use exceptions only for exceptional conditions
// Do not use this hideous code for iteration over a collectiontry{ Iterator<Foo> i = collection.iterator(); while(true){ Foo foo = i.next(); ... }} catch(NoSuchElementException e){}上面的代码,并不是为了处理一些exception的情况,只是因为collection没有元素的时候,next()
2021-02-23 23:54:07 84
翻译 [Effective Java] Item 25: Prefer lists to arrays
Arrays:covariant: if Sub is a subtype of Super, then Sub[ ] is a subtype of Super[ ]reified: arrays know and enforce their element types at runtimeGenerics:invariant: two distinct types Type1 and Type2, List<Type1> is neither a subtype nor a s
2021-02-19 21:52:42 117
翻译 [Effective Java] Item 28: Use bounded wildcards to increase API flexibility
因为parameterized type是invariant的,换句话说,也就是两个不同的类,type1和type2,无论type1和type2是什么关系,List<type1>既不是List<type2>的subtype也不是supertype。这样设计generic类别时会有局限性,还是拿Stack来举例:public class Stack<E> { public Stack(); public void push(E e); publi
2021-02-16 19:58:29 324
翻译 [Effective Java] Item 27: Favor generic methods
我们知道很多static utility方法是generic的,比如集合里面的binarySearch和sort。写generic方法很简单,只需要定义一个type parameter list,这个list是要放在方法的modifier和方法的返回值中间的,其余的跟普通方法一样。比如:// Generic methodpublic static <E> Set<E> union(Set<E> s1, Set<E> s2) { Set<E&
2021-02-06 21:17:52 57
翻译 [Effective Java] Item 26: Favor generic types
Generic类型的集合类不需要客户端代码进行cast操作,而且更安全,所以在设计集合类的时候,尽可能设计成generic类型。如何设计可以借鉴Stack这个类的设计// Object-based collection - a prime candidate for genericspublic class Stack { private Object[] elements; private int size = 0; private static final int DEF
2021-01-31 17:21:53 109
翻译 Wildcards in Java
Wildcards in JavaUnbounded wildcardsThe unbounded wildcard type是用通配符字母?来表示的,比如List<?>,我们把这个叫做一个未知类型的列表。通常有两种情况特别适用使用unbounded wildcard type:如果你写一个method,里面的实现是需要用到Object类的方法的当你的代码用到generic class的方法,但是这个方法不依赖type parameter例子:写一个方法打印出来列表中所有元素(并
2021-01-30 16:52:02 214 1
翻译 [Effective Java] Item 24:Eliminate unchecked warnings
什么是 unchecked warningUnchecked warning是由于compiler没有足够的type信息,无法判断某个操作是否type safety,所以给了warning。比如把一个raw type返回值的方法赋值给了一个generic type,compiler无法知道raw type集合里面的实例时什么,所以就不能肯定这个赋值的正确性,所以给与了警告,很有可能在runtime就发生了ClassCastException.所以每一个unchecked warning都表示可能在run
2020-11-22 00:22:30 155
翻译 [Effective Java] Item 23: Don’t use raw types in new code
[Effective Java] Item 23: Don’t use raw types in new code
2020-11-20 00:22:58 177
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人