io相关

package io;

import java.io.*;

public class InputStreamSample{

         public static void main(String[] args) throws IOException,FileNotFoundException{

         //要是没有a.jpg的文件就会异常。FileNotFoundException(系统找不到指定的文件)

                   File f=new File("F://a.jpg");

                   //如果F//aa.jpg存在的话FileInputStream 就是建立一个连接从这里开始读

                   FileInputStream fis=new FileInputStream(f);

                   //读完了把他写到指定的地址,就是输出流后面的地址,,,默认的写到当前路径

                   FileOutputStream fos=new FileOutputStream("F://ccc.jpg");

                   //调用fisread开始读,他的返回类型时int

                   int data=fis.read();

                   //通过便利看一下是不是读完了,当返回-1时证明读完了,否则继续读

                   while(data>=0){

                            fos.write(data);

                            data=fis.read();

                   }

                   //将缓存的内容全部写到内存

                   fos.flush();

                   //关闭两个流,先关fis再关fos

                   fis.close();

                   fos.close();

         }

}

 

 

 

 

 

package a11;

import java.io.File;

public class FileSample{

         public static void main(String[] args){

                   File file=new File("F://sln//my");

                   //判断是否有这个目录没有的话创建一个

                   if(!file.mkdir()){

                            System.out.println("没有此目录创建一个");

                            file.exists();

                            System.out.println("创建成功");

                   }

         }

}

 

 

package a011;

import java.io.*;

         public class Sample3{

                   public static void main(String[] args){

                            FileInputStream fis=null;

                            FileOutputStream fos=null;

                            try{

                                     File f=new File("F://a//b.txt");

                                     ////说明是f2的当前文件,去掉//就是控制台的当前文件了

                                     File f2=new File("F://a");

                                     //判断有没有a这个目录,没有的话创建一个

                                     if(!f2.exists()){

                                               f2.mkdir();

                                     }

                                     //用第二个构造函数..    //说明是f2的当前文件,去掉//就是控制台的当前文件了,这样就把c写到了a文件夹下了

                                     File f3=new File(f2,"//c.txt");

                                     fis=new FileInputStream(f);

                                     fos=new FileOutputStream(f3);

                                     int data=fis.read();

                                     while(data!=-1){

                                               fos.write(data);

                                               data=fis.read();

                                     }

                                     fos.flush();

                            }catch(FileNotFoundException ff){

                                     ff.printStackTrace();

                            }catch(IOException fm){

                                     fm.printStackTrace();

                            }finally{

                                     try{

                                               fis.close();

                                     }catch(IOException e){

                                               e.printStackTrace();

                                     }try{

                                               fos.close();

                                     }catch(IOException e1){

                                               e1.printStackTrace();

                                     }

                            }

                   }

         }

 

 

package a011;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.File;

import java.io.PrintWriter;

public class Sample5{

         public static void main(String[] args){

                   BufferedReader br=null;

                   PrintWriter pw=null;

                   try{

                            File f=new File("F://a.html");

                            //在当前目录下,就是光标闪的地方,在sln下有文件夹my。所以就在my下生成了一个html,在F盘下有一个a.html

                            File out=new File("my//a.html");

                            //获得输入流对象,利用BufferedReader,缓存优化

                            br=new BufferedReader(new FileReader(f)); //new FileReader(f)????

                            //获得输出流对象。利用PrintWrite包装。在利用BufferedWriter包装

                            pw=new PrintWriter(new BufferedWriter(new FileWriter(out)));

                   //一行行读返回的是StringBufferedReader的方法readerLine是一行行读的意思

                            String line=br.readLine();

                            while(line!=null){

                                     pw.println(line);

                                     line=br.readLine();

                            }

                            //这里不用fulsh()????????????

//捕获异常readLine()会抛出异常IOException

                   }catch(IOException ie){

                            ie.printStackTrace();

                   }finally{

                            try{

                                     br.close();

                            }catch(Exception e){

                                     e.printStackTrace();

                            }try{

                                     pw.close();

                            }catch(Exception e){

                                     e.printStackTrace();

                            }

                   }

         }

}

