package cn.com;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

public class testFile {

public static void main(String[] args) throws IOException {
  //WriterFile();
  ReadFile();
  try {
    WriterFile1();
  } catch (IOException e) {
    e.printStackTrace();
  }

ReadFile1();
}

public static void WriterFile(){
  FileWriter fw = null;
  try {
    fw = new FileWriter("E:/dome.txt", true); //获取到输出流
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write("待我沙场归来,许你西窗话下。");
    fw.write("待我君临天下,许你一生荣华。");
    fw.write("\r\n");
    fw.write("待我浪迹天涯,许你四海为家。");
  } catch (Exception e) {
    e.printStackTrace();
  }finally{
    if(fw != null){
      try {
      fw.close();
    } catch (IOException e) {
      e.printStackTrace();
      }
    }
  }
}

public static void ReadFile(){
  FileReader fr = null;
  try {
    fr = new FileReader("E:/dome.txt");
    String fileName = "dome.txt";
    //判断文件名是否为.java 结尾。
    if(!fileName.endsWith(".txt")){
    System.out.println("目标文件后坠名不是.txt");
    return;
  }
  int a;
  char[] cbuf = new char[1024];
  //String[] str = new String[12];
  //String[] str1 = {"1","2"};
  //String[] str2;
  BufferedReader bf = new BufferedReader(fr);
  String temp;
  while((temp = bf.readLine()) != null){
    System.out.println("读取完毕:***"+temp);
  }
  //每次只能读取一个字符,返回int字符码,(char)强制转换成字符
  while((a = fr.read())!=-1){
    System.out.println("读取完毕:"+(char)a);
  }
  //用数组读取多个字符
  while((a = fr.read(cbuf))!= -1){
    System.out.println("读取完毕:"+new String(cbuf, 0, a));
  }
  } catch (Exception e) {
    e.printStackTrace();
  }finally{
    if(fr != null){
      try {
      fr.close();
    } catch (IOException e) {
      e.printStackTrace();
      }
    }
  }
}

public static void WriterFile1() throws IOException{
  OutputStream os = null;
  try {
    os = new FileOutputStream(new File("E:/dome1.txt"));
    String str = "待我一袭袈裟,许你相思放下。";
    byte[] bt = str.getBytes();
    os.write(bt);
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  }finally{
    if(os != null){
      os.close();
    }
  }
}

public static void ReadFile1() throws IOException{
  InputStream is = null;
  try {
    is = new FileInputStream(new File("E:/dome1.txt"));
    int a ;
    byte[] bt = new byte[1024];
    while((a = is.read(bt)) != -1){
    System.out.println("读取完毕:"+new String(bt, 0 , a));
  }
  } catch (Exception e) {
    e.printStackTrace();
  }finally{
    if(is != null){
      is.close();
      }
    }
 }

public static void bufferedWriter() throws IOException{
  FileOutputStream fs = null;
  try {
    fs = new FileOutputStream(new File("E:/dome2.txt"));
    String str = "待我高官厚禄,许你足迹天涯。";
    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fs));
    bw.write(str);
  } catch (Exception e) {
    e.printStackTrace();
  }finally{
    fs.close();
  }
}


public static void bufferedReader() throws IOException{
  FileInputStream fs = null;
  try {
    fs = new FileInputStream("E:/dome2.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fs));
    String temp;
    while((temp = br.readLine()) != null){
    System.out.println("读取完毕:"+temp);
  }
  } catch (Exception e) {
    e.printStackTrace();
  }finally{
    fs.close();
    }
  }

}

posted on 2015-04-09 15:30  遍地狼烟★风儿起  阅读(217)  评论(0编辑  收藏  举报