欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

java基础第十四天_IO

发布时间:2025/3/20 37 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。