zoukankan      html  css  js  c++  java
  • Java io流详解三

    public  class  IOpractise  {
    
    
    public  void    iotest()  {
    int  b=  0;
    FileInputStream  fis  =  null;
    try  {
    fis  =  new  FileInputStream("C:\Users\wb-cjz286752\Desktop\小程序.txt");
    }  catch  (FileNotFoundException  e)  {
    e.printStackTrace();
    System.out.println("系统找不到指定文件!");
    System.exit(-1);
    
    }
    
    long  num  =0;
    try  {
    while((b=fis.read())!=-1){
    //System.out.println((char)b);
    System.out.println(b);
    num++;
    }
    fis.close();
    System.out.println();
    System.out.println("总共读了"+num  +  "个字节的文件");
    }  catch  (IOException  e)  {    
    e.printStackTrace();
    System.out.println("文件读取错误!");
    }
    
    }
    
    
    public  void  iotest1()  throws  IOException{
    
    try  {
    FileInputStream  fis  =  new  FileInputStream("C:\Users\wb-cjz286752\Desktop\小程序.txt");
    int  read  =  0;
    while((read=fis.read())!=-1){
    System.out.println(read);    
    }
    fis.close();
    }  catch  (FileNotFoundException  e)  {
    //  TODO  Auto-generated  catch  block
    e.printStackTrace();
    }
    
    }
    
    public  String  iotest2()  throws  IOException{
    StringBuffer  result  =  new  StringBuffer();
    
    try  {
    BufferedReader  bf  =  new  BufferedReader(new  FileReader("C:\Users\wb-cjz286752\Desktop\webelement.txt"));
    String  str  =  null;
    while  ((str=bf.readLine())!=null){
    result.append(str);
    //System.out.println(result);    
    }
    bf.close();
    }  catch  (FileNotFoundException  e)  {
    e.printStackTrace();
    }
    return  result.toString();
    }
    
    
    public  void  iotest3(String  filename)  throws  IOException{
    StringBuffer  result  =  new  StringBuffer();
    try  {
    BufferedReader  br  =  new  BufferedReader(new  FileReader(filename));
    String  str  =  null;
    while((str=br.readLine())!=null){
    result.append(System.lineSeparator()+str);
    //System.out.println();
    }
    b r.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println(result.toString());
    
    }
    
    public void iotest4() throws IOException{
    try {
    FileWriter fw = new FileWriter("C:\Users\wb-cjz286752\Desktop\1111111111111.txt");
    
    fw.write("123llove");
    fw.flush();
    fw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    
    try {
    FileReader fr = new FileReader("C:\Users\wb-cjz286752\Desktop\1111111111111.txt");
    int it =0;
    while((it=fr.read())!=-1){
    System.out.print((char)it);
    
    }
    fr.close();
    
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    
    }
    
    public void iotest5(){
    try {
    FileReader fr = new FileReader("C:\Users\wb-cjz286752\Desktop\webelement.txt");
    int it =0;
    char[] buf = new char[10];
    StringBuilder sb = new StringBuilder();
    try {
    while((it=fr.read(buf))!=-1){
    sb.append(new String(buf,0,it));
    }
    String str = sb.toString();
    System.out.println(str);
    fr.close();
    
    } catch (IOException e) {
    e.printStackTrace();
    }
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    
    }
    
    public void iotest6() throws IOException{
    FileWriter fw = new FileWriter("C:\Users\wb-cjz286752\Desktop\1111111111111.txt",true);
    try {
    fw.write("1234567890testtest!!!!!!!");
    fw.close();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    
    public void iotest7(){
    try {
    FileReader fr = new FileReader("C:\Users\wb-cjz286752\Desktop\1111111111111.txt");
    FileWriter fw = new FileWriter("C:\Users\wb-cjz286752\Desktop\2222222222222.txt");
    char [] buf = new char[2];
    int it =0;
    while ((it=fr.read(buf))!=-1){
    String str = new String(buf,0,it);
    fw.write(str);
    }
    fw.flush();
    fw.close();
    fr.close();
    
    } catch (IOException e) {
    e.printStackTrace();
    }
    
    }
    
    public void iotest8() throws IOException{
    try {
    FileReader fr = new FileReader("C:\Users\wb-cjz286752\Desktop\2222222222222.txt");
    BufferedReader br = new BufferedReader(fr);
    String str = null;
    StringBuilder sb = new StringBuilder();
    while ((str=br.readLine())!=null){
    sb.append(str);
    }
    br.close();
    System.out.println(sb.toString());
    
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    
    public void iotest9() throws IOException{
    try {
    BufferedReader br = new BufferedReader(new FileReader("C:\Users\wb-cjz286752\Desktop\2222222222222.txt"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("C:\Users\wb-cjz286752\Desktop\3333333333333.txt"));
    String str = null;
    while ((str=br.readLine())!=null){
    bw.write(str);
    bw.newLine();
    }
    bw.flush();
    bw.close();
    br.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    }
    
    public void iotest10() throws IOException{
    try {
    FileInputStream fis = new FileInputStream("C:\Users\wb-cjz286752\Desktop\3333333333333.txt");
    FileOutputStream fos = new FileOutputStream("C:\Users\wb-cjz286752\Desktop\4444444444444.txt");
    int it = 0;
    byte[] byt = new byte[1024];
    while ((it=fis.read(byt))!=-1){
    fos.write(byt,0 ,it);
    }
    fos.flush();
    fos.close();
    fis.close();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }
    
    }
    
    public static void main(String[] args) throws IOException {
    IOpractise iop = new IOpractise();
    //iop.iotest3("C:\Users\wb-cjz286752\Desktop\webelement.txt");
    iop.iotest10();
    }
    
    
    }
  • 相关阅读:
    mac os x 之通过远程主机在nginx上部署web静态页面
    基于jQuery UI的调色板插件推荐colorpicker
    Mac 访问隐藏文件方法! 网上方法在我电脑上都不可用!
    JavaScript设计模式学习之单例模式
    由一篇博文做出的代码,不用Math.round()如何实现其功能
    mac os x之解决npm安装包失败,或者nodejs工程缺少依赖
    用nginx的反向代理机制解决前端跨域问题
    mac之os x系统下搭建nodejs+express4.x+mongodb+gruntjs整套前端工程
    sourcetree window10 闪退
    滚动条自定义样式
  • 原文地址:https://www.cnblogs.com/111testing/p/8099938.html
Copyright © 2011-2022 走看看