Java中对文件的基本操作

1:创建文件

public class createfile {

    public static void main(String[] args) {

        File file=new File("D:\\hello.txt");            在D盘中创建hello文档

        try {

            file.createNewFile();

            System.out.println("文件创建成功");

        } catch (IOException e) {

            System.out.println("文件创建失败");

            throw new RuntimeException(e);

        }

    }

}

其中,对文件的一些基本操作:

System.out.println(file.getName());                  输出该文件的名字

System.out.printIn(file.delete);                         删除该文件

System.out.println(file.getParentFile());           输出该文件的父文件

System.out.println(file.exists());                       输出文件是否存在    

System.out.println(file.isFile());                       输出该文件是否是文件

System.out.println(file.getAbsolutePath());     输出该文件的绝对路径

System.out,println(file.length());                      输出该文件的大小(一个英文字符为一个字节,一个汉语为三个字节)

 

2:Java io流

Input:读取外部数据

Output:将数据输出到磁盘,光盘等储存设备中

 

字节流

字符流

输入流

InputStream

Reader

输出流

OutputStream

Writer

 

 

单个字节的读取
public class text {
    public static void main(String[] args) throws IOException {
    File file=new File("D:\\hello.txt");                
        try {
            file.createNewFile();
            System.out.println("文件创建成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        int ReadDate=0;
        FileInputStream fileInputStream=null;
        try {
   fileInputStream=new FileInputStream(file);
while((ReadDate=fileInputStream.read())!=-1){           按字符读取数据
                System.out.print((char) ReadDate);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            fileInputStream.close();              关闭输入流
        }
    }
}

 

 

创建byte数组读取数据,相比于单个字节读取效率高

public class text {
    public static void main(String[] args) throws IOException {
            InputStream fileInputStream = new FileInputStream("D:\\abc.txt");
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(buf)) != -1) {
             
                for (int i = 0; i < len; i++) {
                    System.out.print((char) buf[i]);
                }
            }
            fileInputStream.close();
            OutputStream fileOutputStream = new FileOutputStream("D:\\aaa.txt");
            fileOutputStream.write(buf);
            fileInputStream.close();
        }
    }

 

文件的输出

public class text {
    public static void main(String[] args) throws IOException {
        String filepath="D:\\filewrite.txt";
        FileOutputStream fileOutputStream=null;
        try {
            fileOutputStream=new FileOutputStream(filepath);
            fileOutputStream.write('a');               可以单个字符读取
            String str="fgsfg";                       也可以创建字符串
            fileOutputStream.write(str.getBytes());
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
}

 

使用FileReader读取文件内容,输出在控制台

首先在D盘创建一个文件(名字叫Java软件工程)

import java.io.FileReader;
import java.io.IOException;

 

public class demo8 {
    public static void main(String[] args)  {
        try {
            FileReader fileReader=new FileReader("D:\\java软件工程");
            int  i=0;
            while ((i=fileReader.read())!=-1){
                System.out.print((char)i);
            }
            fileReader.close();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

             

使用filewrite写文件,将Java软件工程中的内容写入Java软件工程大数据文件中

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class demo9 {
    public static void main(String[] args) throws IOException {
        try {
            FileReader fileReader=new FileReader("D:\\java软件工程");
            FileWriter fileWriter=new FileWriter("D:\\java软件工程大数据");
            int i=0;
            while ((i=fileReader.read())!=-1){
                System.out.print((char)i);
                fileWriter.write(i);
            }
            fileReader.close();
            fileWriter.flush();
            fileWriter.close();
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_69764845/article/details/127895881