zoukankan      html  css  js  c++  java
  • java io读写文件

    java io读写文件
    相关阅读:http://www.cnblogs.com/wing011203/archive/2013/05/03/3056535.html

    public class DemoIO {
    
        public static byte[] readForInputStream(File file) throws IOException{
            InputStream in = new FileInputStream(file);  
            byte b[]=new byte[(int)file.length()];     //创建合适文件大小的数组   
            int len = 0;  
            int temp=0; //所有读取的内容都使用temp接收 
             while((temp=in.read())!=-1){//当没有读取完时,继续读取   
                 b[len]=(byte)temp;  
                 len++;  
             }
             in.close();  
             return b;  
        }
        
        public static void writeForOutputStream(File file) throws IOException {
            String content = "这是一段感人肺腑的文字";
            FileOutputStream fop = new FileOutputStream(file,true);//第二个参数ture:表示在内容中追加
            // 判断文件是否存在
            if (!file.exists()) {
                file.createNewFile();
            }
            byte[] contentInBytes = content.getBytes();
            fop.write(contentInBytes);
            fop.flush();
            fop.close();
            System.out.println("--------write success-------------");
        }
        
        /**
         * @param args
         * @throws IOException 
         */
        public static void main(String[] args) throws IOException {
             
            writeForOutputStream(new File("e:/testio.txt"));
            
            byte[] b = readForInputStream(new File("e:/testio.txt"));
            System.out.println(new String(b, 0, b.length));
        }
    }
  • 相关阅读:
    各种 SDk
    面试第一周
    KVC/KVO 的概述和使用
    How Many Tables HDU
    The Suspects POJ
    Wireless Network POJ
    Layout POJ
    The Shortest Path in Nya Graph HDU
    Extended Traffic LightOJ
    Tram POJ
  • 原文地址:https://www.cnblogs.com/gavinYang/p/3577251.html
Copyright © 2011-2022 走看看