Java基础———重温关于的文件读写操作,File类,FileInputStream类等的使用

从文件读取文件,或者写入文件非常重要,因为这会很方便文件管理和程序编写结合为一体

  1. 文件的创建与判断

    • 分析:如果文件存在,则输出file has existed. 如果文件不存在,则创建它
package File_all;
import java.io.*;

import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
/* 思路介绍:
 1. 1.首先我想建一个文件,这个文件如果存在我就直接读取数据,如果不存在,则进行创建
 2.   
 3.   
 4.   
 */
class File1{

    public static void main(String[] Args){
        File f = new File("f:\\finding_job\\Github\\File_input_out\\src\\File_all\\abc.txt");

        if(f.exists()){
            System.out.println("this file has existed!");
        }
        else {
            try{
            f.createNewFile();
            System.out.println("this file doesn't exist,but it has been created");
            }catch(IOException e){

                System.out.println("创建文件异常");
            }

        }
    }
    }

输出结果:this file has existed!


  1. 文件用FileInputStream的读取与写入
package File_all;
import java.io.*;

import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
/* 思路介绍:
 * 1.首先我想建一个文件,这个文件如果存在我就直接读取数据,如果不存在,则进行创建
 * 2.如果文件不是空文件,则读取其中数据,之后,将文件清空;并将“I Love Java”写入,并输出提示“I have written appointed data”;
 *   如果文件开始就是空文件,直接将“I Love Java”写入,并读出;  
 */
class File1{

    public static void main(String[] Args){
        File f = new File("f:\\finding_job\\Github\\File_input_out\\src\\File_all\\abc.txt");

        if(f.exists()){
            System.out.println("this file has existed!");
        }
        else {
            try{
            f.createNewFile();
            System.out.println("this file doesn't exist,but it has been created");
            }catch(IOException e){

                System.out.println("创建文件异常");
            }
        }
        //这里开始将开始实现第二部分文件读写功能
                if(f.length()==0 ){
                    System.out.println("this file is empty!");
                    try{
                    //write data, use FileOutStream

                    FileOutputStream fos = new FileOutputStream(f);  //只能一个字节一个字节写入

                    fos.write('A'); //这个地方只能英文,因为只有英语字母可以用一个字节来表示,其他汉字都会出现数据丢失
                    System.out.println("the file has been writteen");
                    fos.close();
                    }catch(Exception e){

                        System.out.println("the file write not correctly");

                    }

                }
                else{System.out.println("the file's length is: "+f.length());} 
    }
    }

这段代码只能一个字母一个字母敲进去,显然不是我们想要的,接下来介绍,DataOutputStream类,这个类是可以完整敲入,int,String等数据。

以下代码:

package File_all;
import java.io.*;

/* 思路介绍:
 * 1.首先我想建一个文件,这个文件如果存在我就直接读取数据,如果不存在,则进行创建
 * 2.如果文件不是空文件,则读取其中数据,之后,将文件清空;并将“I Love Java”写入,并输出提示“I have written appointed data”;
 *   如果文件开始就是空文件,直接将“I Love Java”写入,并读出;
 *   
 *   
 */
class File1{

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

        File f = new File("f:\\finding_job\\Github\\File_input_out\\src\\File_all\\abc1.txt");

