六、標準輸入、輸出流
public static void main(String[] args) {
BufferedReader br = null;
try {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while (true) {
System.out.println("請輸入字符串:");
String data = br.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
System.out.println("程序結束");
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
七、打印流
@Test
public void test2() {
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("D:\\IO\\text.txt"));
// 創建打印輸出流,設置爲自動刷新模式(寫入換行符或字節 '\n' 時都會刷新輸出緩衝區)
ps = new PrintStream(fos, true);
if (ps != null) {// 把標準輸出流(控制檯輸出)改成文件
System.setOut(ps);
}
for (int i = 0; i <= 255; i++) { // 輸出ASCII字符
System.out.print((char) i);
if (i % 50 == 0) { // 每50個數據一行
System.out.println(); // 換行
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (ps != null) {
ps.close();
}
}
}
八、數據流
@Test
public void test3() throws IOException {
//1.
DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt"));
//2.
dos.writeUTF("劉建辰");
dos.flush();//刷新操作,將內存中的數據寫入文件
dos.writeInt(23);
dos.flush();
dos.writeBoolean(true);
dos.flush();
//3.
dos.close();
}
九、對象流
十、隨機存取文件流
十一、NIO.2中Path、Paths、Files類的使用
public class PathTest {
//如何使用Paths實例化Path
@Test
public void test1() {
Path path1 = Paths.get("d:\\nio\\hello.txt");//new File(String filepath)
Path path2 = Paths.get("d:\\", "nio\\hello.txt");//new File(String parent,String filename);
System.out.println(path1);
System.out.println(path2);
Path path3 = Paths.get("d:\\", "nio");
System.out.println(path3);
}
//Path中的常用方法
@Test
public void test2() {
Path path1 = Paths.get("d:\\", "nio\\nio1\\nio2\\hello.txt");
Path path2 = Paths.get("hello.txt");
// String toString() : 返回調用 Path 對象的字符串表示形式
System.out.println(path1);
// boolean startsWith(String path) : 判斷是否以 path 路徑開始
System.out.println(path1.startsWith("d:\\nio"));
// boolean endsWith(String path) : 判斷是否以 path 路徑結束
System.out.println(path1.endsWith("hello.txt"));
// boolean isAbsolute() : 判斷是否是絕對路徑
System.out.println(path1.isAbsolute() + "~");
System.out.println(path2.isAbsolute() + "~");
// Path getParent() :返回Path對象包含整個路徑,不包含 Path 對象指定的文件路徑
System.out.println(path1.getParent());
System.out.println(path2.getParent());
// Path getRoot() :返回調用 Path 對象的根路徑
System.out.println(path1.getRoot());
System.out.println(path2.getRoot());
// Path getFileName() : 返回與調用 Path 對象關聯的文件名
System.out.println(path1.getFileName() + "~");
System.out.println(path2.getFileName() + "~");
// int getNameCount() : 返回Path 根目錄後面元素的數量
// Path getName(int idx) : 返回指定索引位置 idx 的路徑名稱
for (int i = 0; i < path1.getNameCount(); i++) {
System.out.println(path1.getName(i) + "*****");
}
// Path toAbsolutePath() : 作爲絕對路徑返回調用 Path 對象
System.out.println(path1.toAbsolutePath());
System.out.println(path2.toAbsolutePath());
// Path resolve(Path p) :合併兩個路徑,返回合併後的路徑對應的Path對象
Path path3 = Paths.get("d:\\", "nio");
Path path4 = Paths.get("nioo\\hi.txt");
path3 = path3.resolve(path4);
System.out.println(path3);
// File toFile(): 將Path轉化爲File類的對象
File file = path1.toFile();//Path--->File的轉換
Path newPath = file.toPath();//File--->Path的轉換
}
}
以上就是關於javajava中的IO流的介紹,如果有不當之處或者遇到什麼問題,歡迎在文章下面留言~
如果你想了解更多關於Java的內容,可以查看:Java學習之路
轉載請註明:https://blog.csdn.net/weixin_44662961/article/details/106111107