zoukankan      html  css  js  c++  java
  • java学习之IO字符流

     1 package com.io;
     2 
     3 import java.io.*;
     4 /**
     5  * 文件字符流的读取
     6  * @author ganhang
     7  *
     8  */
     9 public class FileReaderDemo {
    10     public static void read(){
    11         File file =new File("1.txt");
    12         try {
    13             FileReader fr=new FileReader(file);
    14             StringBuffer sb=new StringBuffer();
    15             char [] cbuf=new char[10];
    16             int len=-1;
    17             while((len=fr.read(cbuf))!=-1){
    18                 sb.append(cbuf,0,len);
    19             }
    20             fr.close();
    21             System.out.println(sb);
    22         } catch (FileNotFoundException e) {
    23             e.printStackTrace();
    24         } catch (IOException e) {
    25             e.printStackTrace();
    26         }
    27         
    28         
    29     }
    30     public static void main(String[] args) {
    31         read();
    32     }
    33 }
     1 package com.io;
     2 
     3 import java.io.*;
     4 /**
     5  * 文件字符流的写入
     6  * @author ganhang
     7  *
     8  */
     9 public class FileWriterDemo {
    10     public static void write(){
    11         File file =new File("1.txt");
    12         try {
    13             FileWriter fr = new FileWriter(file,true);
    14             String str="哈哈哈哈";
    15             fr.write(str);
    16             fr.close();
    17             System.out.println("写入成功!");
    18         } catch (IOException e) {
    19             e.printStackTrace();
    20         }
    21     }
    22     public static void main(String[] args) {
    23         write();
    24     }
    25 }
  • 相关阅读:
    windows 安装mysql 步骤
    x-editable 的使用方法
    asp.net连接数据库
    fedora下根据字符查找软件包
    ubuntu 常用命令
    第8课-库函数方式文件编程
    第7课-系统调用方式文件编程
    第6课-函数库设计
    第5课-Linux编程规范
    第4课-Linux应用程序地址布局
  • 原文地址:https://www.cnblogs.com/ganhang-acm/p/5154302.html
Copyright © 2011-2022 走看看