package com.mkyong.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadFileExample {

    public static void main(String[] args) {

        File file = new File("C:/robots.txt");
        FileInputStream fis = null;

        try {
            fis = new FileInputStream(file);

            System.out.println("Total file size to read (in bytes) : "
                    + fis.available());

            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                System.out.print((char) content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fis != null)
                    fis.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

JDK1.7 可用

package com.mkyong.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class ReadFileExample {

    public static void main(String[] args) {

        File file = new File("C:/robots.txt");

        try (FileInputStream fis = new FileInputStream(file)) {

            System.out.println("Total file size to read (in bytes) : "+ fis.available());

            int content;
            while ((content = fis.read()) != -1) {
                // convert to char and display it
                System.out.print((char) content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

相关文章:

  • 2021-11-26
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-05
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
  • 2021-11-20
  • 2021-09-03
相关资源
相似解决方案