首页 > 编程语言 > 详细

Java--I/O输入输出流

时间:2017-02-16 11:31:20      阅读:141      评论:0      收藏:0      [点我收藏+]

1、编码问题

package com.imooc_io;
public class CodeTest {
    public static void main(String[] args) throws Exception{       
        String s = "慕课ABC";
        byte[] bytes1 = s.getBytes();//转换成字节序列用的是项目默认的编码
        for(byte b:bytes1){
            //把字节转换成了int以16进制的方式显示
            System.out.print(Integer.toHexString(b & 0xff)+" ");
        }
        System.out.println();
        //gbk编码中文占用两个字节,英文占用一个字节
        
        byte[] bytes2 = s.getBytes("utf-8");
        //utf-8编码中文占用3个字节,英文占用一个字节
        for(byte b:bytes2){
            System.out.print(Integer.toHexString(b & 0xff)+" ");
        }
        System.out.println();
        //java是双字节编码utf-16be
        byte[] bytes3 = s.getBytes("utf-16be");
        //utf-16be编码中文占用2个字节,英文占用2个字节
        for(byte b:bytes3){
            System.out.print(Integer.toHexString(b & 0xff)+" ");
        }
        System.out.println();
        String str1 = new String(bytes3);//乱码
        String str2 = new String(bytes3,"utf-16be");
        System.out.println(str1);
        System.out.println(str2);
    }

}

2、File类

用于表示文件或目录的信息,不能用于文件内容的访问

package com.imooc_io;
import java.io.File;
import java.io.IOException;
public class FileDemo {
    public static void main(String[] args) {
        File file = new File("D:\\imooc\\jsp");
        System.out.println(file.exists());
        if(!file.exists()){
            file.mkdir();
        }else{
            file.delete();
        }
        System.out.println(file.isDirectory());
        System.out.println(file.isFile());
        
        File file2 = new File("D:\\imooc\\1.txt");
        //File file2 = new File("D:\\imooc","1.txt");
        if(!file2.exists()){
            try {
                file2.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            file2.delete();
        }
        //常用的File对象方法
        System.out.println(file.getAbsolutePath());
        System.out.println(file.getName());
        System.out.println(file.getParent());
        System.out.println(file.getParentFile().getAbsolutePath());
        
    }

}

 遍历目录:

package com.imooc_io;
import java.io.*;
public class FileUtils {
    /**
     * 列出指定目录下(包括其子目录)的所有文件
     * @throws IllegalAccessException 
     */
    public static void listDirectory(File dir)throws IOException, IllegalAccessException{
        if(!dir.exists()){
            throw new IllegalAccessException("目录:"+dir+"不存在");
        }if(!dir.isDirectory()){
            throw new IllegalAccessException(dir+"不是目录");
        }
        //直接子的名称,不包含子目录下的文件
        /**
        String[] filenames = dir.list();
        for(String string : filenames){
            System.out.println(dir+"\\"+string);
        }
         */
        //如果要遍历子目录下的内容就需要构造File对象做递归操作
        File[] files = dir.listFiles();//返回的是直接子目录(文件)的抽象
//        for(File file:files){
//            System.out.println(file);
//        }
        if(files!=null&&files.length>0){
            for(File file:files){
                if(file.isDirectory()){
                    listDirectory(file);
                }else{
                    System.out.println(file);
                }
            }
        }
    }
}

3、RandomAcessFile

Java提供的对文件内容的访问,既可以读文件也可以写文件

支持随机访问文件,可以访问文件的任意位置

 

Java--I/O输入输出流

原文:http://www.cnblogs.com/Nyan-Workflow-FC/p/6404488.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!