zoukankan      html  css  js  c++  java
  • java 读取并且显示 txt 文件

    系统:mac os x 10.9

    eclipse

    在eclipse 中建立一个project, 命名为Cin_txt,

    Cin_txt的内容

    test
    wang
    hello
    world

    以下是输入的代码

     

    import java.io.File;
    
    import java.io.FileInputStream;
    
    import java.io.IOException;
    
     
    
    public class Cin_txt {
    
     
    
    public static void main(String[] args) {
    
     
    
    File file = new File("/Users/yinjiewang/Documents/test.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();
    
    }
    
    }
    
    }
    
    }

     JDK7 版本

    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();
            }
        }
    }
  • 相关阅读:
    golang reflect知识集锦
    go test benchmark
    go build -tags 的使用
    golang http 服务器的接口梳理
    go中如何更好的迭代
    滚动条css实现
    记vue nextTick用到的地方
    捕获Ctrl + C中断 优雅的退出程序 golang
    如何处理动态JSON in Go
    golang实现参数可变的技巧
  • 原文地址:https://www.cnblogs.com/assassin/p/3726902.html
Copyright © 2011-2022 走看看