标签:des style class blog code java
Java7语法新特性:
前言,这是大部分的特性,但还有一些没有写进去,比如多核 并行计算的支持加强 fork join 框架;这方面并没有真正写过和了解。也就不写进来了。
public String generate(String name, String gender) {
String title = "";
switch (gender) {
case "男":
title = name + " 先生";
break;
case "女":
title = name + " 女士";
break;
default:
title = name;
}
return title;
public void read(String filename) throws BaseException {
FileInputStream input = null;
IOException readException = null;
try {
input = new FileInputStream(filename);
} catch (IOException ex) {
readException = ex; //保存原始异常
} finally {
if (input != null) {
try {
input.close();
} catch (IOException ex) {
if (readException == null) {
readException = ex;
}
}
}
if (readException != null) {
throw new BaseException(readException);
}
}
}
public void read(String filename) throws IOException {
FileInputStream input = null;
IOException readException = null;
try {
input = new FileInputStream(filename);
} catch (IOException ex) {
readException = ex;
} finally {
if (input != null) {
try {
input.close();
} catch (IOException ex) {
if (readException != null) { //此处的区别
readException.addSuppressed(ex);
}
else {
readException = ex;
}
}
}
if (readException != null) {
throw readException;
}
}
}
public void testSequence() {
try {
Integer.parseInt("Hello");
}
catch (NumberFormatException | RuntimeException e) { //使用‘|‘分割,多个类型,一个对象e
}
}
public void read(String filename) throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
StringBuilder builder = new StringBuilder();
String line = null;
while((line=reader.readLine())!=null){
builder.append(line);
builder.append(String.format("%n"));
}
return builder.toString();
}
}
public void copyFile(String fromPath, String toPath) throws IOException { try ( InputStream input = new FileInputStream(fromPath); OutputStream output = new FileOutputStream(toPath) ) { byte[] buffer = new byte[8192]; int len = -1; while( (len=input.read(buffer))!=-1 ) { output.write(buffer, 0, len); } } }
public int sum(int... args) {
int result = 0;
for (int value : args) {
result += value;
}
return result;
}
对collections的支持
Java代码
现在你还可以:
2.自动资源管理
原来:
BufferedReader br = new BufferedReader(new FileReader(path));
try {
return br.readLine();
} finally {
br.close();
}
现在:
3.对通用实例创建(diamond)的type引用进行了改进
原来:
Map<String, List<String>> anagrams = new HashMap<String, List<String>>();
现在:
Map<String, List<String>> anagrams = new HashMap<>();
4.数值可加下划线
6.二进制文字
int binary = 0b1001_1001;
异常catch可以一次处理完而不像以前一层层的surround;
标签:des style class blog code java
原文地址:http://blog.csdn.net/hjm4702192/article/details/30078877