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();
    }
    
    
    }
  • 相关阅读:
    在Livemedia的基础上开发自己的流媒体客户端
    开源流媒体处理库live555服务器端、客户端源代码分析总结
    海康、大华IpCamera RTSP地址和格式
    专题:Windows编译x264、SDL、faac、ffmpeg过程
    linux下c/c++ IDE开发工具介绍
    socket选项自带的TCP异常断开检测
    logstash-input-jdbc实现mysql 与elasticsearch实时同步(ES与关系型数据库同步)
    哈希存储、哈希表原理
    APM系列-国外新兴厂商New Relic vs. AppDynamics
    SkyWalking 分布式追踪系统
  • 原文地址:https://www.cnblogs.com/111testing/p/8099938.html
Copyright © 2011-2022 走看看