日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

java基础第十四天_IO

發布時間:2025/3/20 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java基础第十四天_IO 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.定義函數,輸出一個byte的二進制字符串。

2.定義工具類,完成int數和byte[]之間的相互轉換。

3.闡述IO流。

輸入輸出流

字符字節流

緩沖和非緩沖流

轉換流.

4.通過File對象打印輸出指定路徑下的整個目錄樹結構。

5.完成文件夾復制。


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

定義函數,輸出一個byte的二進制字符串。

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數和byte[]之間的相互轉換。

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+"個元素是"+bytes[i]);

}

/*

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

System.out.println(nums);

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

*/

//字節轉×××

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


}

public static byte[] integerCovertToByteArray(int num){

byte[] byteArray=new byte[4];

//獲取第一個byte 截取int整數二進制最后八位

byte b0=(byte)num;

//獲取第二個byte

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

//獲取第三個byte

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

//獲取第四個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;

//相與防止移位出錯

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流。

輸入輸出流

字符字節流

緩沖和非緩沖流

轉換流.

????輸入輸出流是相對jvm來說,字節字符流是以字節或者字符為單位,

????緩沖流是先把數據放到緩沖區然后達到一定要求再統一放到目標

????轉換流是指將字節流與字符流之間的轉換,包含兩個類:InputStreamReader和OutputStreamWriter。

4.通過File對象打印輸出指定路徑下的整個目錄樹結構。

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.完成文件夾復制。

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");

}


/**

?* 復制文件夾

?*/

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

if (srcRoot == null) {

srcRoot = srcDir;

}

// 源文件夾

File srcFile = new File(srcDir);

// 目標文件夾

File destFile = new File(destDir);


// 判斷srcFile有效性

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

return;

}

// 創建目標文件夾

if (!destFile.exists()) {

destFile.mkdirs();

}


// 判斷是否文件?

if (srcFile.isFile()) {

String absPath = srcFile.getAbsolutePath();

// 取出上級目錄 d:

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


// 取出相對的路徑

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);

}


/**

?* 復制文件

?*/

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

try {


// 準備目錄

// 取出相對的路徑

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()));


// 流的對拷貝

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();

}

}

}


學習內容:

java中IO和Byte與int間轉換

遇到問題:

目錄樹和文件復制不是很理解,程序應該寫復雜了。


轉載于:https://blog.51cto.com/10718270/1785165

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。