zoukankan      html  css  js  c++  java
  • [Java] 读写字符串数据

    package test.stream;
    
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    /**
     * 读写字符串数据
     * @author Frost.Yen
     * @E-mail 871979853@qq.com
     * @date 2016年4月13日
     */
    public class TestWriteData01 {
        public static void main(String[] args) {
            FileOutputStream fos = null;
            FileInputStream fis = null;
            try {
                fos = new FileOutputStream("E:\JAVA\Examples\To Learn\src\test\stream\data.dat");
                String hello = "hello world";
                byte[] buf = hello.getBytes();
                fos.write(buf, 0, buf.length);
                
                String str = String.valueOf(12024568);
                fos.write(str.getBytes(), 0, str.getBytes().length);
                str = String.valueOf(13);
                fos.write(str.getBytes(), 0, str.getBytes().length);
                str = String.valueOf(28);
                fos.write(str.getBytes(), 0, str.getBytes().length);
                
                fis = new FileInputStream("E:\JAVA\Examples\To Learn\src\test\stream\data.dat");
                byte[] buf1 = new byte[1024];
                int len = 0;
                while((len = fis.read(buf1))>=0){
                    System.out.write(buf1, 0, len);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                try {
                    if(fos!=null) fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    if(fis!=null) fis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            
        }
    }
  • 相关阅读:
    Reverse Bits
    Number of 1 Bits
    Single Number,Single Number II
    Repeated DNA Sequences
    Fraction to Recurring Decimal
    Isomorphic Strings
    Valid Sudoku
    Count Primes
    Bulls and Cows
    高性能Web服务器Nginx的配置与部署研究(12)应用模块之Memcached做文件缓存时压缩引起的问题
  • 原文地址:https://www.cnblogs.com/frost-yen/p/5387123.html
Copyright © 2011-2022 走看看