java基础第十四天_IO
1.定義函數(shù),輸出一個(gè)byte的二進(jìn)制字符串。
2.定義工具類,完成int數(shù)和byte[]之間的相互轉(zhuǎn)換。
3.闡述IO流。
輸入輸出流
字符字節(jié)流
緩沖和非緩沖流
轉(zhuǎn)換流.
4.通過File對象打印輸出指定路徑下的整個(gè)目錄樹結(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)換流.
????輸入輸出流是相對jvm來說,字節(jié)字符流是以字節(jié)或者字符為單位,
????緩沖流是先把數(shù)據(jù)放到緩沖區(qū)然后達(dá)到一定要求再統(tǒng)一放到目標(biāo)
????轉(zhuǎn)換流是指將字節(jié)流與字符流之間的轉(zhuǎn)換,包含兩個(gè)類:InputStreamReader和OutputStreamWriter。
4.通過File對象打印輸出指定路徑下的整個(gè)目錄樹結(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();
// 取出上級目錄 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);
}
/**
?* 復(fù)制文件
?*/
public static void copyFile(String srcRoot, String path, String destDir) {
try {
// 準(zhǔn)備目錄
// 取出相對的路徑
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();
}
}
}
學(xué)習(xí)內(nèi)容:
java中IO和Byte與int間轉(zhuǎn)換
遇到問題:
目錄樹和文件復(fù)制不是很理解,程序應(yīng)該寫復(fù)雜了。
轉(zhuǎn)載于:https://blog.51cto.com/10718270/1785165
總結(jié)
以上是生活随笔為你收集整理的java基础第十四天_IO的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 850 USB 烧录模式
- 下一篇: Java中浮点数的基础知识