码迷,mamicode.com
首页 > 编程语言 > 详细

IT十八掌作业_java基础第十四天_IO

时间:2016-06-21 21:02:27      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:java基础   字符串   二进制   

1.定义函数,输出一个byte的二进制字符串。

2.定义工具类,完成int数和byte[]之间的相互转换。

3.阐述IO流。

    输入输出流

    字符字节流

    缓冲和非缓冲流

    转换流.

4.通过File对象打印输出指定路径下的整个目录树结构。

5.完成文件夹复制。


---------------------------------------------

1.

package com.it18zhang.day14;


public class ByteToBin {

    public static void main(String[] args) {

        byte b1 = -128;

        byte b2 = 127;

        byte b3 = 0;

        byte b4 = -1;

        byte b5 = 1;

        System.out.println("-128的二进制字符串" + printByteBinary(b1));

        System.out.println("127的二进制字符串" + printByteBinary(b2));

        System.out.println("0的二进制字符串" + printByteBinary(b3));

        System.out.println("-1的二进制字符串" + printByteBinary(b4));

        System.out.println("1的二进制字符串" + printByteBinary(b5));

    }




    /**

     * 输出一个byte的二进制字符串

     * 

     * @param b

     * @return binary string

     */

    public static String printByteBinary(byte b) {

        // 先将byte隐式转换成int数,

        int num = b;

        // 然后将int数与256进行位或运算,将前24位都换成1。

        num |= 256;

        // 通过Integer工具类的toBinaryString打印出int的二进制字符串。

        String str = Integer.toBinaryString(num);

        int len = str.length();

        // 截取字符串的最后8位字符

        return str.substring(len - 8, len);


    }


}




2.

package com.it18zhang;

/**

 *定义工具类,完成int到byte[]之间转换

 */

public class BinToByteUtils {


public static void main(String[] args) {

//int转换成字节数组

int num = -400;

byte[] bytes = intToByteArray(num);

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

System.out.println("index " + i +" = "+bytes[i]);

}

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

//字节数组转int

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

}

/**

* 将int整数转换成字节数组

*/

public static byte[] intToByteArray(int param){

//一个int四个字节,一个字节8位

byte[] byteArray = new byte[4];

byte b0 = (byte)param;

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

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

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

byteArray[0] = b0;

byteArray[1] = b1;

byteArray[2] = b2;

byteArray[3] = b3;

return byteArray;

}

/**

* 将字节数组转换成int整数

*/

public static int byteToIntArray(byte[] arr){

int num =0;

if(arr.length == 4){

//将最后一个字节左移24位,称为最高位

int num3 = arr[3] << 24;

//为了消除负数类型提升后前面多余的1,采用&0xFF保留有效8位

int num2 = (arr[2] & 0xFF) <<16;

int num1 = (arr[1] & 0xFF)<<8;

int num0 = (arr[0] & 0xFF) <<0;

num = num3 | num2 | num1 | num0;

}

return num;

}

}


3.阐述IO流。

    输入输出流

    字符字节流

    缓冲和非缓冲流

    转换流.

答:

流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象。即数据在两设备间的传输称为流,

IO流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作。 

按数据流向分为输入输出流,主要有InputStream和OutputStream两个类及其子类

按数据类型分为字符字节流,主要有Reader和Writer两个类及其子类

为了提高IO效率,引入了缓冲技术,通过缓冲流批量读写。而非缓冲流是通过一个一个字节或者字符进行读写。

转换流是指字节流和字符流之间的转换,主要包括InputStreamReader和OutputStreamWriter两个类。



4.


package com.it18zhang;


import java.io.File;


/**

 * 通过File对象打印输出指定路径下的整个目录树结构。

 * 

 */

public class TreeDirectory {


    // 文件所在的层数

    private int fileLevel;




    public static void main(String[] args) {

        TreeDirectory rd = new TreeDirectory();

        String dirPath = "C:/Program Files";

        rd.printDir(dirPath);

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

        rd.readFile(dirPath);

    }




    /**

     * 生成输出格式

     * 

     * @param name

     *            输出的文件名或目录名

     * @param level

     *            输出的文件名或者目录名所在的层次

     * @return 输出的字符串

     */


    public String createPrintStr(String name, int level) {

        // 输出的前缀

        String printStr = "";

        // 按层次进行缩进

        for (int i = 0; i < level; i++) {

            printStr = printStr + "   ";

        }

        printStr = printStr + "--" + name;

        return printStr;

    }




    /**

     * 输出初始给定的目录

     * 

     * @param dirPath

     *            给定的目录

     */

    public void printDir(String dirPath) {

        // 将给定的目录进行分割

        String[] dirNameList = dirPath.split("\\\\");

        // 设定文件level的base

        fileLevel = dirNameList.length;

        // 按格式输出

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

            System.out.println(createPrintStr(dirNameList[i], i));

        }

    }




    /**

     * 输出给定目录下的文件,包括子目录中的文件

     * 

     * @param dirPath

     *            给定的目录

     */

    public void readFile(String dirPath) {

        // 建立当前目录中文件的File对象

        File file = new File(dirPath);

        // 取得代表目录中所有文件的File对象数组

        File[] list = file.listFiles();

        // 遍历file数组

        if (list != null) {

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

                if (list[i].isDirectory()) {

                    System.out.println(createPrintStr(list[i].getName(), fileLevel));

                    fileLevel++;

                    // 递归子目录

                    readFile(list[i].getPath());

                    fileLevel--;

                }

            }

        }

    }

}

5.

package com.it18zhang;



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


// 取出相对的路径a

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

}

}

}


本文出自 “菜鸟成就数据之路” 博客,转载请与作者联系!

IT十八掌作业_java基础第十四天_IO

标签:java基础   字符串   二进制   

原文地址:http://liubx.blog.51cto.com/11235064/1791412

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!