最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java-IO流實(shí)驗(yàn)

 更新時(shí)間:2021年06月30日 10:35:03   作者:小狐貍FM  
流是一種抽象概念,它代表了數(shù)據(jù)的無結(jié)構(gòu)化傳遞。。用來進(jìn)行輸入輸出操作的流就稱為IO流。換句話說,IO流就是以流的方式進(jìn)行輸入輸出,希望能給您帶來幫助

前言

項(xiàng)目結(jié)構(gòu)如下,在使用代碼的時(shí)候注意修改成你自己的包名和類名

在這里插入圖片描述

一、資源管理器

[1]. 題目

設(shè)計(jì)一個(gè)用于顯示指定目錄下所有文件與文件夾的資源管理器類,要求包括:

  • 從命令行輸入一個(gè)路徑,如果不是目錄,則輸出提示信息
  • 如果是目錄且存在,則顯示該目錄下,所有的文件與文件夾的名稱
  • 如果不存在,則輸出不存在該目錄

[2]. 實(shí)例

在這里插入圖片描述

在這里插入圖片描述

[3]. 代碼

package p1;
import java.util.*;
import java.io.*;
public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("請輸入路徑:");
		String path = scanner.next();
		File file = new File(path);
		if(file.isDirectory()) {//類型為目錄時(shí)
			if(!file.exists()) {//目錄不存在時(shí)
				System.out.println("該路徑不存在!");
			}else {
				System.out.println("該文件夾下的路徑如下:");
				String[] sub = file.list();//獲取文件夾下的所有文件、文件夾
				for(String i:sub) {//輸出
					System.out.println(i);
				}
			}
		}else {//不為目錄時(shí)
			System.out.println("該路徑不為目錄!");
		}
	}
}

二、文件復(fù)制與剪切

[1]. 題目

編寫一個(gè)文件操作類(FileOperation),具有復(fù)制和剪切兩個(gè)方法,要求:

  • 源路徑和目標(biāo)路徑由控制臺輸入
  • 使用靜態(tài)方法

[2]. 復(fù)制

在這里插入圖片描述 、

在這里插入圖片描述

在這里插入圖片描述

[3]. 剪切

在這里插入圖片描述

在這里插入圖片描述

在這里插入圖片描述

[4]. 代碼

package p2;
import java.util.*;
import java.io.*;
public class Main {
	public static void main(String[] args)  throws Exception{
		Scanner scanner = new Scanner(System.in);
		System.out.println("1. 復(fù)制");
		System.out.println("2. 剪切");
		System.out.println("請選擇: ");
		int choose = scanner.nextInt();
		System.out.println("源路徑: ");
		String resource = scanner.next();
		System.out.println("目標(biāo)路徑: ");
		String target = scanner.next();
		if(choose==1) {//復(fù)制
			FileOperation.copy(resource,target);
		}else {//剪切
			FileOperation.cut(resource, target);
		}
		System.out.println("執(zhí)行完畢");
	}
}
class FileOperation{//文件操作類
	public static void copy(String resource,String target)  throws Exception{//復(fù)制文件
		//文件流對象
		File file_resource = new File(resource);//源文件
		File file_target = new File(target);//目標(biāo)文件
		//文件輸入輸出流對象
		FileInputStream fis = new FileInputStream(file_resource);//輸入流,寫入數(shù)據(jù)
		FileOutputStream fos = new FileOutputStream(file_target);//輸出流,讀取數(shù)據(jù)
		//讀取文件數(shù)據(jù)
		byte[] buff_resource = new byte[(int) file_resource.length()];
		fis.read(buff_resource);//讀取文件數(shù)據(jù)
		//寫入文件數(shù)據(jù)
		String str = new String(buff_resource);
		byte[] buff_target = str.getBytes();
		fos.write(buff_target);//寫入文件數(shù)據(jù)
		//關(guān)閉文件流
		fis.close();
		fos.close();
	}
	public static void cut(String resource,String target)  throws Exception{//剪切文件
		//文件流對象
		//File file_delete = new File(resource);//源文件
		File file_resource = new File(resource);//源文件
		File file_target = new File(target);//目標(biāo)文件
		//文件輸入輸出流對象
		FileInputStream fis = new FileInputStream(file_resource);//輸入流,讀取數(shù)據(jù)
		FileOutputStream fos = new FileOutputStream(file_target);//輸出流,寫入數(shù)據(jù)
		//讀取文件數(shù)據(jù)
		byte[] buff_resource = new byte[(int) file_resource.length()];
		fis.read(buff_resource);//讀取文件數(shù)據(jù)
		//寫入文件數(shù)據(jù)
		String str = new String(buff_resource);
		byte[] buff_target = str.getBytes();
		fos.write(buff_target);//寫入文件數(shù)據(jù)
		//關(guān)閉文件流
		fis.close();
		fos.close();
		//刪除源文件,必須先關(guān)閉fis文件流后才能成功刪除文件
		file_resource.delete();
	}
}

三、文件數(shù)據(jù)讀寫

[1]. 題目

將“2018 FIFA World Cup will play in Russia.”寫入到D:\data.txt文件中,然后再從該文件中讀取所有內(nèi)容,并顯示在控制臺上。

[2]. 實(shí)例

在這里插入圖片描述

在這里插入圖片描述

[3]. 代碼

package p3;
import java.io.*;
public class Main {
	public static void main(String[] args) throws IOException{
		String str = "2018 FIFA World Cup will play in Russia.";
		Write("D:\\data.txt",str);
		System.out.println(Read("D:\\data.txt"));
	}
	public static String Read(String filename) throws IOException{//文件讀取
		File file = new File(filename);
		FileInputStream fis = new FileInputStream(file);//輸入流,讀取數(shù)據(jù)
		byte[] buff = new byte[(int) file.length()];
		fis.read(buff);//讀取文件
		String str = new String(buff);
		fis.close();
		return str;
	}
	public static void Write(String filename,String str) throws IOException{//文件寫入
		File file = new File(filename);
		FileOutputStream fos = new FileOutputStream(file);//輸出流,寫入數(shù)據(jù)
		byte[] buff = str.getBytes();
		fos.write(buff);//寫入文件
		fos.close();
	}
}

總結(jié)

本篇文章就到這里了,希望能給您帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

最新評論

邵阳县| 益阳市| 桑日县| 吉木乃县| 青神县| 宜丰县| 卢氏县| 崇信县| 海林市| 南平市| 阳城县| 南京市| 黔南| 驻马店市| 苍梧县| 宁陵县| 竹北市| 沂水县| 蒙山县| 尖扎县| 临湘市| 曲水县| 鸡东县| 丹巴县| 沙田区| 云林县| 平度市| 崇州市| 郑州市| 福清市| 武宁县| 平邑县| 修水县| 静乐县| 莱芜市| 化德县| 八宿县| 兴仁县| 收藏| 普宁市| 武清区|