日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java基础第十四天_IO

發(fā)布時(shí)間:2025/3/20 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java基础第十四天_IO 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1.定義函數(shù),輸出一個(gè)byte的二進(jìn)制字符串。

2.定義工具類,完成int數(shù)和byte[]之間的相互轉(zhuǎn)換。

3.闡述IO流。

輸入輸出流

字符字節(jié)流

緩沖和非緩沖流

轉(zhuǎn)換流.

4.通過(guò)File對(duì)象打印輸出指定路徑下的整個(gè)目錄樹(shù)結(jié)構(gòu)。

5.完成文件夾復(fù)制。


=========================================================================

定義函數(shù),輸出一個(gè)byte的二進(jìn)制字符串。

package com.it18zhang14;


public class ByteToBin {


/**

* @param args

*/

public static void main(String[] args) {

byte b=127;

byte b1=-1;

byte b2=-128;

printBinary(b);

printBinary(b1);

printBinary(b2);

}

public static void printBinary(byte b){

int num=b;

num|=256;

//System.out.println(num);

String str=Integer.toBinaryString(num);

//System.out.println(str);

int len=str.length();

//System.out.println(len);

System.out.println(str.substring(len-8, len));

System.out.println("-------------------------");

//System.out.println(Byte.toString(b));

//System.out.println(Byte.valueOf(b));

//System.out.println(str);

//System.out.println(Byte.);

//return str;

}

}

2.定義工具類,完成int數(shù)和byte[]之間的相互轉(zhuǎn)換。

package com.it18zhang14;


public class BinToByteUtils {


/**

* @param args

*/

public static void main(String[] args) {

int num=-5142;

byte[] bytes=integerCovertToByteArray(num);

for(int i=0;i<bytes.length;i++){

System.out.println("第"+i+"個(gè)元素是"+bytes[i]);

}

/*

int nums=(bytes[0]&0xff)<<8;

System.out.println(nums);

System.out.println(ByteArrayToInteger(bytes));

*/

//字節(jié)轉(zhuǎn)×××

System.out.println(ByteArrayToInteger(bytes));


}

public static byte[] integerCovertToByteArray(int num){

byte[] byteArray=new byte[4];

//獲取第一個(gè)byte 截取int整數(shù)二進(jìn)制最后八位

byte b0=(byte)num;

//獲取第二個(gè)byte

byte b1=(byte)(num>>8);

//獲取第三個(gè)byte

byte b2=(byte)(num>>16);

//獲取第四個(gè)byte

byte b3=(byte)(num>>24);

byteArray[0]=b0;

byteArray[1]=b1;

byteArray[2]=b2;

byteArray[3]=b3;

return byteArray;

}

public static int ByteArrayToInteger(byte[] bytes){

int num=0;

//相與防止移位出錯(cuò)

int num3=(bytes[3]&0xff)<<24;

int num2=(bytes[2]&0xff)<<16;

int num1=(bytes[1]&0xff)<<8;

int num0=(bytes[0]&0xff);

num=num3|num2|num1|num0;

return num;

}

}

3.闡述IO流。

輸入輸出流

字符字節(jié)流

緩沖和非緩沖流

轉(zhuǎn)換流.

????輸入輸出流是相對(duì)jvm來(lái)說(shuō),字節(jié)字符流是以字節(jié)或者字符為單位,

????緩沖流是先把數(shù)據(jù)放到緩沖區(qū)然后達(dá)到一定要求再統(tǒng)一放到目標(biāo)

????轉(zhuǎn)換流是指將字節(jié)流與字符流之間的轉(zhuǎn)換,包含兩個(gè)類:InputStreamReader和OutputStreamWriter。

4.通過(guò)File對(duì)象打印輸出指定路徑下的整個(gè)目錄樹(shù)結(jié)構(gòu)。

package com.it18zhang14;


import java.io.File;


