zoukankan      html  css  js  c++  java
  • Java读取本地文件(输入流)

    package cn.buaa;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileReader;
    import java.io.InputStream;
    import java.io.Reader;
    
    public class Hello {
    
        public static void main(String[] args) throws Exception {
            //字节输入流
            inputReaderStram();
            
            
            //字符输入流
            charReaderStream();
            
            
            
        }
        
        public static void inputReaderStram() throws Exception{
            //1:使用File 类创建一个要操作的文件路径
            File file = new File("D:" + File.separator + "demo" + File.separator +"test.txt");
            if(file.exists()){
                //2:实例化InputStream类对象
                InputStream input = new FileInputStream(file);
                //3:开辟一个字节数组用于数据的读取
                byte data[] = new byte[1024];
                //4:读取数据
                input.read(data); //将数据读取到字节数组中
                System.out.println("读取数据: " + new String(data));
                input.close();
            }
        }
        
        public static void charReaderStream() throws Exception{
            //1:使用File 类创建一个要操作的文件路径
            File file = new File("D:" + File.separator + "demo" + File.separator +"test.txt");
            if(file.exists()){
                //2:实例化Reader类对象
                Reader in = new FileReader(file);
                //3:开辟一个字符数组用于数据的读取
                char[] data = new char[1024];
                int len = in.read(data);
                System.out.println(new String(data));
                in.close();
            }
        }
            
    }
    
    
        
        
    
        
        
  • 相关阅读:
    poj2728 Desert King
    bzoj4289 Tax
    洛谷P4141消失之物
    Code Forces 698A Vacations
    Code Forces 543A Writing Code
    洛谷P1133 教主的花园
    poj3177 Redundant Paths
    bzoj1151 动物园
    bzoj1503 郁闷的出纳员
    bzoj1208 宠物收养所
  • 原文地址:https://www.cnblogs.com/StanLong/p/6417860.html
Copyright © 2011-2022 走看看