        if(f.exists()){
            System.out.println("this file has existed!");
        }
        else {
            try{
            f.createNewFile();
            System.out.println("this file doesn't exist,but it has been created");
            }catch(IOException e){

                System.out.println("创建文件异常");
            }
        }
        //这里开始将开始实现第二部分文件读写功能
                if(f.length()==0 ){
                    System.out.println("this file is empty!");
                    try{
                    //write data, use FileOutStream

                    FileOutputStream fos = new FileOutputStream(f,false);  //只能一个字节一个字节写入,替换掉原来的东西
                    //定义DataOutputStream类
                    DataOutputStream dos = new DataOutputStream(fos); //这样是为了将数据传入流中,相当于数据流的入口

                    dos.writeUTF("I don't Love Java"); 
                    System.out.println("the file has been writteen");
                    fos.close();
                    dos.close();
                    }catch(Exception e){

                        System.out.println("the file write not correctly");

                    }

                } 
                else{
                    System.out.println("the file's length is: "+f.length()+"byte");
                    try{
                        //clear data

                        FileOutputStream fos = new FileOutputStream(f,false);  //只能一个字节一个字节写入,替换掉原来的东西
                        //定义DataOutputStream类
                        DataOutputStream dos = new DataOutputStream(fos); //这样是为了将数据传入流中,相当于数据流的入口

                        dos.write(new String("").getBytes()); //z这里就是为了清空文档
                        System.out.println("the file has been cleared,it is an empty file now");
                        fos.close();
                        dos.close();
                        }catch(Exception e){

                            System.out.println("the file clear not correctly");

                        }

                    try{
                        //write data, use FileOutputStream

                        FileOutputStream fos = new FileOutputStream(f,false);  //只能一个字节一个字节写入,替换掉原来的东西
                        //定义DataOutputStream类
                        DataOutputStream dos = new DataOutputStream(fos); //这样是为了将数据传入流中,相当于数据流的入口

                        dos.writeUTF("I Love Java"); //这个地方读入字符串
                        System.out.println("the file has been writteen");

                        dos.close();
                        }catch(Exception e){

                            System.out.println("the file write not correctly");

                        }
                    try{  
                    //read data, use FileInputStream


                    FileInputStream fis = new FileInputStream(f);  //只能一个字节一个字节读
                    //定义DataOutputStream类
                    DataInputStream dis = new DataInputStream(fis); //这样是为了将数据传入流中,相当于数据流的入口

                    System.out.println("the file's content is: "+dis.readUTF());

                    fis.close();
                    dis.close();

                        }catch(IOException e){
                            System.out.println("the file can't read correctly");
                        }

                }           
    }
    }

输出结果:
第一次运行结果:
this file has existed!
the file’s length is: 13byte
the file has been cleared,it is an empty file now
the file has been writteen
the file’s content is: I Love Java


以上都是以字节为单位进行处理数据,包括DataInputStream类这种可以输入字符串的方式,那么引入以字符为单位的输入和输出流非常有必要。

package File_all;
import java.io.*;

/* 思路介绍:
 * 1.首先我想建一个文件,这个文件如果存在我就直接读取数据,如果不存在,则进行创建
 * 2.如果文件不是空文件,则读取其中数据,之后,将文件清空;并将“I Love Java”写入,并输出提示“I have written appointed data”;
 *   如果文件开始就是空文件,直接将“I Love Java”写入,并读出;
 *   
 *   
 */
class File1{

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

        File f = new File("f:\\finding_job\\Github\\File_input_out\\src\\File_all\\abc.txt");

