zoukankan      html  css  js  c++  java
  • Java中的读文件,文件的创建,写文件

    前言

    大家好,我是 Vic,今天给大家带来Java中的读文件,文件的创建,写文件的概述,希望你们喜欢

    示意图

    读文件

    public static void read(String path,String filename){
     try{
      int length=0;
      String str="";
      byte buffer[] = new byte[10];
      FileInputStream fis = new FileInputStream(new File(path,filename));
      while((length=fis.read(buffer,0,buffer.length))!=-1){
      str+=new String(buffer,0,length);
      }
      System.out.println(str);
      fis.close();
      }catch(FileNotFoundException e){
       System.out.println("文件不存在");
       }catch(IOException e){
        e.printStackTrace();
      }
    }
    

    文件的创建

    public class FileDemo{
     public static void createFolder(String path){
      File folder=new File(path);
      if(folder.exists()){
       System.out.println("文件夹已存在!");
      }else{
       folder.mkdir();
      }
     }
      public static void createFile(String path,String filename){
      File file=new File(path,filename);
      if(file.exists()){
       System.out.println("文件已存在!");
       System.out.println(file.length());
       }else{
        try{
         file.createNewFile();
        }catch(IOException e){
         System.out.println("文件创建失败");
        }
      }
     }
      public static void main(String[] args){
       FileDemo.createFolder("c:/text");
       FileDemo.createFile("c:/text","1.txt");
      }
    }
    

    写文件

    public static void write(String path,String filename){
     try{
      String str="123456789";
      byte b[] = str.getBytes();
      FileOutputStream fos=new FileOutputStream(new File(path,filename));
      fos.write(b);
      }catch(FileNotFoundException e){
       System.out.println("文件不存在");
      }catch(IOException e){
       System.out.println("写文件失败");
      }
    }
    

    获取文件的属性

    String getName()

    boolean canRead()
    boolean canWrite()

    boolean exits()

    boolean isFile()
    boolean isDirectory()
    boolean isHidden()

    相关知识与技术

    boolean mkdir():创建目录,若成功返回true

    boolean createNewFile():创建一个文件

    boolean delete():删除一个文件

    Java中流的分类

    • 流的运动方向:分为输入流和输出流两种
    • 流的数据类型:分为字节流和字符流

    所有的输入流类都是抽象类,所有的输出流类都是抽象类。

    字节:InputStream,OutputStream
    字符:Reader类,Writer类

    从输入流读取数据:

    FileInputStream vFile=new FileInputStream("File1.dat");
    vFile.read();
    vFile.close();
    

    输出流:

    FileOutputStream oFile=new FileOutputStream("File2.dat");
    oFile.write(vFile.read()):
    oFile.close();
    

    如果觉得不错,那就点个赞吧!❤️

    总结

    • 本文讲了Java中的读文件,文件的创建,写文件,如果您还有更好地理解,欢迎沟通
    • 定位:分享 Android&Java知识点,有兴趣可以继续关注
  • 相关阅读:
    HTML DOM 教程Part5 [DOM 冷门对象] 摘录自W3C School
    Javascript DOM 的节点操作示例
    CSS 教程Part2 [背景、文本、字体](摘录自 W3C School)
    Stream Part.1
    Thread.Join() 方法
    CSS 教程Part3 [列表、表格、轮廓](摘录自 W3C School)
    使用委托异步调用方法让程序并行运行
    JSON 数据格式
    C# WinForm 程序中获取本机IP地址
    微软示例代码 for ManualResetEvent
  • 原文地址:https://www.cnblogs.com/dashucoding/p/11932632.html
Copyright © 2011-2022 走看看