zoukankan      html  css  js  c++  java
  • java文本输入输出小结

    Java 文本输入主要包含两种方法:FileRead -- 按字符读入,InputSreamReader -- 按行输入。

    java 文本输出也包含两种方法:FileWriter 和 OuputStreamWriter,这两种都是按字符输出。

    应用代码如下:

      1 package stream.inout;
      2 
      3 import java.io.*;
      4 
      5 public class FileStream {
      6     
      7     public static void main(String a[]){
      8         String inputfilepath = "E:/in.txt";
      9         /*
     10         in.txt data : 
     11             12345
     12         in2.txt data : 
     13             11
     14             22
     15             33
     16             44
     17             55
     18         output1.txt data :
     19             tttttrrrrreeeeewwwwwqqqqq
     20         output2.txt data :
     21             tttrrreeewwwqqq
     22         */
     23         
     24         //method no.1 =======================================
     25         try{
     26             int []input1 = new int[5];
     27             File inputfile = new File(inputfilepath);
     28             if(inputfile.isFile() && inputfile.exists()){
     29                 //这里的读入是按字符读入
     30                 FileReader fileread = new FileReader(inputfile);
     31                 int tmp = 0;
     32                 int i = 0;
     33                 while((tmp = fileread.read()) != -1){
     34                     input1[i++] = tmp - '0';
     35                 }
     36                 fileread.close();
     37                 for(i = 0; i < 5; i ++)
     38                     System.out.println("input1[ " + i +" ]= " + input1[i]);
     39             }else{
     40                 System.out.println("file is not exist");
     41             }
     42         }catch (Exception e){
     43             System.err.println("error happened");
     44             e.printStackTrace();
     45         }
     46         
     47 
     48         //method no.2 =======================================
     49         try{
     50             String []input2 = new String[5];
     51             String input2filepath = "E:/in2.txt";
     52             File inputfile = new File(input2filepath);
     53             if(inputfile.isFile() && inputfile.exists()){
     54                 //这里的读入是按行读入
     55                 InputStreamReader isr = new InputStreamReader(new FileInputStream(inputfile));
     56                 BufferedReader br = new BufferedReader(isr);
     57                 int i = 0;
     58                 String line = null;
     59                 //需注意的是每执行一次br.readLine(),就跳入下一行,故引入一个变量来记录
     60                 while((line = br.readLine()) != null){
     61                     input2[i++] = line;
     62                 }
     63                 br.close();
     64                 isr.close();
     65                 for(i = 0; i < 5; i ++)
     66                     System.out.println("input2[ " + i +" ]= " + input2[i]);
     67                 
     68             }else{
     69                 System.out.println("file is not exist");
     70             }
     71         }catch (Exception e){
     72             System.err.println("error happened");
     73             e.printStackTrace();
     74         }
     75         
     76         //output method no.1 ============================
     77         String outputfile1 = "E:/output1.txt";
     78         String outputfile2 = "E:/output2.txt";
     79         try{
     80             String []output = {"ttttt", "rrrrr", "eeeee", "wwwww", "qqqqq"};
     81             FileWriter filewriter = new FileWriter(outputfile1, false);
     82             for(int i = 0; i < 5; i ++){
     83                 filewriter.write(output[i]);
     84             }
     85             filewriter.close();
     86             
     87         }catch (Exception e){
     88             System.err.println("error happened");
     89             e.printStackTrace();
     90         }
     91         //output method no.2 ============================
     92         try {
     93             String []output1 = {"ttt", "rrr", "eee", "www", "qqq"};
     94             OutputStreamWriter outsw = new OutputStreamWriter(new FileOutputStream(outputfile2));
     95             BufferedWriter bw = new BufferedWriter(outsw);
     96             
     97             for(int i = 0; i < 5; i ++){
     98                 bw.write(output1[i]);
     99             }
    100             bw.flush();
    101             bw.close();
    102             outsw.close();
    103         } catch (Exception e) {
    104             // TODO: handle exception
    105             System.err.println("error happened");
    106             e.printStackTrace();
    107         }
    108     }
    109 
    110 }
  • 相关阅读:
    实现业务逻辑的几种不同方法,及其优缺点 事务脚本、表模块、活动记录、领域模型
    JQuery Tree Jquery树型菜单插件
    SQL实现表名更改,列名更改,约束更改
    Differences Between NHibernate and Entity Framework
    CSS菜单,图片阴影,表单样式
    事务
    纯CSS三列布局
    Quartz Develop Practice One
    创建WinPE启动盘、常用imagex指令、常用dism指令
    Using User Defined Types in COM & ATL
  • 原文地址:https://www.cnblogs.com/z1987/p/4886289.html
Copyright © 2011-2022 走看看