日韩性视频-久久久蜜桃-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ò),歡迎將生活随笔推薦給好友。