JAVA学习之路---io.File 文件操作(txt类型)

1.二进制读取文件内容(内容只有字母)  单个字节读取

public static void main(String[] args) throws Exception{

        String file = "C:/Users/r/Desktop/222.txt";    //创建一个String 类对象,值为要被访问的文件路径
        File file1 = null;     //创建一个File类型对象
        FileInputStream tmp = null;    //创建一个FileInputStream类型对象
        file1 = new File(file);    //给file1赋值
        tmp = new FileInputStream(file);    //给tmp赋值
        int num;    //创建一个int类型对象 
        num = tmp.read();    // 使用FileInputStream类中的read方法,读取文件一个字节的内容,返回值为二进制,int类型,赋值给之前 创建好的int类型对象 num
        System.out.println(num);    //输出num    当文件中数据读取完毕后,输出结果为 -1

    };


2.二进制读取文件内容(内容只有字母)  多个字节读取 (利用FileInputStream 中 read方法在文件无值时返回-1 的概念 + while循环实现需求)

public static void main(String[] args) throws Exception{

String file = "C:/Users/r/Desktop/222.txt";    //创建一个String 类对象,值为要被访问的文件路径
        File file1 = null;     //创建一个File类型对象
        FileInputStream tmp = null;    //创建一个FileInputStream类型对象
        file1 = new File(file);    //给file1赋值
        tmp = new FileInputStream(file);    //给tmp赋值

        int num;    //创建一个int类型对象 

        //利用while循环,判断num != -1

        while ((num = tmp.read()) != -1){        

            System.out.println(num);

        }

}


3.把读取出的二进制int类型 result  强转为char类型并输出

public static void main(String[] args) throws Exception{

String file = "C:/Users/r/Desktop/222.txt";    //创建一个String 类对象,值为要被访问的文件路径
        File file1 = null;     //创建一个File类型对象
        FileInputStream tmp = null;    //创建一个FileInputStream类型对象
        file1 = new File(file);    //给file1赋值
        tmp = new FileInputStream(file);    //给tmp赋值

        int num;    //创建一个int类型对象 

        //利用while循环,判断num != -1

        while ((num = tmp.read()) != -1){        

            System.out.println(( char)num);

        }

}


4.利用byte[] 数组,设置每次字节读取个数,并将读取后的结果储存在byte[]数组中,使用String args = new String(byte) 将byte数组变为String类型,最后输出。

public static void main(String[] args) throws Exception{

String file = "C:/Users/r/Desktop/222.txt";    //创建一个String 类对象,值为要被访问的文件路径
        File file1 = null;     //创建一个File类型对象
        FileInputStream tmp = null;    //创建一个FileInputStream类型对象
        file1 = new File(file);    //给file1赋值
        tmp = new FileInputStream(file);    //给tmp赋值

        int num;    //创建一个int类型对象 

        byte[] bytes = new byte[3];    //创建一个byte数组,并设置数组大小为3,也就是一次读取三个字节

        int num = tmp.read(bytes);    //num的值为实际读取到的字节数(当剩余字节数小于3时,num值也会发生变化)

        System.out.println(new String(bytes));    //利用String中的String(byte[]) 方法输出byte数组

注:如果当文件末尾剩余字节数小于 设置的 次 读取值时(也就是  byte[] bytes = new byte[3] 中的3时)输出结果中会有空格(下面代码中会解决此问题)


5. 以行为单位,输入文件中的字符数据,第一种方法:先用FileInputStream中的read方法 单个的读取字节,然后新建一个byte类型数组,将读取出的字节储存在数组中,利用String(byte,offect,length)将数组输出

public static void main(String[] args) throws Exception{
        String file = "C:/Users/r/Desktop/222.txt";    //创建一个String 类对象,值为要被访问的文件路径
        File file1 = null;    //创建一个File类型对象
        FileInputStream tmp = null;    //创建一个FileInputStream类型对象
        file1 = new File(file);    //给file1赋值
        tmp = new FileInputStream(file);    //给tmp赋值
        byte[] bytes = new byte[1024];   //创建一个byte数组,并设置数组大小为1024,也就是要储存字节的容器(注:要比读取文件字节数大,否则报数组下角标越界错误
        int num;    //创建一个int类型对象 
        int i = 0;    //创建一个int类型对象 

        while ((num = tmp.read()) != -1){
            if (num == 13){     //判断是否为换行\r\n   在assII 表中,换行符为 13;
                break;
            }
            bytes[i] = (byte) num;    //将读取到的num值 强转为byte类型,并添加到byte数组中
            i++;     //角标自增

        }

    System.out.println(new String(bytes,0,i));    //利用String(byte,offect,length)输出数组,此方法可避免输出内容中有空格

    }

注:如出现如下报错信息,请检查byte[ args ] 中的参数 args 是否小于文件总字节数,属于数组下标越界异常改大args参数即可

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2

at Study07_os.test03.main(test03.java:19)


6.以行为单位,输入文件中的字符数据,第二种方法:利用 new BufferedReader(new InputStreamReader(new FileInputStream(file))) 中的readLine方法,已行为单位进行文件读取

public static void main(String[] args) throws Exception{


        String file = "C:/Users/r/Desktop/222.txt";    //创建一个String 类对象,值为要被访问的文件路径
        File file1 = null;    //创建一个File类型对象
        FileInputStream tmp = null;    //创建一个FileInputStream类型对象

        BufferedReader reader = null;     //创建一个BufferedReader类型对象

        file1 = new File(file);    //给file1赋值
        tmp = new FileInputStream(file);    //给tmp赋值
        reader = new BufferedReader(new InputStreamReader(tmp));    //给reader赋值
        String str = reader.readLine();    //因为 BufferedReader 中 readLine方法返回值为String类型,所以用一个String类型对象来接收
        System.out.println(str);    //输出接收到的参数

    }

加入while 可循环输出文件所有内容:

public static void main(String[] args) throws Exception {


        String file = "C:/Users/r/Desktop/222.txt";    //创建一个String 类对象,值为要被访问的文件路径
        File file1 = null;    //创建一个File类型对象
        FileInputStream tmp = null;    //创建一个FileInputStream类型对象
        BufferedReader reader = null;     //创建一个BufferedReader类型对象
        file1 = new File(file);   //给file1赋值
        tmp = new FileInputStream(file);    //给tmp赋值
        reader = new BufferedReader(new InputStreamReader(tmp));    //给reader赋值
        String str = null;
        while ((str = reader.readLine())!=null){   //当无内容时,BufferedReader中readLine方法会 返回一个null值 
            System.out.println();    //输出接收到的参数
        }

    }


本人为初学小白,这是自己一点浅显的理解,不喜勿喷,哈哈!

    



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值