        if(f.exists()){
            System.out.println("this file has existed!");
        }
        else {
            try{
            f.createNewFile();
            System.out.println("this file doesn't exist,but it has been created");
            }catch(IOException e){

                System.out.println("创建文件异常");
            }
        }
        //这里开始将开始实现第二部分文件读写功能
                if(f.length()==0 ){
                    System.out.println("this file is empty!");
                    try{
                    //write data, use FileOutStream

                    FileOutputStream fos = new FileOutputStream(f,false);  //只能一个字节一个字节写入,替换掉原来的东西
                    //定义DataOutputStream类
                    DataOutputStream dos = new DataOutputStream(fos); //这样是为了将数据传入流中,相当于数据流的入口

                    dos.writeUTF("I don't Love Java"); 
                    System.out.println("the file has been writteen");
                    fos.close();
                    dos.close();
                    }catch(Exception e){

                        System.out.println("the file write not correctly");

                    }

                } 
                else{
                    System.out.println("the file's length is: "+f.length()+"byte");
                    try{
                        //clear data

                        FileOutputStream fos = new FileOutputStream(f,false);  //只能一个字节一个字节写入,替换掉原来的东西
                        //定义DataOutputStream类
                        DataOutputStream dos = new DataOutputStream(fos); //这样是为了将数据传入流中,相当于数据流的入口

                        dos.write(new String("").getBytes()); //z这里就是为了清空文档
                        System.out.println("the file has been cleared,it is an empty file now");
                        fos.close();
                        dos.close();
                        }catch(Exception e){

                            System.out.println("the file clear not correctly");

                        }

                    try{
                        //write data, use FileOutputStream

                        FileOutputStream fos = new FileOutputStream(f,false);  //只能一个字节一个字节写入,替换掉原来的东西
                        //定义DataOutputStream类
                        DataOutputStream dos = new DataOutputStream(fos); //这样是为了将数据传入流中,相当于数据流的入口

                        dos.writeUTF("I Love Java"); //这个地方读入字符串
                        System.out.println("the file has been writteen");

                        dos.close();
                        }catch(Exception e){

                            System.out.println("the file write not correctly");

                        }
                    try{  
                    //read data, use FileInputStream


                    FileInputStream fis = new FileInputStream(f);  //只能一个字节一个字节读
                    //定义DataOutputStream类
                    DataInputStream dis = new DataInputStream(fis); //这样是为了将数据传入流中,相当于数据流的入口

                    System.out.println("the file's content is: "+dis.readUTF());

                    fis.close();
                    dis.close();

                        }catch(IOException e){
                            System.out.println("the file can't read correctly");
                        }

                }
        System.out.println("****************the DataInputStream has ended,the BufferedWriter and BufferedReader start now***********");
        try{  
            //read data, use FileInputStream


            FileOutputStream fos = new FileOutputStream(f);  //只能一个字节一个字节读
            //定义DataOutputStream类
            OutputStreamWriter osw = new OutputStreamWriter(fos); //这个是针对字符流,可以一个个输入字符
            BufferedWriter bw = new BufferedWriter(osw); //这样是为了将数据传入流中,相当于数据流的入口,但是可以输入字符串,并用行来读出
            System.out.println("please input the content that you want to show");

            InputStreamReader isr = new InputStreamReader(System.in); //这样是为了将数据传入流中,相当于数据流的入口
            BufferedReader br = new BufferedReader(isr);
            String temp = new String(br.readLine());

            bw.write(temp);
            bw.newLine();
            System.out.println("the content from your keyboard has been written correctly");
            bw.close();

                }catch(IOException e){
                    System.out.println("the file has not written correctly");
                }
        try{  
            //read data, use FileInputStream


            FileInputStream fis = new FileInputStream(f);  //只能一个字节一个字节读
            //定义IutputStreamReader类
            InputStreamReader isr = new InputStreamReader(fis); //这样是为了将数据传入流中,相当于数据流的入口
            BufferedReader br = new BufferedReader(isr);
            System.out.println("the file's content is: ");
            //输出结果
            String s;
            while((s= br.readLine())!=null){
                System.out.println("I AM IN loop");
            System.out.println(s);
            }

            br.close();

                }catch(IOException e){
                    System.out.println("the file can't read correctly");
                }   

    }
    }

输出结果:
this file has existed!
this file is empty!
the file has been writteen
***********the DataInputStream has ended,the BufferedWriter and BufferedReader start now******
please input the content that you want to show
it’s your last chance
the content from your keyboard has been written correctly
the file’s content is:
I AM IN loop
it’s your last chance

最后介绍一下完整的从键盘输入数据到文件中,并且读出

功能描述:从键盘不限行的输入数据,并且保存到文件中,同时读出

package File_all;
import java.io.*;

/* 思路介绍:
 * 1.首先我想建一个文件,这个文件如果存在我就直接读取数据,如果不存在,则进行创建
 * 2.如果文件不是空文件,则读取其中数据,之后,将文件清空;并将“I Love Java”写入,并输出提示“I have written appointed data”;
 *   如果文件开始就是空文件,直接将“I Love Java”写入,并读出;
 *  3.从键盘上不限行读入,如果遇到一个新的行“ok”,则读入完毕;并将这个内容写入到文件中,读出;
 *   
 */
class File1{

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

        File f = new File("f:\\finding_job\\Github\\File_input_out\\src\\File_all\\abc.txt");

