Java简单IO介绍

Java IO流类结构图,转载自 https://www.cnblogs.com/zhaoyanjun/p/6292384.html

è¿éåå¾çæè¿°

 

在这里不对所有的类进行举例,只对FileReader/FileWriter, 文本输入输出

FileInputStream,FileOutputStream: 字节输入输出

下面是具体的代码示例:

package com.main;
import java.io.*;
import java.nio.charset.Charset;
public class FileTest { 
     
     public void readTextFile(String fileName){
         FileReader f = null;
         try {
            f= new FileReader(fileName);            
            char[] a=new char[1024];            
            while (f.read(a)>0){            
                String strTemp = new String(a);                
                System.out.println(getStringCharSet(strTemp));
            }
            f.close();
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
            try {
                f.close();
            } catch (IOException e1) {
                // TODO 自动生成的 catch 块
                e1.printStackTrace();
            }
        } 
     }
     
     public void readTextFile2(String fileName){
         File f = new File(fileName);
         String str = null;
         FileInputStream in = null;
         StringBuilder strB = null;
         
         try {
            in = new FileInputStream(f);
            byte[] b = new byte[1024];
            Charset charset = Charset.forName("utf8");
            strB = new StringBuilder();
            while (in.read(b)>0){
                str= new String(b,charset);
                strB.append(str);
            }
            System.out.println(strB);
            in.close();
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();            
            try {
                in.close();
            } catch (IOException e1) {
                // TODO 自动生成的 catch 块
                e1.printStackTrace();
            }
        }
     }
     //返回utf-8 编码的字符串
     public String getStringCharSet(String strValue){
         Charset charset = Charset.forName("utf8");
         byte[] b = strValue.getBytes();
         String c= new String(b,charset);
         return c;
     }
     
     //写入文本
     public void writelog(String strValue,String sFileName){
         FileWriter w = null;
         try {
            w = new FileWriter(sFileName,true);                
            w.write(strValue+"\n");
            w.close();
        } catch (IOException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
            try {
                w.close();
            } catch (IOException e1) {
                // TODO 自动生成的 catch 块
                e1.printStackTrace();
            }
        }
     }
     
     //以字节形式写入
     public void writelog2(String strValue,String sFileName){
         File f = null;
         FileOutputStream out = null;
         f= new File(sFileName);
         try {            
            out = new FileOutputStream(f,f.exists());
            strValue = strValue + "\n"; 
            byte[] b = strValue.getBytes();
            out.write(b);
            out.close();
        } catch (Exception e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
            try {
                out.close();
            } catch (IOException e1) {
                // TODO 自动生成的 catch 块
                e1.printStackTrace();
            }
        }
     }
}
 

package com.main;

public class Main {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        FileTest f = new FileTest();
        String sFileName = "E:\\Desktop\\Temp\\01.txt";
        f.writelog2("好久不见", sFileName);
        //f.readTextFile("E:\\Desktop\\Temp\\01.txt");
        /*StringBuilder c= new StringBuilder();
        c.append("Hello");
        f.test(c);
        System.out.print(c);*/
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值