package a14;

import java.net.Socket;

import java.net.ServerSocket;

import java.io.*;

public class SocketServer{

         //服务器中的方法

         public static void serve() throws Exception{

         //创建一个服务器,得到一个ServerSocket对象。规定一个端口

         ServerSocket server=new ServerSocket(1111);

         //当服务器和客户端连接起来后会立即生成一个Socket对象

         Socket s=server.accept();

         //然后在生成io的对象。这里用的是封装类

         BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream()));

         PrintWriter pw=new PrintWriter(new OutputStreamWriter(s.getOutputStream()));

         //

         pw.println("hello");

         pw.flush();

         System.out.println(br.readLine());

         //      pw.flush();    放下面不认识,要放到上面

         br.close();

                   pw.close();

                   s.close();

                   server.close();

         }

         public static void main(String[] args) throws Exception{

                  

                   serve();

         }

}

 

 

package a14;

import java.net.*;

import java.io.*;

public class SocketClient{

         public static void client(){

         Socket client=null;

         BufferedReader br=null;

         PrintWriter pw=null;

         try{

                   client= new Socket("localhost",1111);

                   br=new BufferedReader(new InputStreamReader(client.getInputStream()));

                   pw=new PrintWriter(new OutputStreamWriter(client.getOutputStream()));

                   System.out.println(br.readLine());

                   pw.println("hello this is client");

                       pw.flush();

                   }catch(Exception e){

                            e.printStackTrace();

                   }finally{

                            try{

                                     br.close();

                            }catch(Exception e){

                                     e.printStackTrace();

                            }

                            try{

                                     pw.close();

                            }catch(Exception e){

                                     e.printStackTrace();

                            }

                            try{

                                     client.close();

                            }catch(Exception e){}

                   }       

         }

         public static void main(String[] args){

                   SocketClient a=new SocketClient();

                   a.client();

         }

}

package c;

public class OuterClass{   

         private int a1;

         public int a2;

         protected int a3;

         int a4;

         public int v = 123;

         public OuterClass(){}

         public OuterClass(int a1,int a2,int a3,int a4){

                   this.a1 = a1;

                   this.a2 = a2;

                   this.a3 = a3;

                   this.a4 = a4;

         }

         //内部类

         public class InnerClass{

                   public int v = 456;

                   public int b1 = 1;

                   private int b2 = 2;              //public static int b3 = 5;

                            /*编译错误,不允许有静态

                   public static class D{

                   }*/

                   public class C{

                            public void method1(){

                                     System.out.println("c class");

                            }

                   }

                   public int add(int a,int b){

                            return a+b;

                   }

                   public void print(){

                            int v =100;

                            System.out.println("method :" + v);

                            System.out.println("inner : " + this.v);

                            System.out.println("outer : " + OuterClass.this.v);                                                         

                            System.out.println("a1:"+a1+",a2:"+a2+",a3:"+a3+",a4:"+a4);

                            System.out.println("****************************2****");

                   }

         }

         //外部类实例化内部类的对象引用

         public InnerClass inner = new InnerClass();

         //实例了内部类的引用就可以外部类方法调用内部类方法了

         public int add(int a,int b){

                   return inner.add(a,b);

         }

         //外部类中能否访问内部类的属性

         public void print(){

                   //InnerClass i = new InnerClass();

                   System.out.println("b1:"+inner.b1+",b2:"+inner.b2);

         }

         public static void main(String[] args){

                   OuterClass out = new OuterClass();

                   int result = out.add(15,33);

                   System.out.println("result: " + result);

                   //实例内部类的声明方式

                   OuterClass.InnerClass inner = new OuterClass(1,2,3,4).new InnerClass();

                   inner.print();

                   System.out.println("------------------------------");

                   out.print();

                   OuterClass.InnerClass.C c = new OuterClass().new InnerClass().new C();

                   c.method1();

         }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值