        if(f.exists()){
            System.out.println("this file has existed!");
        }
        else {
            try{
            f.createNewFile();
            System.out.println("this file doesn't exist,but it has been created");
            }catch(IOException e){

                System.out.println("创建文件异常");
            }
        }
        //这里开始将开始实现第二部分文件读写功能
                if(f.length()==0 ){
                    System.out.println("this file is empty!");
                    try{
                    //write data, use FileOutStream

                    FileOutputStream fos = new FileOutputStream(f,false);  //只能一个字节一个字节写入,替换掉原来的东西
                    //定义DataOutputStream类
                    DataOutputStream dos = new DataOutputStream(fos); //这样是为了将数据传入流中,相当于数据流的入口

                    dos.writeUTF("I don't Love Java"); 
                    System.out.println("the file has been writteen");
                    fos.close();
                    dos.close();
                    }catch(Exception e){

                        System.out.println("the file write not correctly");

                    }

                } 
                else{
                    System.out.println("the file's length is: "+f.length()+"byte");
                    try{
                        //clear data

                        FileOutputStream fos = new FileOutputStream(f,false);  //只能一个字节一个字节写入,替换掉原来的东西
                        //定义DataOutputStream类
                        DataOutputStream dos = new DataOutputStream(fos); //这样是为了将数据传入流中,相当于数据流的入口

                        dos.write(new String("").getBytes()); //z这里就是为了清空文档
                        System.out.println("the file has been cleared,it is an empty file now");
                        fos.close();
                        dos.close();
                        }catch(Exception e){

                            System.out.println("the file clear not correctly");

                        }

                    try{
                        //write data, use FileOutputStream

                        FileOutputStream fos = new FileOutputStream(f,false);  //只能一个字节一个字节写入,替换掉原来的东西
                        //定义DataOutputStream类
                        DataOutputStream dos = new DataOutputStream(fos); //这样是为了将数据传入流中,相当于数据流的入口

                        dos.writeUTF("I Love Java"); //这个地方读入字符串
                        System.out.println("the file has been writteen");

                        dos.close();
                        }catch(Exception e){

                            System.out.println("the file write not correctly");

                        }
                    try{  
                    //read data, use FileInputStream


                    FileInputStream fis = new FileInputStream(f);  //只能一个字节一个字节读
                    //定义DataOutputStream类
                    DataInputStream dis = new DataInputStream(fis); //这样是为了将数据传入流中,相当于数据流的入口

                    System.out.println("the file's content is: "+dis.readUTF());

                    fis.close();
                    dis.close();

                        }catch(IOException e){
                            System.out.println("the file can't read correctly");
                        }

                }
        System.out.println("****************the DataInputStream has ended,the BufferedWriter and BufferedReader start now***********");
        try{  
            //read data, use FileInputStream


            FileOutputStream fos = new FileOutputStream(f);  //只能一个字节一个字节读
            //定义DataOutputStream类
            OutputStreamWriter osw = new OutputStreamWriter(fos); //这个是针对字符流,可以一个个输入字符
            BufferedWriter bw = new BufferedWriter(osw); //这样是为了将数据传入流中,相当于数据流的入口,但是可以输入字符串,并用行来读出
            System.out.println("please input the content that you want to show,end with new line <ok>");

            InputStreamReader isr = new InputStreamReader(System.in); //这样是为了将数据传入流中,相当于数据流的入口
            BufferedReader br = new BufferedReader(isr);
            while(true){
            String temp = new String(br.readLine());

            if(temp.equals("ok")) break; //注意这个地方不能用等号,用equals
            bw.write(temp);
            bw.newLine();
            }
            bw.newLine();
            System.out.println("the content from your keyboard has been written correctly");
            bw.close();

                }catch(IOException e){
                    System.out.println("the file has not written correctly");
                }
        try{  
            //read data, use FileInputStream


            FileInputStream fis = new FileInputStream(f);  //只能一个字节一个字节读
            //定义IutputStreamReader类
            InputStreamReader isr = new InputStreamReader(fis); //这样是为了将数据传入流中,相当于数据流的入口
            BufferedReader br = new BufferedReader(isr);
            System.out.println("the file's content is: ");
            //输出结果
            String s;
            while((s= br.readLine())!=null){

            System.out.println(s);
            }

            br.close();

                }catch(IOException e){
                    System.out.println("the file can't read correctly");
                }   

    }
    }

运行结果:
this file has existed!
this file is empty!
the file has been writteen
***********the DataInputStream has ended,the BufferedWriter and BufferedReader start now******
please input the content that you want to show,end with new line ok
hello,welcome to my blog!
test this file
progress a lot
ok
the content from your keyboard has been written correctly
the file’s content is:
hello,welcome to my blog!
test this file
progress a lot


这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值