最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

4月3日IO流

2023-04-04 08:32 作者:搜糞海  | 我要投稿


刪除文件夾

? ? public static void main(String[] args) {

? ? ? ? // new File("testIO").delete();

? ? ? ? deleteDir(new File("testIO"));

? ? }


? ? public static void deleteDir(File dir){

? ? ? ? if(dir.isDirectory()){

? ? ? ? ? ? File[] listFiles = dir.listFiles();

? ? ? ? ? ? for (File sub : listFiles) {

? ? ? ? ? ? ? ? deleteDir(sub);

? ? ? ? ? ? }

? ? ? ? }


? ? ? ? dir.delete();

? ? }


復(fù)制文件夾

public static void main(String[] args) throws IOException {

? ? ? ? File src = new File("D:/testIO");

? ? ? ? File dest = new File("testIO");

? ? ? ? copyDir(src, dest);

? ? }

? ? public static void copyDir(File src, File dest) throws IOException{

? ? ? ? if(dest.isFile()){

? ? ? ? ? ? throw new RuntimeException(dest +"不是文件夾");

? ? ? ? }

? ? ? ? if(src.isFile()){

? ? ? ? ? ? File destFile = new File(dest.getPath()+"/" + src.getName());

? ? ? ? ? ? copyFile(src, destFile);

? ? ? ? }else if(src.isDirectory()){

? ? ? ? ? ? File destFile = new File(dest.getPath()+"/" + src.getName());

? ? ? ? ? ? destFile.mkdir();


? ? ? ? ? ? File[] listFiles = src.listFiles();

? ? ? ? ? ? for (File sub : listFiles) {

? ? ? ? ? ? ? ? copyDir(sub,destFile);

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? public static void copyFile(File srcFile, File destFile) throws IOException {

? ? ? ? FileInputStream fis = new FileInputStream(srcFile);

? ? ? ? FileOutputStream fos = new FileOutputStream(destFile);

? ? ? ? byte[] data = new byte[1024];

? ? ? ? int len;

? ? ? ? while((len = fis.read(data)) !=-1){

? ? ? ? ? ? fos.write(data, 0, len);

? ? ? ? }

? ? ? ? fis.close();

? ? ? ? fos.close();

? ? }


遞歸遍歷文件夾

? ? public static void main(String[] args) {

? ? ? ? File file = new File("C:/zzk/zzk_code");

? ? ? ? ArrayList<String> all = listAllSubs(file);

? ? ? ? for (String string : all) {

? ? ? ? ? ? System.out.println(string);

? ? ? ? }

? ? }

? ? public static ArrayList<String> listAllSubs(File file) {

? ? ? ? ArrayList<String> list = new ArrayList<>();

? ? ? ? if (file.isFile()) {

? ? ? ? ? ? if (file.getName().endsWith(".java")) {

? ? ? ? ? ? ? ? list.add(file.getPath());

? ? ? ? ? ? }

? ? ? ? } else if (file.isDirectory()) {

? ? ? ? ? ? File[] listFiles = file.listFiles();

? ? ? ? ? ? for (File sub : listFiles) {

? ? ? ? ? ? ? ? list.addAll(listAllSubs(sub));

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return list;

? ? }


java_cmd 和 file 做 IO

? ? ? ? Scanner input = new Scanner(System.in);

? ? ? ? PrintStream ps = new PrintStream("words.txt");

? ? ? ? for (int i = 0; i < 3; i++) {

? ? ? ? ? ? System.out.println("請輸入第" + (i + 1) + "句要對柴老師說的話:");

? ? ? ? ? ? String content = input.nextLine();

? ? ? ? ? ? ps.println(content);

? ? ? ? }

? ? ? ? input.close();

? ? ? ? ps.close();

? ? ? ? Scanner input2 = new Scanner(new File("words.txt"));

? ? ? ? while (input2.hasNextLine()) {

? ? ? ? ? ? System.out.println(input2.nextLine());

? ? ? ? }

? ? ? ? input2.close();


把對象存入 ROM

? ? public static void main(String[] args) throws IOException, ClassNotFoundException {

? ? ? ? test01();

? ? ? ? test02();

? ? }

? ? public static void test01() throws? IOException{

? ? ? ? Message msg = new Message("柴老師", "佟老師", "加工資", System.currentTimeMillis());

? ? ? ? ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("message.dat"));

? ? ? ? oos.writeObject(msg);

? ? ? ? oos.close();

? ? }

? ? public static void test02() throws? IOException, ClassNotFoundException{

? ? ? ? ObjectInputStream ois = new ObjectInputStream(new FileInputStream("message.dat"));

? ? ? ? Object msg = ois.readObject();

? ? ? ? System.out.println(msg);

? ? ? ? ois.close();

? ? }


數(shù)據(jù)在 RAM 和 ROM 之間 IO

? ? ? ? int a = 10;

? ? ? ? char c = 'a';

? ? ? ? double d = 2.5;

? ? ? ? boolean b = true;

? ? ? ? String str = "尚硅谷";

? ? ? ? // output 把程序中的變量 write 到 ROM

? ? ? ? DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.dat"));

? ? ? ? dos.writeInt(a);

? ? ? ? dos.writeChar(c);

? ? ? ? dos.writeDouble(d);

? ? ? ? dos.writeBoolean(b);

? ? ? ? dos.writeUTF(str);

? ? ? ? dos.close();

? ? ? ? // input 從 file 中 read 數(shù)據(jù)到 RAM

? ? ? ? DataInputStream dis = new DataInputStream(new FileInputStream("data.dat"));

? ? ? ? System.out.println(dis.readInt());

? ? ? ? System.out.println(dis.readChar());

? ? ? ? System.out.println(dis.readDouble());

? ? ? ? System.out.println(dis.readBoolean());

? ? ? ? System.out.println(dis.readUTF());

? ? ? ? dis.close();


復(fù)制時修改字符編碼

? ? ? ? try(

? ? ? ? ? ? ? ? FileInputStream fis = new FileInputStream("D:/testIO/我想對你說.txt");

? ? ? ? ? ? ? ? BufferedInputStream bis = new BufferedInputStream(fis);

? ? ? ? ? ? ? ? InputStreamReader isr = new InputStreamReader(bis,"GBK");

? ? ? ? ? ? ? ? // 分三層的IO

? ? ? ? ? ? ? ? FileOutputStream fos = new FileOutputStream("testIO/柴老師的話.txt");

? ? ? ? ? ? ? ? BufferedOutputStream bos = new BufferedOutputStream(fos);

? ? ? ? ? ? ? ? OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF-8");

? ? ? ? ){

? ? ? ? ? ? char[] data = new char[1024];

? ? ? ? ? ? int len;

? ? ? ? ? ? while((len = isr.read(data))!=-1){

? ? ? ? ? ? ? ? osw.write(data, 0, len);

? ? ? ? ? ? }

? ? ? ? }catch(IOException e){

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }


復(fù)制一個文件

? ? ? ? try (

? ? ? ? ? ? ? ? BufferedInputStream bis = new BufferedInputStream(

? ? ? ? ? ? ? ? ? ? ? ? new FileInputStream("D:/testIO/test.txt")); // 源文件

? ? ? ? ? ? ? ? BufferedOutputStream bos = new BufferedOutputStream(

? ? ? ? ? ? ? ? ? ? ? ? new FileOutputStream("testIO/test.txt")); // 目標(biāo)文件

? ? ? ? ) {

? ? ? ? ? ? byte[] data = new byte[1024];

? ? ? ? ? ? int len;

? ? ? ? ? ? // 分批讀入

? ? ? ? ? ? while ((len = bis.read(data)) != -1) {

? ? ? ? ? ? ? ? bos.write(data, 0, len); // 最后一批長度不足1024

? ? ? ? ? ? }

? ? ? ? } catch (IOException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }


刪除某個文件或文件夾都是delete

? ? ? ? File file4 = new File("d:/testIO/a.txt");

? ? ? ? file4.delete();


判斷是不是一個文件或文件夾

? ? ? ? File file3 = new File("d:/testIO");

? ? ? ? if (file3.isFile()) {

? ? ? ? ? ? System.out.println(file3 + "是一個文件。");

? ? ? ? } else if (file3.isDirectory()) {

? ? ? ? ? ? System.out.println(file3 + "是一個文件夾");

? ? ? ? }


獲取文件屬性

? ? ? ? System.out.println("文件名:" + file2.getName());

? ? ? ? System.out.println("文件大?。?#34; + file2.length());

? ? ? ? System.out.println("文件的絕對路徑:" + file2.getAbsolutePath());

? ? ? ? System.out.println("文件的父目錄:" + file2.getParent());


新建 file 時,考慮異常

? ? ? ? try {

? ? ? ? ? ? file.createNewFile();

? ? ? ? } catch (IOException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }


throws IOException


文件是否存在

if (file.exists()) {

}


新建一個文件夾或文件:

? ? ? ? File dir = new File("D:/testIO"); // 用 String 指定絕對路徑

? ? ? ? dir.mkdir(); // make directory 開一個沒后綴的文件夾目錄

? ? ? ? File file = new File("D:/testIO/test.txt");

? ? ? ? file.createNewFile(); // 開一個有后綴的新文件

? ? ? ? dir = new File("testIO"); // 用 String 指定相對路徑

? ? ? ? dir.mkdir();

? ? ? ? file = new File("testIO/test.txt");

? ? ? ? file.createNewFile();


IO流:

dir file

input output reader writer stream

四個頂級抽象父類 {字節(jié)流IO, 字符流IO}


4月3日

昨晚 war3 到12點(diǎn),今天精神還可以

周三去醫(yī)院抽血

猶豫 Redmi G i7 3050 + Redmi手機(jī)


4月3日IO流的評論 (共 條)

分享到微博請遵守國家法律
舞钢市| 股票| 安远县| 萝北县| 微山县| 明水县| 枣强县| 遂宁市| 文昌市| 山阴县| 柯坪县| 尉氏县| 东阿县| 南昌县| 论坛| 海宁市| 虹口区| 镇原县| 金坛市| 杨浦区| 图木舒克市| 长顺县| 岑溪市| 贞丰县| 宝山区| 新和县| 和田市| 崇阳县| 临泉县| 昌江| 商南县| 浪卡子县| 普兰县| 五家渠市| 新郑市| 乐清市| 凤凰县| 乐平市| 镇沅| 惠东县| 凤山市|