MATLAB將陣列轉化為帶行列名字的表格並且寫入
阿新 • • 發佈:2021-01-01
JDK7新特性
1.二進位制字面量
JDK7開始,終於可以用二進位制來表示整數(byte,short,int和long)。
使用二進位制字面量的好處是,可以使程式碼更容易被理解。語法非常簡單,只要在二進位制數值前面加 0b或者0B例如:int x = 0b110110
2.數字字面量
為了增強對數值的閱讀性,如我們經常把資料用逗號分隔一樣。JDK7提供了_對資料分隔。舉例:int x = 100_1000;
注意事項:
- 不能出現在進位制標識和數值之間
- 不能出現在數值開頭和結尾
- 不能出現在小數點旁邊
switch中可以使用字串了
switch ("" ){
case "":
break;
try-with-resources
操作的類****只要是實現了AutoCloseable介面就可以在try語句塊退出的時候自動呼叫close方法關閉流資源****。
- 原來
InputStream is = null;
OutputStream os = null;
try {
// 流
is = xxx;
os = xxx;
//use
is.read();
os.write();
} finally {
// close
if (stream != null){
try{
is.close();
}catch(Exeception e){
//xxx
}
try{
os.close();
}catch(Exeception e){
//xxx
}
}
}
- 現在
try ( InputStream is = new FileInputStream("xx");
OutputStream os = new FileOutputStream("xx")
) {
//xxx
//不用關閉了,JVM幫你關閉流
}
常用於流物件,連線池等的自動關閉。
如果自己的類,實現AutoCloseable,實現其close方法,即可使用。
NIO2檔案 Files
讀取寫入
public final class Files {}
//讀取位元組
byte[] dataBytes = Files.readAllBytes(Paths.get("D:\\xxx"));
//readline
List<String> lines = Files.readAllLines(Paths.get("D:\\xxx"));
lines.forEach(v-> System.out.println(v));
寫入方法
//寫入檔案
Files.write(Paths.get("D:\\xxx"), "xxx".getBytes());
//指定模式寫入,比如追加
Files.write(Paths.get("D:\\xxx"), "xxx".getBytes(), StandardOpenOption.APPEND);
/****預設UTF-8編碼,可以手工指定******/
構造流模式
InputStream is = Files.newInputStream(path);
OutputStream os = Files.newOutputStream(path);
Reader reader = Files.newBufferedReader(path);
Writer writer = Files.newBufferedWriter(path);
操作目錄
//判斷存在
Files.exists(path);
//
Files.createFile(path);
//
Files.createDirectory(path);
//檔案列表,可以流運算遍歷
Stream<Path> list(Path dir)
//檔案列表
Stream<Path> walk(Path start, FileVisitOption... options)
Files.copy(in, path);
Files.move(path, path);
Files.delete(path);
//建立臨時檔案、臨時目錄:
Files.createTempFile(dir, prefix, suffix);
Files.createTempFile(prefix, suffix);
Files.createTempDirectory(dir, prefix);
Files.createTempDirectory(prefix);
Path物件
Path path = Paths.get("xxx", "name");
Path path = Paths.get("xxx/name");
Path path = Paths.get(URI.create("xxx路徑"));
Path path = new File("xxx").toPath();
Path path = FileSystems.getDefault().getPath("xxx");
//Path、URI、File之間的轉換
File file = new File("xxx");
Path path = file.toPath();
File file = p1.toFile();
URI uri = file.toURI();
多異常統一處理
try {
//xxx
} catch (AException e) {
e.printStackTrace();
} catch (BException e) {
e.printStackTrace();
}
try {
//xxx
} catch (AException | BException e) {
e.printStackTrace();
}
反應推導
List<String> list = new ArrayList<>();