zoukankan      html  css  js  c++  java
  • JAVA 基础之 IO复习

    一、文件:

    在IO包中唯一与文件相关的类就是 File类。
    File类中经常使用的常量和方法

    1、创建文件:
    指定路径和将要创建的文件名称字以及类型;
    然后调用 createNewFile()方法
    File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator+"hello.java");
      file.createNewFile();

    2、删除文件:
    指定路径和文件,包括类型属性,
    然后调用 delete()方法。

    File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator+"hello.java");
     file.delete ();

    3、创建文件文件夹:
    设定路径后,调用mkdir()方法
    如在“D:MyJavaProgram”这个路径下创建一个名叫“hello”的文件夹。那么程序例如以下:
    File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator+"hello");
      file.mkdir();
    4、列出指定文件夹下的所有文件:
    File类中有两个方法: 
    • public String[] list()
    • public File[] listFiles()
    第一个是列出该文件夹下所有文件的名字。不包括属性,也就是说,我看到的是hello这个文件名称,而不晓得它是word还是txt文本;
    第二个则是以文件夹的形式,包括属性和名称,以File文件数组的形式
    第一个示比例如以下:
    public class FileDemo2{
     public static void main(String[] args){
      File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator);
      String[] str = file.list();
     
      for(int i =0; i< str.length;i++){
       System.out.println(str[i]);
       }
     }
    }
    第二个示比例如以下:
    import java.io.*;
    public class FileDemo2{
     public static void main(String[] args){
      File file = new File("D:"+File.separator+"MyJavaProgram"+File.separator);
      File[] str = file.listFiles();
     
      for(int i =0; i< str.length;i++){
       System.out.println(str[i]);
       }
     }
    }
    执行结果:


    二、使用RandomAccessFile来写入读取指定位置的数据
    以上File仅仅是用来创建管理文件等, 可是向文件里写入读取数据。还是得借助其它类对象。RandomAccessFile类就是用来写入读取指定位置的数据的
    RandomAccessFile类主要完毕随机读取功能,能够读取制定位置的内容。



    由于在文件里,所有的内容都是依照字节存放的,如一个int 整型数就是占领4个字节。
    从上面经常使用的方法表中能够看到,RandomAccessFile类中有两个构造方法。
    比如,如今用第一个构造方法来完毕一个写入字符串和整数的操作——
    import java.io.*;
    public class RandomAccessFileDemo{
     public static void main (String args[])throws Exception{
     
      File file = new File("d:"+File.separator+"MyJavaProgram"+File.separator+"test.txt");
      //读写模式。假设文件不存在则自己主动创建
      RandomAccessFile ramdomFile = new RandomAccessFile(file,"rw");
      //写入2个数据
      String name = "girl";
      int love = 25257758;
      //写字符串用writeBytes方法
      ramdomFile.writeBytes(name);
      //写整型数。用writeInt方法
      ramdomFile.writeInt(love);
      //对文件的读写操作完毕后一定要记得关闭
      ramdomFile.close();
     
     }
    }

    结果:

    好,如今再演示怎样读取指定位置的数据
    如今跳过前面“girl”这4个字节,直接读取后面的数字
    import java.io.File ;
    import java.io.RandomAccessFile ;
    public class RandomAccessFileDemo{
     
     public static void main(String args[]) throws Exception{
     File file = new File("d:"+File.separator+"MyJavaProgram"+File.separator+"test.txt");
      //读写模式,假设文件不存在则自己主动创建
      RandomAccessFile randomFile = new RandomAccessFile(file,"rw");
      //读取字符串后面的数字,得先跳过前面的字符串(占4个字节)
      randomFile.skipBytes(4);
      int i = randomFile.readInt();
      //输出所读到的值
      System.out.println("读到的数字是:"+i);
      //再回头读字符串。用seek方法设置指针位置
      randomFile.seek(0);
      byte[] temp = new byte[4];
      for(int j = 0;j< temp.length;j++){
       temp[j] = randomFile.readByte();
      }
      //转化为字符串
      String s = new String(temp);
      System.out.println("读到的字符串是:"+ s);
      //System.out.println("读到的字符串是"+s);
      //对文件的读写操作完毕后一定要记得关闭
      randomFile.close();
     }
    };

    执行结果是:

    这中间遇到了一个问题就是在用System.out.println()
    打印输出中文。总是报告编码警告:


    后来确实是编码格式问题。解决方法是:用记事本打开java源文件。然后另存为,选择 ANSI编码,覆盖,然后再次编译,就可以消除警告或者错误。




  • 相关阅读:
    有问题的Py代码
    Python撑爆内存的代码
    python socket 绑定端口收发信息
    python socket UDP通信
    B类IP地址
    python in的用法
    Python continue的用法
    python27接受用户输入的数据
    基于jQuery实现左右图片轮播(原理通用)
    Jquery实现的简单轮播效果-代码
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10867840.html
Copyright © 2011-2022 走看看