public class TreeDemo1 {


/**

* @param args

*/

public static void main(String[] args)

? ? {

? ? ? ? File f = new File("d:/test");

? ? ? ? StringBuffer prefix = new StringBuffer("");

? ? ? ? System.out.println(f.getName());

? ? ? ? list(f, prefix);

? ? }

?

? ? public static void list(File path, StringBuffer prefix)

? ? {

? ? ? ? if (path != null && path.exists() && path.isDirectory())

? ? ? ? {

? ? ? ? ? ? prefix.append("\t");

? ? ? ? ? ? File[] childs = path.listFiles();

? ? ? ? ? ? if (childs != null && childs.length > 0)

? ? ? ? ? ? {

? ? ? ? ? ? ? ? for (File child : childs)

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? System.out.println(prefix + "|--" + child.getName());

? ? ? ? ? ? ? ? ? ? list(child, prefix);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? prefix.deleteCharAt(prefix.length()-1);

? ? ? ? }

? ? }



}

5.完成文件夾復(fù)制。

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;


public class DirCopy {


public static void main(String[] args) {

copyDir("d:/a", "d:/b");

}


/**

?* 復(fù)制文件夾

?*/

private static void copyDir(String srcRoot, String srcDir, String destDir) {

if (srcRoot == null) {

srcRoot = srcDir;

}

// 源文件夾

File srcFile = new File(srcDir);

// 目標(biāo)文件夾

File destFile = new File(destDir);


// 判斷srcFile有效性

if (srcFile == null || !srcFile.exists()) {

return;

}

// 創(chuàng)建目標(biāo)文件夾

if (!destFile.exists()) {

destFile.mkdirs();

}


// 判斷是否文件?

if (srcFile.isFile()) {

String absPath = srcFile.getAbsolutePath();

// 取出上級(jí)目錄 d:

String parentDir = new File(srcRoot).getParent();


// 取出相對(duì)的路徑

String relPath = absPath.substring(parentDir.length());

File destFile2 = new File(destDir, relPath);

// 拷貝文件

copyFile(srcRoot, srcFile.getAbsolutePath(), destDir);

}

// 目錄

else {

File[] children = srcFile.listFiles();

if (children != null) {

for (File f : children) {

copyDir(srcRoot, f.getAbsolutePath(), destDir);

}

}

}

}


public static void copyDir(String srcDir, String destDir) {

copyDir(srcDir, srcDir, destDir);

}


/**

?* 復(fù)制文件

?*/

public static void copyFile(String srcRoot, String path, String destDir) {

try {


// 準(zhǔn)備目錄

// 取出相對(duì)的路徑

String tmp = path.substring(srcRoot.length());

String folder = new File(destDir, tmp).getParentFile()

.getAbsolutePath();

System.out.println(folder);

File destFolder = new File(folder);

destFolder.mkdirs();


File f = new File(path);

// fis

FileInputStream fis = new FileInputStream(path);


String newDestpath = null;

// 文件輸出流

FileOutputStream fos = new FileOutputStream(new File(destFolder,

new File(path).getName()));


// 流的對(duì)拷貝

byte[] buf = new byte[1024];

int len = 0;

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

fos.write(buf, 0, len);

}

fis.close();

fos.close();

} catch (Exception e) {

e.printStackTrace();

}

}

}


學(xué)習(xí)內(nèi)容:

java中IO和Byte與int間轉(zhuǎn)換

遇到問(wèn)題:

目錄樹(shù)和文件復(fù)制不是很理解,程序應(yīng)該寫(xiě)復(fù)雜了。


轉(zhuǎn)載于:https://blog.51cto.com/10718270/1785165

總結(jié)

以上是生活随笔為你收集整理的java基础第十四天_IO的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄色三级网络 | 激情91视频| 国产精品成人一区二区 | 成人免费黄色大片 | 久久网亚洲 | 亚洲二区一区 | 欧美日韩亚洲天堂 | 欧美三日本三级少妇三级99观看视频 | 牲欲强的熟妇农村老妇女视频 | 日韩在线电影一区二区 | 天天操中文字幕 | aaaa黄色 | xxxx在线视频 | 黄色二级视频 | 色综合网站 | 奇米四色777| 性高潮在线观看 | 日韩中文字幕不卡 | 精品免费| 国产真人做爰毛片视频直播 | 日本做爰全过程免费看 | 高潮一区二区三区 | 四川话毛片少妇免费看 | 日本精品一区二区三区四区的功能 | 男人的天堂视频在线观看 | ww欧美| 在线观看黄av| 天堂影视av | 99这里只有 | 国产精品色哟哟 | 精品久久一 | 在线观看你懂的网站 | 成人久久久精品国产乱码一区二区 | 91视频地址 | 亚洲国产看片 | 黑丝一区二区三区 | 亚洲一区欧美一区 | 中文字幕欧美色图 | 亚洲美女视频在线 | 美女日批视频在线观看 | 五月婷婷社区 | 中日韩免费毛片 | 18久久 | 校园春色av | 在线观看一区 | 国产成人区 | 免费在线一区二区三区 | 久久免费黄色网址 | 亚洲福利电影网 | 日本高清有码视频 | 在线观看网站av | 欧美亚洲高清 | 黄色片18 | 成人福利片 | 亚洲成色www久久网站 | 国产美女网站视频 | 51精品 | 男裸体无遮挡网站 | 天堂视频免费在线观看 | 激情999 | 91亚洲精品在线 | 国产精品午夜视频 | 天天干天天操天天干 | 久久久香蕉 | 污污小视频| 免费观看成人鲁鲁鲁鲁鲁视频 | 欧美一级淫片免费视频魅影视频 | 久久久久久久黄色片 | 国产亚洲小视频 | 国产一区二区综合 | 福利视频在线导航 | 九热在线 | 黄色污污网站 | 欧美一区二区在线视频观看 | 中文字幕日韩精品一区 | 国产av日韩一区二区三区精品 | 熟妇女人妻丰满少妇中文字幕 | 精品午夜久久久 | 国产wwwwww | 一区二区激情视频 | 国产精品一区二区三区在线免费观看 | 欧美性猛交富婆 | 国产美女激情视频 | 91丨porny丨成人蝌蚪 | 日韩欧美亚洲一区二区三区 | 天堂网色 | 国产精品91一区二区 | 污漫网站 | 午夜精品视频一区 | 日韩一区久久 | 欧美黑人一区二区三区 | 97国产超碰 | 91久久精品国产 | 葵司av电影| 男生操男生网站 | 91精品婷婷国产综合久久蝌蚪 | 91精品国产91久久久久久黑人 | 伊人久久青草 | 女女百合高h喷汁呻吟玩具 www.亚洲一区 |